Program compiling method, program running method, chip, equipment and medium

By evaluating the application's function call relationship and data requirements after interruption, determining the application's running stack space size, solving the problems of memory waste and stack overflow in the existing technology, and achieving more accurate stack space allocation.

CN119201129APending Publication Date: 2024-12-27ARM TECH CHINA CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411281087.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-12
Publication Date
2024-12-27

AI Technical Summary

Technical Problem

In the prior art, the size of the running stack space of an application is set by manual experience values, resulting in the inability to avoid memory waste and stack overflow problems.

Method used

By traversing the application's functions, evaluate the stack space size of the first data required and the second data required after the interrupt, and use the sum of the two as the running stack space size to ensure the accuracy of the stack space size and avoid Memory waste and stack overflow.

Benefits of technology

Improve the accuracy of the stack space allocated by the application, avoiding memory waste caused by too large stack space and overflow caused by too small stack space.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119201129A_ABST
    Figure CN119201129A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of memory processing, in particular to a program compiling method, a program running method, a chip, equipment and a medium. According to the method, in the program compiling method, a processor can traverse all functions in a first application program with the running stack space size to be set, and the first stack space size for storing first data needed by the first application program in the running process of the first application program is determined. Moreover, the processor can determine the size of a second stack space for storing second data required by the first application program after the first application program is interrupted. And then, the processor can determine the size of the running stack space of all functions in the first application program according to the sum of the size of the first stack space and the size of the second stack space. In this way, the accuracy of the size of the stack space allocated to the first application program can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of memory technology, and in particular to a program compiling method, a program running method, a chip, a device and a medium. Background Art

[0002] When the application is running, the processor can allocate stack space for the application to store the data required for all function calls or calls during the application running process, such as the return address, parameters, local variables, etc. of the function. In addition, the processor can call each function in a certain logical order to complete the corresponding task. Among them, the stack space is a memory area that inserts new data and deletes old data according to the last in first out (LIFO) principle. In addition, the stack space, as a dynamic memory area in the memory, plays an important role in the normal operation of the application. During the running process of the application, data can be pushed into the stack space when context switching is performed, and data can be popped from the top of the stack space when reading data.

[0003] At present, the running stack space size of an application is usually set based on manual experience values, and once set, it cannot be modified. However, when the running stack space size of an application is set to be large, that is, when the stack space allocated to the application is large, some memory will be wasted; when the running stack space size of an application is set to be small, that is, when the stack space allocated to the application is small, the stack space will be insufficient to store the data required for function calls or calls, which will cause stack overflow. Therefore, the existing method of setting the running stack space size of an application based on manual experience values ​​cannot avoid memory waste and stack overflow. Summary of the invention

[0004] In order to solve the problem that the existing method of setting the running stack space size of the application based on manual experience values ​​cannot avoid memory waste and stack overflow, the embodiments of the present application provide a program compilation method, a program running method, a chip, a device and a medium.

[0005] In a first aspect, an embodiment of the present application provides a program compilation method, which is applied to an electronic device, comprising: obtaining a first application to be compiled; traversing the functions of the first application, evaluating a first stack space size for storing first data required by the functions of the first application during the running of the first application, and predicting a second stack space size for storing second data required by the functions of the first application after an interruption of the first application; determining the sum of the first stack space size and the second stack space size as the running stack space size of the functions of the first application.

[0006] Based on the above solution, the electronic device can improve the accuracy of the stack space size allocated to the first application.

[0007] In some optional implementations of the first aspect, the function of the first application includes multiple functions, the first data includes at least one of the parameters, return addresses and local variables of each of the multiple functions; the second data includes at least one of the address of the next instruction and the register value of the instruction executed by the electronic device when the first application is interrupted.

[0008] In some optional implementations of the first aspect, functions of a first application are traversed to evaluate a first stack space size of first data required by the functions of the first application during the running of the first application, including: traversing the functions of the first application to determine a calling relationship between multiple functions; and based on the calling relationship between the multiple functions, evaluating a first stack space size of first data required by the functions of the first application during the running of the first application.

[0009] In some optional implementations of the first aspect, the calling relationship between multiple functions is determined, including: determining a function call tree corresponding to the function of the first application; based on the calling relationship between the multiple functions, evaluating the first stack space size of the first data required by the function of the first application during the running of the first application, including: based on the function call tree corresponding to the multiple functions, evaluating the first stack space size of the first data required by the function of the first application during the running of the first application.

[0010] In some optional implementations of the first aspect, the sum of the first stack space size and the second stack space size is determined as the running stack space size of the function of the first application, including: corresponding to the sum of the first stack space size and the second stack space size being less than a preset value, the sum of the first stack space size and the second stack space size is used as the running stack space size of the function of the first application.

[0011] It is understandable that the preset value may be N times the total memory value, and when the sum of the first stack space size and the second stack space size is less than N times the total memory value, the running stack space size of the function of the first application is determined. In this way, while improving the accuracy of the stack space size allocated to the first application, it is possible to avoid the stack space size allocated to the first application being too large, resulting in excessive memory usage.

[0012] In a second aspect, an embodiment of the present application provides a program running method, applied to an electronic device, comprising: detecting a running instruction of a first application; obtaining the running stack space size of a function of the first application; and allocating memory to the first application based on the running stack space size.

[0013] Based on the above solution, when running the first application, the processor can allocate memory to the first application based on the running stack space size of all functions in the first application to facilitate the normal operation of the first application. In this way, it is possible to avoid memory waste caused by a large stack space allocated to the first application, and to avoid stack overflow caused by a small stack space allocated to the first application.

[0014] In some optional implementations of the second aspect, the running stack space size of the function of the first application is determined in the following manner: obtaining the first application to be compiled; traversing the functions of the first application, evaluating the first stack space size for storing first data required by the functions of the first application during the running of the first application, and predicting the second stack space size for storing second data required by the functions of the first application after an interruption of the first application; and determining the sum of the first stack space size and the second stack space size as the running stack space size of the function of the first application.

[0015] In a third aspect, an embodiment of the present application provides a chip, including a processor, and the processor is used to execute any of the above-mentioned program compilation methods, or execute any of the above-mentioned program running methods.

[0016] In a fourth aspect, an embodiment of the present application provides an electronic device, including: the above-mentioned chip.

[0017] In a fifth aspect, an embodiment of the present application provides a readable storage medium, on which instructions are stored. When the instructions are executed on an electronic device, the electronic device executes any of the above-mentioned program compilation methods, or executes any of the above-mentioned program running methods. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] Figure 1 A schematic structural diagram of an electronic device is shown;

[0019] Figure 2A According to some examples of the present application, a flowchart of a program compiling method is shown;

[0020] Figure 2B According to some examples of the present application, a flowchart of a program running method is shown;

[0021] Figure 3 According to some examples of the present application, a flow chart of a memory allocation method is shown;

[0022] Figure 4 According to some examples of the present application, a schematic diagram of the hardware structure of an electronic device is shown. DETAILED DESCRIPTION

[0023] The illustrative embodiments of the present application include, but are not limited to, a program compiling method, a program running method, a chip, an electronic device, and a storage medium.

[0024] It is understandable that the program compiling method and program running method mentioned in the embodiments of the present application can be applied to any processor or chip. For example, processors using X86 architecture, RISC-V architecture, ARM architecture, MIPS architecture, PowerPC architecture, SPARC architecture, and DEC Alpha architecture.

[0025] As mentioned above, the existing method of setting the running stack space size of an application based on manual experience values ​​will cause some memory waste when the stack space allocated to the application is large; when the stack space allocated to the application is small, the stack space will be insufficient to store the data required for function calls or being called, which will cause stack overflow.

[0026] It can be understood that during the running process of the application, different functions can be called to perform specific functions and tasks. In addition, during the function calling process, the processor needs to allocate stack space for the application to store the actual parameter values ​​passed to the called function (referred to as the function parameters), the location where the function returns after execution (referred to as the function return address), and the variables defined inside the function (referred to as local variables). In addition, after the application is interrupted, the processor also needs to allocate stack space for the application to store the address of the next instruction of the current instruction executed by the application (referred to as the breakpoint address), register values, and other second data.

[0027] Therefore, in order to solve the above problem, an embodiment of the present application provides a program compilation method. In this method, a processor can traverse all functions in a first application program whose running stack space size is to be set, and determine a first stack space size for storing first data required by the first application program during the running process of the first application program. In addition, the processor can determine a second stack space size for storing second data required by the first application program after the first application program is interrupted. Then, the processor can determine the running stack space size of all functions in the first application program based on the sum of the first stack space size and the second stack space size. In this way, the accuracy of the stack space size allocated to the first application program can be improved.

[0028] Furthermore, when running the first application, the processor can allocate memory to the first application based on the running stack space size of all functions in the first application to facilitate the normal operation of the first application. In this way, memory waste caused by a large stack space allocated to the first application can be avoided, and stack overflow caused by a small stack space allocated to the first application can be avoided.

[0029] It can be understood that the function call tree is a tree structure used to represent the calling relationship and hierarchy between functions, and the function call tree can clearly show deeply nested function calls. In this way, the processor can traverse all functions in the first application and determine the function call tree representing the calling relationship between all functions in the first application, so that the first stack space size for storing the first data required by the first application can be preliminarily determined based on the calling relationship between all functions in the first application during the running of the first application. In this way, the stack space size required for storing data of recursive or deeply nested functions can be avoided due to omission, and the accuracy of the stack space size allocated for the first application can be further improved.

[0030] In some embodiments, during the operation of some applications, there are some scenarios where these applications need to be interrupted in order to run other applications with higher running priorities. For example, if the preset configuration of the electronic device is that the system music application can be interrupted by a system photography application with a higher running priority, then during the operation of the system music application, the system music application may be interrupted by a system photography application with a higher running priority. At this time, interrupting the currently running system music application and running the system photography application also requires a certain amount of stack space to store data, such as storing the current playback position of the system music application. Therefore, when setting the running stack space size of the system music application, the processor can pre-configure an additional stack space for storing the data of the system music application after interrupting the currently running system music application to deal with this abnormal situation. Specifically, the configuration of the stack space corresponding to the abnormal situation can be determined based on the actual needs of different applications, and is not limited here.

[0031] It is understandable that the program compiling method and program running method mentioned in the embodiments of the present application can be applied to electronic devices. The structure of the electronic device mentioned in the embodiments of the present application is introduced below.

[0032] Figure 1 FIG. 1 shows a schematic diagram of the structure of an electronic device. Figure 1 As shown, the electronic device may include a processor 110 and a memory 120 .

[0033] In some specific implementations, the processor 110 may be an integrated circuit chip. The processor 110 may include one or more processing cores (e.g., a single-core processor, a multi-core processor). Exemplarily, the processor 110 may include a central processing unit (CPU), an application specific integrated circuit (ASIC), an application specific instructionset processor (ASIP), a graphics processing unit (GPU), a physics processing unit (PPU), a digital signal processor (DSP), a field programmable gate array (FPGA), a programmable logic device (PLD), a controller, a microcontroller unit, a reduced instruction set computer (RISC), a microprocessor, or any combination thereof.

[0034] In some specific implementations, the memory 120 may be used to store instructions and data. The memory 120 may be a random access memory (RAM).

[0035] In practical applications, for the first application whose running stack space size is to be set, the processor 110 can determine the first stack space size based on the function call tree of all functions in the first application and the first data required when each function in the function call tree is called or called. And the second stack space size required for each function in the first application to store the second data for an abnormal situation (such as an interruption situation) during execution can be obtained. In addition, the running stack space size of the first application can be determined based on the first stack space size and the second stack space size. Thus, when it is detected that the first application is running, memory can be allocated to the first application from the memory 120 based on the running stack space size of the first application. In this way, the accuracy of the stack space size allocated to the first application can be improved. In addition, the memory waste caused by the large stack space allocated to the first application can be avoided, and the stack overflow caused by the small stack space allocated to the first application can be avoided.

[0036] It can be understood that the source code of the functions in the application needs to be processed through stages such as compilation, assembly and linking before it can be converted into an executable file that can be directly recognized and run by the computer system.

[0037] Among them, during the compilation stage, the compiler can traverse the source code (including functions) of the application, generate a function call tree, and perform semantic analysis, type checking and other processing on the function call tree to determine the correctness of the source code. In addition, the compiler can convert the source code of the application into assembly language. For example, ARM Compiler 6 (a compiler) can traverse the source code of the application and generate a function call tree (for example, when compiling, the compilation option - callgraph is turned on), wherein the function call tree can be represented in hypertext markup language (html) or text format. In addition, the stack space for storing the data required for each function call or call in the function call tree can be output.

[0038] During the assembly stage, the assembly language corresponding to the source code of the application can be converted into binary machine language and a target file can be generated.

[0039] During the linking stage, multiple target files and runtime libraries that provide common functional support can be linked to form an executable file that can be directly recognized and run by the computer.

[0040] As mentioned above, in order to solve the problem that the existing method of setting the running stack space size of the application based on manual experience values ​​cannot avoid memory waste and stack overflow, in some specific implementation methods, in the compilation stage, a static analysis method can be used to determine the running stack space size of the application. For example, the running stack space size of the application can be determined based on the calling relationship between functions described by the function call tree generated by the compiler and the first data required when each function is called or called.

[0041] However, in the above static analysis method, it is not taken into account that during the running of the application, when the application needs to be interrupted to run other applications with higher priority, the stack space required to store the second data of the application is not taken into account. For example, during the running of the system music application, the system music application will be interrupted by the system photography application. The stack space required by the runtime library that provides support for common functions is not taken into account, and the abnormal situations such as different stack space sizes required for different recursive times when different actual parameters are input externally for recursive functions (such as functions that call themselves) are not taken into account. This will result in a smaller running stack space size for the determined application, resulting in insufficient stack space to store the data required for function calls or being called, which will cause a stack overflow.

[0042] In some specific implementations, the running stack space size of the application may be determined by dynamic analysis. For example, the application may be run to determine the running stack space size of the application during the running of the application. For example, when setting the running stack space size for a system music application, the system music application may be run to determine the running stack space size of the system music application.

[0043] However, in the above-mentioned dynamic analysis method, the running stack space size required during the current running process of the application cannot represent the running stack space size required for each function of the application during each running process. For example, when setting the running stack space size for the system music application, if during the running of the system music application, the application is interrupted to run other applications with higher priority, then it is determined that the running stack space size of the system music application includes the stack space size required to interrupt the application. However, during another running process of the application, there may be no need to interrupt the application to run other applications with higher priority. In this way, when considering interrupting the application to run other applications with higher priority to set the running stack space size of the application, the running stack space size set for the application may be larger, which will cause some memory waste;

[0044] For example, when setting the running stack size for the system music application, if the system music application is not interrupted to run other applications with higher priority during the running of the system music application, it is determined that the running stack size of the system music application does not include the stack size required to interrupt the application. However, during another running of the application, the application may need to be interrupted to run other applications with higher priority. In this case, the running stack size set for the application will be smaller, resulting in insufficient stack space to store the data required for function calls or calls, which will cause stack overflow.

[0045] It is understandable that the above-mentioned method of determining the stack space allocated to the process corresponding to the application by static analysis or dynamic analysis cannot avoid memory waste and stack overflow.

[0046] To this end, in an embodiment of the present application, the stack space allocated for the process corresponding to the application is determined by combining static analysis and dynamic analysis. For example, when setting the running stack space size of the system music application, all functions in the system music application can be traversed to evaluate the first stack space size for storing the first data required by the system music application during the running process of the system music program. In addition, the system music application can be run, and the interruption of the system music application can be controlled to obtain the second stack space size of the second data required by the system music application. Thus, the running stack space size of the system music program can be determined based on the first stack space size and the second stack space size. In this way, the accuracy of the stack space size allocated for the system music application can be improved. In addition, the memory waste caused by a large stack space allocated for the system music application can be avoided, and the stack overflow caused by a small stack space allocated for the system music application can be avoided.

[0047] The program compilation method mentioned in the embodiment of the present application is introduced in detail below.

[0048] Figure 2A A flowchart of a program compiling method is shown, and the program compiling method can be executed by an electronic device, and specifically can be executed by a processor in the electronic device. Figure 2A As shown, the program compilation method may include:

[0049] 201: Obtain a first application to be compiled.

[0050] It can be understood that the first application may be an application that needs to set the running stack space size.

[0051] 202: Traverse all functions in the first application program to determine a first stack space size of first data required by all functions in the first application program during the running of the first application program.

[0052] It can be understood that the function of the first application includes multiple functions, and the first data includes at least one of the parameters, return addresses and local variables of the multiple functions.

[0053] In some specific implementations, the functions of the first application can be traversed to determine the calling relationship between multiple functions; based on the calling relationship between the multiple functions, the first stack space size of the first data required by the functions of the first application during the running of the first application can be evaluated.

[0054] Specifically, a function call tree corresponding to a function of the first application can be determined, and based on the function call trees corresponding to multiple functions, a first stack space size of first data required by the function of the first application during the running of the first application can be evaluated.

[0055] 203: Predict a second stack space size for interrupting the first application and storing second data required by all functions in the first application.

[0056] In some specific implementations, the second data includes at least one of an address of a next instruction and a register value of an instruction executed by the processor when the first application is interrupted.

[0057] 204: Determine the sum of the first stack space size and the second stack space size as the running stack space size of all functions in the first application program.

[0058] It can be understood that after determining the first stack space size and the second stack space size, it can be determined whether the sum of the first stack space size and the second stack space size is less than N times the total memory value. When the sum of the first stack space size and the second stack space size is less than N times the total memory value, the running stack space size of the function of the first application is determined. In this way, while improving the accuracy of the stack space size allocated to the first application, it is possible to avoid the stack space size allocated to the first application being too large, resulting in excessive memory usage.

[0059] Figure 2B A flowchart of a program running method is shown, and the program running method can be executed by an electronic device, specifically by a processor in the electronic device. Figure 2B As shown, the program running method may include:

[0060] 205: When the execution instruction of the first application is detected, the execution stack space size of all functions in the first application is obtained.

[0061] In some specific implementations, when the electronic device detects a user's startup operation on the first application, or detects that the electronic device displays a startup interface of the first application, it can be determined that a running instruction of the first application is detected.

[0062] In some specific implementations, the running stack space size of all functions in the first application program can be Figure 2A The steps shown are determined and will not be described again here to avoid repetition.

[0063] 206: Allocate memory for the first application based on the running stack space size of all functions in the first application.

[0064] In the embodiment of the present application, memory waste caused by a large stack space allocated to the first application can be avoided, and stack overflow caused by a small stack space allocated to the first application can be avoided.

[0065] Figure 3 FIG. 1 is a flow chart of another memory allocation method, which is applied to electronic devices. Figure 3 As shown, the memory allocation method may include:

[0066] 301: Configure the location where the first application program is interrupted to run the second application program.

[0067] It is understood that the first application may be an application for which the running stack space size needs to be set, and the second application may be an application with a higher priority that interrupts the running of the first application. Furthermore, the first application and the second application may be system applications that come with the electronic device, such as applications that are built into the electronic device before the electronic device leaves the factory and have privileged permissions to use.

[0068] In some specific implementations, the simulator test bench may configure a location where the first application program is interrupted to run the second application program in response to a setting operation of the user.

[0069] Since the first application and the second application are system applications that come with the electronic device, the priority of the first application and the second application can be configured. For example, the priority of the second application can be configured to be higher than the priority of the first application. And, when the priority of the second application is configured to be higher than the priority of the first application, the location where the first application is interrupted to run the second application can be configured. Assume that the first application includes function A, function B, and function C, where function A calls function B, and function B calls function C. In this way, it can be configured that after function A calls function B, the first application is interrupted and the second application is run; it can also be configured that after function B calls function C, the first application is interrupted and the second application is run. The embodiment of the present application does not specifically limit the location where the first application is interrupted to run the second application.

[0070] It is understandable that the location of interrupting the second application to run the third application can also be configured, where the third application can be an application with a higher priority than the second application, and the location of interrupting the third application to run other applications can also be configured. The embodiment of the present application does not limit the specific nested interrupt method. For ease of understanding, the following description is based on the example of only configuring the location of interrupting the first application to run the second application.

[0071] 302: Initialize the stack top address and size of the stack space allocated for the first application and the data stored in the stack space, and run the first application to obtain the stack space required for storing the first data during the running of the first application.

[0072] It can be understood that the size can refer to the size of the stack space, and the memory initialization function of the simulator test bench can be used to initialize the stack top address, size and data stored in the stack space allocated for the first application. For example, a fixed value can be used to fill the stack space allocated in advance for the first application. Thus, when the first application is running, the size of the stack space required for storing the first data during the operation of the first application can be determined according to whether the fixed value in the stack space allocated for the first application changes. For example, the space where the fixed value changes can be determined as the stack space required for storing the first data during the operation of the first application. For example, the fixed value can be "012345", and when the value in the stack space is "111111", it can be determined that the stack space is the stack space required by the first application. In this way, the accuracy of the running stack space size set for the first application can be improved, and the memory waste caused by the large stack space allocated for the first application can be avoided, and the stack space allocated for the first application is small, resulting in insufficient stack space to store the data required when the function is called or called, which causes a stack overflow.

[0073] It is understandable that after the first application is run, a tracking log of the first application can be stored, wherein the tracking log can record the calling status of each function in the first application.

[0074] It can be understood that, since the position where the first application is interrupted to run the second application is configured in step 301, when the first application is interrupted to run the second application, the first application will be interrupted and the second application will be run. In other words, during the process of running the first application, the second application will also be run. Therefore, the tracking log can also record the calling status of each function in the second application.

[0075] Furthermore, when the second application is running, the stack space size required by the second application may be preset by the user.

[0076] In the process of interrupting the first application to run the second application, stack space needs to be allocated to store the data required by the interruption processing program, that is, to save the "scene" of the first application. The size of the stack space required by the interruption processing program can also be preset by the user. For example, the simulator test bench can set the size of the stack space required by the interruption processing program in response to the user's setting operation.

[0077] It can be understood that after determining the stack space size required by the first application, the stack space size required by the second application that interrupts the first application, and the stack space size required by the interrupt handler, the stack space size allocated for the first application can be preliminarily determined based on the stack space size required by the first application, the stack space size required by the second application, and the stack space size required by the interrupt handler. For example, the sum of the stack space size required by the first application, the stack space size required by the second application, and the stack space size required by the interrupt handler can be determined as the running stack space size allocated for the first application.

[0078] 303: Traverse all functions in the first application program and generate a function call tree.

[0079] It can be understood that, during the compilation phase, the compiler can traverse all functions in the first application program to generate a function call tree. For example, based on the compilation options "--callgraph" and "-info=stack" in ARM Compiler 6, the source code of the first application program can be compiled to generate a function call tree (e.g., callgraph). The function call tree can be represented in a hypertext markup language (html) or text format.

[0080] In some specific implementations, the compiler may also output a stack space for storing data required for each function call or being called that is preliminarily estimated in the function call tree.

[0081] 304: Generate a hash table for each function in the function call tree, run the first application, and obtain the stack space size required by each function in the first application.

[0082] It can be understood that in order to enable the hardware to identify the function call tree in the hypertext markup language or text format, the function call tree can be converted into a hash table. In the hash table, the function name of the function in the first application can be used as a key, and the starting address and the end address of the function stored in the memory can be used as values. In addition, when the first application is run, the memory address pointed to by the stack pointer can be obtained in real time, and according to the memory address executed by the stack pointer obtained in real time, the stack space size required for each function in the first application is dynamically obtained, and a serial port information log (uart log) is output, wherein the serial port information log can include a hash table of each function and the stack space size required for each function.

[0083] In some optional implementations, the running stack space size required by the first application can be determined based on the stack space size required by each function in the first application. Furthermore, the performance of the first application can be optimized based on the stack space size required by each function in the first application. In this way, the system performance can be improved on the basis of improving the accuracy of determining the stack space required by the first application.

[0084] It can be understood that step 302 and step 304 respectively adopt different methods to determine the size of the running stack space required by the first application. In the actual processing process, the size of the running stack space required by the first application can be determined by step 302, and the size of the running stack space required by the first application can also be determined by step 304. The accuracy of the determined stack space required for the first application can also be further improved by comparing the sizes of the running stack space required by the first application determined by step 302 and step 304.

[0085] 305: Store the tracking log of the first application, the stack space size required by the second application, the serial port information log of each function of the first application, and the stack space size required by the interrupt handler.

[0086] It can be understood that when the first application is run using a real time operating system (RTOS), the stack space size required by the second application can be referred to as the stack usage for context switching. In a bare metal scenario (a scenario without an RTOS), the stack space size required by the second application can be referred to as the stack usage required by the exception handler.

[0087] 306: Scan the tracking log of the first application program and update the function call tree.

[0088] It can be understood that the tracking log of the first application can be scanned to obtain the calling status of each function in the first application, and the function call tree in step 303 can be updated based on the calling status of each function in the first application and the calling status of each function in the second application, so that the updated function call tree can not only represent the calling status of the function in the first application, but also represent the calling status of the function in the second application that interrupts the operation of the first application.

[0089] 307: Determine whether the size of the execution stack space allocated for the first application meets the allocation condition.

[0090] It can be understood that after the stack space size allocated to the first application is preliminarily determined based on the stack space size required by the first application, the stack space size required by the second application, and the stack space size required by the interrupt handler, it can be determined whether the running stack space size allocated to the first application meets the allocation conditions. For example, whether the running stack space size allocated to the first application is less than N times the total memory value, where N can be between [0.1, 0.4], for example, it can be 0.1, 0.2, 0.3, etc.

[0091] If it is determined that the size of the running stack space allocated for the first application is less than N times the total memory value, the process may proceed to step 308, and the size of the stack space allocated for the first application, which is initially determined, may be used as the stack space finally allocated for the first application. If it is determined that the stack space allocated for the first application is greater than or equal to N times the total memory value, the process may proceed to step 302, and the stack top address and size of the stack space allocated for the first application, as well as the data stored in the stack space, may be initialized, and the first application may be run to obtain the running stack space size required by the first application.

[0092] 308: Determine the size of the execution stack space allocated for the first application.

[0093] It can be understood that, in the case where it is determined that the size of the running stack space allocated for the first application is less than N times the total memory value, the stack space initially determined to be allocated for the first application can be used as the size of the running stack space finally allocated for the first application. In this way, while improving the accuracy of the stack space size allocated for the first application, it is possible to avoid the stack space size allocated for the first application being too large, resulting in excessive memory usage. In addition, it is possible to avoid memory waste caused by a large stack space allocated for the first application, and to avoid stack overflow caused by a small stack space allocated for the first application.

[0094] It can be understood that the memory allocation method mentioned in the embodiment of the present application can be applied to electronic devices. The hardware structure of the electronic device is introduced below. Figure 4 As shown, Figure 4 It is understood that the electronic device of the present application can be an electronic device, a desktop computer, a handheld computer, a notebook computer, etc. The structure of the electronic device is introduced below.

[0095] In one embodiment, the electronic device may include one or more processors 401, a system control logic 402 connected to at least one of the processors 401, a system memory 403 connected to the system control logic 402, a non-volatile memory (NVM) 404 connected to the system control logic 402, and an input / output (I / O) device 405 and a network interface 406 connected to the system control logic 402.

[0096] In some embodiments, the processor 401 may include one or more single-core or multi-core processors. In some embodiments, the processor 401 may include any combination of a general-purpose processor and a dedicated processor (e.g., a graphics processor, an application processor, a baseband processor, etc.). In an embodiment in which the electronic device uses an enhanced base station (Evolved Node B, eNB) or a radio access network (Radio Access Network, RAN) controller, the processor 401 may be configured to execute various embodiments that comply with the present invention.

[0097] In some embodiments, system control logic 402 may include any suitable interface controller to provide any suitable interface to at least one of processors 401 and / or any suitable device or component in communication with system control logic 402 .

[0098] In some embodiments, system control logic 402 may include one or more memory controllers to provide an interface to system memory 403. System memory 403 may be used to load and store data and / or instructions 4031. In some embodiments, the memory of the electronic device may include any suitable volatile memory, such as a suitable dynamic random access memory (DRAM).

[0099] The non-volatile memory (NVM) 404 may include one or more tangible, non-transitory computer-readable media for storing data and / or instructions. In some embodiments, the non-volatile memory (NVM) 404 may include any suitable non-volatile memory such as flash memory and / or any suitable non-volatile storage device, such as at least one of a HDD (Hard Disk Drive), a CD (Compact Disc) drive, and a DVD (Digital Versatile Disc) drive.

[0100] The non-volatile memory (NVM) 404 may include a portion of storage resources on a device on which the electronic device is installed, or it may be accessible by the device but not necessarily a portion of the device. For example, the non-volatile memory (NVM) 404 may be accessed over a network via the network interface 406 .

[0101] In particular, the system memory 403 and the non-volatile memory (NVM) 404 may include: a temporary copy and a permanent copy of the instruction, respectively. The instruction may include: an instruction that causes the electronic device to implement the memory allocation method mentioned in the embodiment of the present application when executed by at least one of the processors 401. In some embodiments, the instruction, hardware, firmware and / or its software components may be placed in the system control logic 402, the network interface 406 and / or the processor 401 in addition or alternatively.

[0102] The network interface 406 may include a transceiver for providing a radio interface for the electronic device, and then communicating with any other suitable device (such as a front-end module, an antenna, etc.) through one or more networks. In some embodiments, the network interface 406 can be integrated with other components of the electronic device. For example, the network interface 406 can be integrated with at least one of the processor 401, the system memory 403, the non-volatile memory (NVM) 404, and a firmware device (not shown) with instructions. When at least one of the processors 401 executes the instructions, the electronic device implements the memory allocation method mentioned in the embodiments of the present application.

[0103] The network interface 406 may further include any suitable hardware and / or firmware to provide a multiple-input multiple-output radio interface. For example, the network interface 406 may be a network adapter, a wireless network adapter, a telephone modem and / or a wireless modem.

[0104] In one embodiment, at least one of the processors 401 may be packaged together with logic for one or more controllers of the system control logic 402 to form a system in package (SiP). In one embodiment, at least one of the processors 401 may be integrated on the same die with logic for one or more controllers of the system control logic 402 to form a system on chip (SoC).

[0105] The electronic device may further include: an input / output (I / O) device 405. The I / O device 405 may include a user interface to enable a user to interact with the electronic device; the design of the peripheral component interface enables the peripheral component to also interact with the electronic device. In some embodiments, the electronic device further includes a sensor for determining at least one of an environmental condition and location information related to the electronic device.

[0106] In some embodiments, the user interface may include, but is not limited to, a display (e.g., an LCD display, a touch screen display, etc.), a speaker, a microphone, one or more cameras (e.g., a still image camera and / or a video camera), a flashlight (e.g., an LED flash), and a keyboard.

[0107] In some embodiments, the peripheral component interface may include, but is not limited to, a non-volatile memory port, an audio jack, and a power interface.

[0108] In some embodiments, the sensors may include, but are not limited to, gyroscope sensors, accelerometers, proximity sensors, ambient light sensors, and positioning units. The positioning unit may also be part of or interact with the network interface 406 to communicate with components of a positioning network (e.g., global positioning system (GPS) satellites).

[0109] The above describes the hardware structure that the electronic device may have. It is understood that the structure illustrated in the embodiments of the present application does not constitute a specific limitation on the electronic device. In other embodiments of the present application, the electronic device may include more or fewer components than shown in the figure, or combine certain components, or split certain components, or arrange the components differently. The illustrated components may be implemented in hardware, software, or a combination of software and hardware.

[0110] In the accompanying drawings, some structural or method features may be shown in a specific arrangement and / or order. However, it should be understood that such a specific arrangement and / or order may not be required. Instead, in some embodiments, these features may be arranged in a manner and / or order different from that shown in the illustrative drawings. In addition, the inclusion of structural or method features in a particular figure does not mean that such features are required in all embodiments, and in some embodiments, these features may not be included or may be combined with other features.

[0111] The embodiments disclosed in the present application may be implemented in hardware, software, firmware, or a combination of these implementation methods. The embodiments of the present application may be implemented as a computer program or program code executed on a programmable system, the programmable system including at least one processor, a storage system (including volatile and non-volatile memory and / or storage elements), at least one input device, and at least one output device.

[0112] Program code can be applied to input instructions to perform the functions described in this application and generate output information. The output information can be applied to one or more output devices in a known manner. For the purposes of this application, a processing system includes any system having a processor such as, for example, a digital signal processor (DSP), a microcontroller, an application specific integrated circuit, or a microprocessor.

[0113] Program code can be implemented with high-level programming language or object-oriented programming language to communicate with the processing system. When necessary, program code can also be implemented with assembly language or machine language. In fact, the mechanism described in this application is not limited to the scope of any specific programming language. In either case, the language can be a compiled language or an interpreted language.

[0114] In some cases, the disclosed embodiments may be implemented in hardware, firmware, software, or any combination thereof. The disclosed embodiments may also be implemented as instructions carried or stored on one or more temporary or non-temporary machine-readable (e.g., computer-readable) storage media, which may be read and executed by one or more processors. For example, instructions may be distributed over a network or through other computer-readable media. Therefore, machine-readable media may include any mechanism for storing or transmitting information in a machine (e.g., computer) readable form, including, but not limited to, floppy disks, optical disks, optical disks, read-only memories (CD-ROMs), magneto-optical disks, read-only memories (ROMs), random access memories (RAMs), erasable programmable read-only memories (EPROMs), electrically erasable programmable read-only memories (EEPROMs), magnetic or optical cards, flash memory, or a tangible machine-readable memory for transmitting information (e.g., carrier waves, infrared signals, digital signals, etc.) using the Internet in electrical, optical, acoustic, or other forms of propagation signals. Therefore, machine-readable media include any type of machine-readable media suitable for storing or transmitting electronic instructions or information in a machine (e.g., computer) readable form.

[0115] It should be noted that the units / modules mentioned in the various device embodiments of the present application are all logical units / modules. Physically, a logical unit / module can be a physical unit / module, or a part of a physical unit / module, or can be implemented as a combination of multiple physical units / modules. The physical implementation method of these logical units / modules themselves is not the most important. The combination of functions implemented by these logical units / modules is the key to solving the technical problems proposed by the present application. In addition, in order to highlight the innovative part of the present application, the above-mentioned device embodiments of the present application do not introduce units / modules that are not closely related to solving the technical problems proposed by the present application, which does not mean that there are no other units / modules in the above-mentioned device embodiments.

[0116] It should be noted that in the examples and description of this patent, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "including one" do not exclude the existence of other identical elements in the process, method, article or device including the elements.

[0117] Although the present application has been illustrated and described with reference to certain embodiments thereof, it will be understood by those skilled in the art that various changes in form and details may be made therein without departing from the scope of the present application.

Claims

1. A program compiling method, applied to an electronic device, characterized in that: include: Obtaining a first application to be compiled; Traversing the functions of the first application, evaluating a first stack space size for storing first data required by the functions of the first application during the running of the first application, and predicting a second stack space size for storing second data required by a function of the first application after the first application is interrupted; The sum of the first stack space size and the second stack space size is determined as the running stack space size of the function of the first application.

2. The method according to claim 1, characterized in that The function of the first application includes a plurality of functions, and the first data includes at least one of a parameter, a return address, and a local variable of each of the plurality of functions; The second data includes at least one of an address of a next instruction and a register value of an instruction executed by the electronic device when the first application is interrupted.

3. The method according to claim 2, characterized in that The traversing the functions of the first application program to evaluate the first stack space size of the first data required by the functions of the first application program during the running of the first application program includes: Traversing the functions of the first application program to determine the calling relationship between the multiple functions; Based on the calling relationship between the multiple functions, a first stack space size of first data required by the function of the first application program during the running of the first application program is evaluated.

4. The method according to claim 3, characterized in that Determining the calling relationship between the multiple functions includes: Determining a function call tree corresponding to the function of the first application; The evaluating, based on the calling relationship between the multiple functions, a first stack space size of first data required by the function of the first application during the running of the first application includes: Based on the function call trees corresponding to the multiple functions, a first stack space size of first data required by the functions of the first application program during the running of the first application program is evaluated.

5. The method according to claim 1, characterized in that The determining the running stack space size required for the function running of the first application program by summing the first stack space size and the second stack space size includes: Corresponding to the sum of the first stack space size and the second stack space size being smaller than a preset value, the sum of the first stack space size and the second stack space size is used as the running stack space size of the function of the first application.

6. A program running method, applied to an electronic device, characterized in that: include: detecting an execution instruction of a first application program; Obtaining the size of the running stack space of the function of the first application program; Based on the size of the running stack space, memory is allocated to the first application.

7. The method according to claim 6, characterized in that The size of the running stack space of the function of the first application program is determined in the following manner: Obtaining a first application to be compiled; Traversing the functions of the first application, evaluating a first stack space size for storing first data required by the functions of the first application during the running of the first application, and predicting a second stack space size for storing second data required by a function of the first application after the first application is interrupted; The sum of the first stack space size and the second stack space size is determined as the running stack space size required for the function running of the first application.

8. A chip, characterized in that: Including processor, The processor is used to execute the program compiling method according to any one of claims 1 to 5, or to execute the program running method according to any one of claims 6 to 7.

9. An electronic device, characterized in that: include: The chip as claimed in claim 8.

10. A readable storage medium, characterized in that: The readable storage medium stores instructions, and when the instructions are executed on an electronic device, the electronic device executes the program compiling method described in any one of claims 1 to 5, or executes the program running method described in any one of claims 6 to 7.