Memory management method and device, equipment, storage medium and program product
Through the hierarchical memory management strategy, memory pool or memory heap management mode is adopted for different functional modules, which solves the system instability problem caused by memory allocation failure and achieves a balance between system stability and memory resource utilization.
Patent Information
- Application Number
- CN202510750900.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-05
- Publication Date
- 2025-09-12
AI Technical Summary
The existing memory heap management method is prone to memory allocation failure when subjected to high-traffic data impact, causing some modules to fail to operate normally, system dysfunction or even crash, and reducing system stability and reliability.
By determining the module types of different functional modules in the target system, the memory pool management mode is used to allocate a preset memory pool for the first type of modules, and the memory heap management mode is used to allocate memory for the second type of modules. The advantages of pre-allocated memory pool and dynamic memory allocation are combined to achieve hierarchical memory management.
Ensure that key modules can still successfully apply for memory under high load or severe heap fragmentation, reduce system instability caused by memory allocation failure, and improve the overall stability and reliability of the system.
Smart Images

Figure CN120631797A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a memory management method, apparatus, device, storage medium, and program product. Background Art
[0002] As electronic devices become increasingly complex, the number of functional modules included in the system continues to increase. These functional modules often need to frequently allocate and release memory during operation for tasks such as data storage, message transmission, and cache management.
[0003] In the related art, commonly used memory management solutions include memory pool management and memory heap management. Compared with the memory pool management method that allocates fixed-size memory in batches, the memory heap management method can dynamically allocate memory blocks of corresponding sizes based on the storage requirements of the module. When the system memory resources are limited, it can effectively reduce memory usage and improve memory utilization and allocation flexibility. However, when encountering high-traffic data impact, if the memory heap management method is used to allocate memory resources, memory allocation failure may occur, causing some modules to fail to operate normally due to memory allocation failure, which in turn causes system dysfunction or even crash, reducing system stability and reliability. Summary of the Invention
[0004] The present invention provides a memory management method, apparatus, device, storage medium and program product to solve the problem that when memory resources are allocated using a memory heap management method, some modules cannot operate normally due to memory allocation failure, thereby reducing the stability and reliability of the system.
[0005] In a first aspect, an embodiment of the present application provides a memory management method, comprising:
[0006] Determine the module types of different functional modules in the target system, where the module type is used to indicate whether the functional module is a first-class module that works abnormally after a memory allocation failure;
[0007] When the module type of the functional module indicates that the functional module is a first-category module, memory management is performed on the functional module according to the memory pool management mode, so as to allocate a corresponding preset memory pool to the functional module, and memory allocation is performed on the functional module based on the preset memory pool;
[0008] When the module type of the functional module indicates that the target module is a second-category module, memory management is performed on the functional module according to the memory heap management mode, so as to perform memory allocation based on the memory heap on the functional module.
[0009] In one embodiment, when the module type of the functional module indicates that the functional module is a first-category module, memory management is performed on the functional module according to the memory pool management mode to allocate a corresponding preset memory pool to the functional module, and memory allocation is performed on the functional module based on the preset memory pool, including:
[0010] When the type of the functional module indicates that the functional module is a first-category module, performing a memory pool initialization operation on the functional module to allocate a preset memory space for the functional module, and dividing the allocated preset memory space into a plurality of fixed-size memory blocks to obtain a preset memory pool corresponding to the functional module;
[0011] Generate memory descriptors of functional modules based on the structure information of the preset memory pool;
[0012] When a functional module has storage requirements, an idle target memory block is allocated to the functional module in a preset memory pool according to the memory descriptor of the functional module, and the data to be stored of the functional module is stored in the target memory block.
[0013] In one embodiment, generating a memory descriptor of a functional module according to structural information of a preset memory pool includes:
[0014] Determine the base address of the preset memory pool and the first free memory block in the preset memory pool;
[0015] According to the base address of the preset memory pool and the first free memory block, a base address pointer pointing to the preset memory pool and a memory block pointer pointing to the first free memory block are generated to obtain a memory descriptor of the functional module.
[0016] In one embodiment, when a functional module has a storage requirement, allocating an idle target memory block to the functional module in a preset memory pool according to the memory descriptor of the functional module includes:
[0017] When a functional module has storage requirements, a memory descriptor of the functional module is obtained. The memory descriptors of different types of functional modules contain different information. The memory descriptor of the first type of module additionally contains a base address pointer pointing to a preset memory pool actually allocated to the first type of module, and a memory block pointer pointing to the first free memory block in the preset memory pool.
[0018] According to the base address pointer and the memory block pointer in the memory descriptor, the first free memory block in the preset memory pool is determined to obtain the target memory block.
[0019] In one embodiment, before determining the module types of different functional modules in the target system, the method further includes:
[0020] After the target system is started, a memory descriptor template of each functional module is generated according to the memory configuration information of the functional module. The memory descriptor template is a data structure used to describe the memory description information of the functional module. The parameters contained in the memory descriptor template of different types of functional modules are different.
[0021] According to the memory descriptor template of each functional module, the module type of different functional modules in the target system is determined, and different memory management strategies are implemented for different types of functional modules.
[0022] In one embodiment, determining the module types of different functional modules in the target system includes:
[0023] Get the preset fields defined for the first type of module;
[0024] The module types of different functional modules in the target system are determined according to the matching conditions between the parameter fields in the memory descriptor template of each functional module and the preset fields.
[0025] In one embodiment, the preset field includes a memory pool name, and the module type of different functional modules in the target system is determined based on the matching between the information in the memory descriptor template of each functional module and the preset field, including:
[0026] For each functional module, read the memory name field in the memory descriptor template of the functional module;
[0027] When the memory name field in the memory descriptor template matches the memory pool name of the first-category module, determining that the module type of the functional module is a first-category module;
[0028] When the memory name field in the memory descriptor template does not match the memory pool name of the first type module, it is determined that the module type of the functional module is a second type module.
[0029] In a second aspect, an embodiment of the present application provides a memory management device, including:
[0030] A determination module is used to determine module types of different functional modules in the target system, where the module type is used to indicate whether the functional module is a first type module that works abnormally after a memory allocation failure;
[0031] a first management module configured to, when the module type of the functional module indicates that the functional module is a first-category module, perform memory management on the functional module according to the memory pool management mode, allocate a corresponding preset memory pool to the functional module, and perform memory allocation on the functional module based on the preset memory pool;
[0032] The second management module is used to manage the memory of the functional module according to the memory heap management mode when the module type of the functional module indicates that the target module is a second type module, so as to allocate memory to the functional module based on the memory heap.
[0033] In a third aspect, an embodiment of the present application provides an electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the above-mentioned memory management method when executing the computer program.
[0034] In a fourth aspect, an embodiment of the present application provides a readable storage medium, which stores a computer program, and when the computer program is executed by a processor, the steps of the above-mentioned memory management method are implemented.
[0035] In a fifth aspect, an embodiment of the present application provides a computer program product, including a computer program, which enables the above-mentioned memory management method to be executed when the computer program is run.
[0036] In one solution provided by the above-mentioned memory management method, apparatus, device, storage medium and program product, by determining the module types of different functional modules in the target system, the module type is used to indicate whether the functional module is a first-type module that works abnormally after memory allocation fails, so as to execute different memory management strategies for different types of functional modules according to the module type of the functional module; wherein, when the module type of the functional module indicates that the functional module is a first-type module, the functional module is memory managed according to the memory pool management mode, so as to allocate a corresponding preset memory pool to the functional module, and memory allocation is performed on the functional module based on the preset memory pool; when the module type of the functional module indicates that the target module is a second-type module, the functional module is memory managed according to the memory heap management mode, so as to allocate memory to the functional module based on the memory heap. On the one hand, for the second type of modules whose work is not affected by memory allocation failure, the memory heap management mode is adopted to dynamically manage the memory of this type of functional modules, which can reduce the situation of extra memory occupation and improve the utilization rate of memory resources; on the other hand, for the first type of modules that have abnormal work after memory allocation failure, the memory pool management mode is adopted to manage the memory of this type of functional modules, which can ensure that this type of modules can successfully apply for memory even when the system is under high load or heap fragmentation is serious, realize real-time and stable memory allocation, reduce the situation of system instability caused by abnormal work due to memory allocation failure, and ensure the stability of system operation. In this embodiment, the hierarchical memory management mode based on different types of functional modules combines the advantages of pre-allocated memory pool and dynamic memory allocation, can meet the memory management needs of different functional modules, not only strengthen the overall stability of the system, but also achieve a balance between system performance and memory resource utilization, and improve system reliability. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments of the present invention. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0038] Figure 1 is a schematic structural diagram of a target system in one embodiment of the present invention;
[0039] Figure 2 is a flow chart of a memory management method according to an embodiment of the present invention;
[0040] Figure 3 is another flowchart of a memory management method according to an embodiment of the present invention;
[0041] Figure 4 yes Figure 3 A schematic diagram of an implementation flow of step S01;
[0042] Figure 5 yes Figure 2 A schematic diagram of an implementation flow of step S10;
[0043] Figure 6 yes Figure 2 A schematic diagram of an implementation flow of step S20;
[0044] Figure 7 is a schematic diagram of determining different memory management modes in one embodiment of the present invention;
[0045] Figure 8 yes Figure 1 A structural diagram of a memory management device;
[0046] Figure 9 FIG. 1 is a structural diagram of an electronic device in an embodiment of the present invention. DETAILED DESCRIPTION
[0047] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of them. All other embodiments obtained by ordinary technicians in this field based on the embodiments of the present invention without making any creative efforts shall fall within the scope of protection of the present invention.
[0048] It should be understood that when used in the present specification and the appended claims, the term "comprising" indicates the presence of the described features, integers, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or their collections. It should also be understood that the term "and / or" used in the present specification and the appended claims refers to any combination and all possible combinations of one or more of the associated listed items, and includes these combinations.
[0049] In addition, in the description of the present specification and the appended claims, the terms "first", "second", "third", etc. are only used to distinguish the descriptions and cannot be understood as indicating or implying relative importance.
[0050] References to "one embodiment" or "some embodiments" in the present specification mean that a particular feature, structure, or characteristic described in conjunction with that embodiment is included in one or more embodiments of the present invention. Thus, phrases such as "in one embodiment," "in some embodiments," "in other embodiments," and "in yet other embodiments" appearing in various places in this specification do not necessarily refer to the same embodiment, but rather mean "one or more, but not all, embodiments," unless otherwise specifically emphasized. The terms "including," "comprising," "having," and variations thereof mean "including but not limited to," unless otherwise specifically emphasized.
[0051] It should be understood that the order of execution of the steps in the following embodiments does not necessarily mean the order in which they are executed. The order in which each process is executed should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0052] In order to illustrate the technical solution of the present invention, specific embodiments are provided below.
[0053] In related technologies, commonly used memory management solutions include memory pool management and memory heap management.
[0054] 1. Memory pool management method: A fixed-size memory block allocation mechanism is used. During the memory pool initialization phase, the system accurately divides the preset memory area into multiple memory blocks of the same size as the memory pool. During the actual use phase (i.e., the memory allocation phase), when a module has a data storage requirement and requests memory allocation, the system extracts a free memory block from the memory pool and allocates it to the module. Accordingly, when the module releases the memory after use, the system marks the released memory block as free, completing the recycling process. This design ensures that the time consumption of memory allocation and release operations is constant, improving system performance and response speed; however, the memory pool management solution only supports fixed-size memory block allocation. If the size of these small blocks does not match the actual needs well, it may cause memory waste or shortage, memory fragmentation or memory waste, resulting in low memory usage efficiency and lack of flexibility.
[0055] 2. Memory heap management method: The First Fit algorithm is used to achieve efficient memory allocation and recycling. The memory heap management solution does not allocate the system's memory space. During the memory allocation phase, when a module has a data storage requirement and requests memory allocation, the system will search for the first free memory block that can meet the requested memory size in the memory heap formed by memory blocks of different sizes. If the free memory block is larger than the requested memory size, the system will cut out a memory block of the required size from the free memory block and allocate it to the module, and reinsert the remaining memory blocks into the memory heap for subsequent use. When releasing memory, the system will mark the released memory block as a free memory block for subsequent use.
[0056] Compared to the memory pool management method that allocates fixed-size memory in batches, the memory heap management method can dynamically allocate memory blocks of corresponding sizes based on the storage requirements of the module. When system memory resources are limited, it can effectively reduce memory usage and improve memory utilization and allocation flexibility. However, in situations where memory allocation and release operations are frequently performed, systems that use memory heap management may experience severe memory fragmentation and heap memory resource exhaustion. When encountering high-traffic data impact, if the memory heap management method is used to allocate memory resources, due to memory fragmentation and resource exhaustion, it is impossible to find enough memory blocks to meet the memory allocation request, and memory allocation failure occurs. As a result, some modules cannot operate normally due to memory allocation failure, which in turn causes system dysfunction or even crashes, reducing system stability and reliability.
[0057] In response to the above problems, the embodiments of the present application provide a memory management method, apparatus, device, storage medium and program product, which determines the module type of different functional modules in the target system, and the module type is used to indicate whether the functional module is a first-class module that works abnormally after memory allocation fails, so as to execute different memory management strategies for different types of functional modules according to the module type of the functional module. Among them, when the module type of the functional module indicates that the functional module is a first-class module, the functional module is memory managed according to the memory pool management mode, so as to allocate a corresponding preset memory pool for the functional module, and memory allocation is performed on the functional module based on the preset memory pool. When the module type of the functional module indicates that the target module is a second-class module, the functional module is memory managed according to the memory heap management mode, so as to allocate memory to the functional module based on the memory heap.
[0058] For the second type of modules whose work is not affected by memory allocation failure, the memory heap management mode is adopted to perform dynamic memory management on such functional modules, which can reduce the situation of extra memory occupation and improve the utilization rate of memory resources. For the first type of modules that have abnormal work after memory allocation failure, the memory pool management mode is adopted to perform memory management on such functional modules, which can ensure that such modules can successfully apply for memory even when the system is under high load or heap fragmentation is serious, realize real-time and stable memory allocation, reduce the situation of system instability caused by abnormal work due to memory allocation failure, and ensure the stability of system operation. In this embodiment, the hierarchical memory management mode based on different types of functional modules combines the advantages of pre-allocated memory pool and dynamic memory allocation, can meet the memory management needs of different functional modules, not only enhance the overall stability of the system, but also achieve a balance between system performance and memory resource utilization, and improve system reliability.
[0059] The memory management method provided by the embodiment of the present invention can be applied in Figure 1 In the target system shown, the target system includes a memory management device and multiple functional modules (functional module 1 ... functional module n, where n is an integer greater than 1). Each functional module communicates with the memory management device via a network or a bus.
[0060] Specifically, based on the attribute information of each functional module in the target system, multiple functional modules in the target system can be pre-classified into first-category modules and second-category modules. The attribute information of the functional modules is used to determine whether the functional modules will operate abnormally after a memory allocation failure. The first-category modules are modules that operate abnormally after a memory allocation failure, and the second-category modules are modules that operate normally after a memory allocation failure. That is, based on the attribute information of whether each functional module will operate abnormally after a memory allocation failure, multiple functional modules in the target system can be pre-classified into first-category modules and second-category modules, and the module type indicating whether the functional module is a first-category module that operates abnormally after a memory allocation failure is stored for subsequent invocation.
[0061] After the target system is started, the memory management device determines the module types of different functional modules in the target system. The module type is used to indicate whether the functional module is a first-category module that works abnormally after memory allocation fails, so as to execute different memory management strategies for different types of functional modules according to the module type of the functional module. Specifically, when the module type of the functional module indicates that the functional module is a first-category module, the functional module is memory managed according to the memory pool management mode, so that a corresponding preset memory pool is allocated to the functional module, and memory allocation and data storage are performed on the functional module based on the preset memory pool. When the module type of the functional module indicates that the target module is a second-category module, the functional module is memory managed according to the memory heap management mode, so that memory allocation and data storage are performed on the functional module based on the memory heap.
[0062] In the above-mentioned target system, on the one hand, for the second type of modules whose work is not affected by memory allocation failure, the memory heap management mode is adopted to perform dynamic memory management on this type of functional modules, which can reduce the situation of extra memory occupation and improve the utilization rate of memory resources; on the other hand, for the first type of modules that have abnormal work after memory allocation failure, the memory pool management mode is adopted to perform memory management on this type of functional modules, which can ensure that this type of modules can successfully apply for memory even when the system is under high load or heap fragmentation is serious, realize real-time and stable memory allocation, reduce the situation of system instability caused by abnormal work due to memory allocation failure, and ensure the stability of system operation. In this embodiment, the hierarchical memory management mode based on different types of functional modules combines the advantages of pre-allocated memory pool and dynamic memory allocation, can meet the memory management needs of different functional modules, not only strengthen the overall stability of the system, but also achieve a balance between system performance and memory resource utilization, and improve system reliability.
[0063] The memory management device can be a terminal device, including but not limited to various personal computers, laptops, smartphones, tablet computers, and portable wearable devices. The memory management device can also be a server, which can be implemented as an independent server or a server cluster consisting of multiple servers.
[0064] The target system may be a system using a Light Weight Internet Protocol (LWIP) protocol stack, and may be run on a hardware device with an operating system or in an environment without an operating system.
[0065] The target system can be an embedded system based on the LWIP protocol stack, and the memory management device can be an embeddable module within the embedded system. An embedded system is a computer system embedded within another device with specific functions. These embedded systems are typically highly specialized, resource-constrained, and require high real-time performance. For example, the target system can be a mobile phone, vehicle control system, or smart device such as a router, smart TV, or digital camera.
[0066] Taking a router in an embedded system as an example, the memory management device may be a control chip in the router. The first type of module may be a module in the router that malfunctions due to memory allocation failure, such as a timer. The second type of module may be a module in the router that does not malfunction due to memory allocation failure, such as a server module based on the Hypertext Transfer Protocol (Hypertext Transfer Protocol) and an Address Resolution Protocol (ARP) module.
[0067] In various embedded systems, the memory management method provided in the embodiment of the present application performs memory management on different functional modules in the embedded device, which can effectively cope with the two main challenges of limited resources and high real-time requirements in the embedded system. The memory management method provided in the embodiment of the present application adopts a dynamic allocation mechanism to provide different memory management allocation mechanisms for different types of functional modules. For the key first-class modules in the embedded system (such as modules that work abnormally after memory allocation fails), by pre-allocating corresponding preset memory pools for some first-class modules, it can be ensured that the key tasks in the embedded system can obtain sufficient memory resources when needed, thereby ensuring the real-time and stability of the embedded system. At the same time, for ordinary modules (i.e., second-class modules) in the embedded system, a memory heap management mode is adopted to perform dynamic memory management on them, allowing the system to flexibly allocate memory resources during non-critical periods to adapt to the needs of different functional modules.
[0068] The target system may also be an IoT device based on the LWIP protocol stack, such as a smart home device, a medical device, a wearable device (such as a smart watch, an exoskeleton), etc. The memory management device may be a control module in the IoT device.
[0069] IoT devices often face resource constraints, including limitations on processing power, memory, and storage space. These devices require network data transmission to enable remote monitoring and control. However, when IoT devices process large amounts of data, especially when the amount of data processed surges within a short period of time, traditional LWIP memory management solutions can encounter issues such as memory fragmentation. This can prevent critical first-class modules in IoT devices from being allocated sufficient memory resources in a timely manner, leading to operational anomalies.
[0070] By using the dynamic memory allocation mechanism provided in the memory management method provided in the embodiments of this application, by pre-allocating corresponding preset memory pools for key first-category modules in IoT devices, memory blocks of a certain size and quantity can be allocated to these first-category modules when they require memory resources, thus providing stable memory resources for these key first-category modules. Simultaneously, for ordinary modules (i.e., second-category modules) in IoT devices, dynamic memory management is implemented using a memory heap management model, enabling the system to flexibly respond to unexpected or variable-sized data processing needs.
[0071] Taking the smart camera in the smart home device as an example, when the smart camera needs to send video stream data through the network, that is, when the smart camera has memory resource requirements, the memory management method provided in the embodiment of the present application can accurately determine the key modules (i.e., the first type of modules) involved in the data stream transmission task, such as the network transmission module, timer, etc., according to the pre-calibrated module type, and allocate a certain number and size of memory blocks to them based on the pre-allocated preset memory pool, ensuring that the network transmission module, timer and other key modules can still be allocated sufficient memory resources in time when the instantaneous traffic increases, effectively reducing the system crash problem caused by excessive instantaneous traffic and insufficient memory, and improving the stability and reliability of the system.
[0072] Furthermore, the target system can also be other systems with limited memory resources and real-time and reliability requirements. For example, the target system can be an industrial control system. In industrial control systems, real-time and reliable data transmission and execution are crucial. For example, on an automated production line, various sensors and actuators need to exchange data in real time to ensure the smooth progress of the production process. Delays or failures in data exchange can easily lead to system anomalies, crashes, production halts, and even safety incidents.
[0073] By adopting the memory management solution in the embodiment of the present application and executing different memory allocation strategies for different types of functional modules, it can be ensured that stable memory resources can be allocated to the key first-class modules in the industrial control system in a timely manner, avoiding the situation where memory allocation failure causes data exchange delay or failure, ensuring the real-time nature of data exchange, reducing the possibility of system crashes, and effectively taking into account the real-time nature and reliability of the system.
[0074] Taking the automated production system in the industrial control system as an example, during the operation of the automated production system, when various sensors and actuators exchange data, that is, when they have memory resource requirements, the memory management method provided in the embodiment of the present application can accurately determine that it is a key module (that is, a first-class module) in the automated control system based on the pre-calibrated module type, and allocate a certain number and size of memory blocks to it based on the pre-allocated preset memory pool, ensuring that each sensor and actuator can send and receive data packets quickly and stably, so that the data exchange of each device can be completed in a timely and smooth manner, reducing system operation anomalies and crashes, thereby ensuring the real-time and reliability of the automated production system and ensuring the safety of industrial production.
[0075] In addition, this solution also helps to reduce system crashes and data loss caused by improper memory management strategies. For example, it can reduce memory allocation failures caused by memory fragmentation when only the memory heap is used for dynamic memory allocation. It can enable key modules in industrial control systems to successfully apply for memory even when the system is highly loaded or the heap is severely fragmented, reducing system crashes caused by abnormal work due to memory allocation failures, and reducing data loss due to memory allocation failures, thereby improving the overall stability and security of industrial control systems.
[0076] In one embodiment, if Figure 2 As shown, a memory management method is provided, which is applied in Figure 1 The memory management device in FIG. 1 is used as an example to illustrate the process, which includes the following steps:
[0077] S10: Determine module types of different functional modules in the target system, where the module type is used to indicate whether the functional module is a first type of module that works abnormally after memory allocation fails.
[0078] After the target system is started, module types of different functional modules in the target system are determined, wherein the module types are generated in advance based on attribute information of the functional modules and are used to indicate whether the functional modules are first-class modules that work abnormally after memory allocation failure.
[0079] Among them, the memory management device can pre-divide multiple functional modules in the target system into first-category modules that will work abnormally after memory allocation failure and second-category modules that will not work normally after memory allocation failure based on the attribute information of each functional module in the target system pre-configured by the user, that is, based on the attribute information of whether each functional module will work abnormally after memory allocation fails, and store the module type used to identify whether the functional module is a first-category module or a second-category module for subsequent call, which is simple and intuitive. For example, the module type of different functional modules can be updated to the memory configuration information of the corresponding functional module. After the target system is started, the memory management device reads the memory configuration information of the source file to determine the module type of each functional module.
[0080] For example, the first type of module can be a module such as a timer module that works abnormally after memory allocation fails, and the second type of module can be a software or hardware module based on HTTP, such as an HTTP server module, and a module such as an address resolution protocol (ARP) module that does not work abnormally after memory allocation fails.
[0081] S20: When the module type of the functional module indicates that the functional module is a first-category module, memory management is performed on the functional module according to the memory pool management mode, so as to allocate a corresponding preset memory pool to the functional module, and memory allocation is performed on the functional module based on the preset memory pool.
[0082] The memory management of the first type of module includes three types of operations: memory pool initialization, memory allocation, and memory release (recycling). When the module type of the functional module indicates that the functional module is a first type of module, the memory management device performs memory management on the functional module according to the memory pool management mode, including the following steps:
[0083] During the memory pool initialization phase, the memory management device divides a fixed memory area from the total memory space of the target system and allocates it as the preset memory area for the functional module (it may also be allocated as a preset memory area shared by all first-category modules). Then, the memory management device divides the preset memory area into multiple memory blocks of the same size as the preset memory pool for the functional module, and organizes the memory blocks in the preset memory pool in the form of a one-way linked list to obtain a linked list of the preset memory pool.
[0084] After obtaining the preset memory pool, when the functional module (i.e., the first type of module) has a data storage requirement to request memory allocation, that is, in the memory allocation stage, the memory management device extracts an idle memory block from the head of the linked list of the preset memory pool and allocates it to the functional module for use, so as to store the data to be stored of the functional module in the idle memory block.
[0085] When the memory block where the functional module stores data needs to be released, that is, in the memory release phase, the memory management device releases the memory block and adds the released memory block back to the linked list of the preset memory pool to complete the memory release and recovery process for subsequent reallocation.
[0086] S30: When the module type of the functional module indicates that the target module is a second-category module, memory management is performed on the functional module according to the memory heap management mode, so as to perform memory allocation based on the memory heap on the functional module.
[0087] The memory management of the second type of module includes three types of operations: memory allocation and memory release. When the module type of the functional module indicates that the functional module is a second type of module, the memory management device performs memory management on the functional module according to the memory heap management mode, including the following steps:
[0088] When the functional module (i.e., the second type of module) has a data storage requirement and requests memory allocation, that is, in the memory allocation stage, the memory management device searches for the first free memory block that can meet the requested memory size in the remaining memory area of the target system except the preset memory area of the first type of module, and when the free memory block is larger than the size of the memory requested by the functional module, cuts out a memory block of the required size from the free memory block and allocates it to the functional module for data storage, and then reinserts the remaining memory blocks into the memory heap for subsequent use.
[0089] When the memory block where the functional module stores data needs to be released, that is, in the memory release phase, the memory management device releases the memory block, marks the released memory block as a free memory block, and merges other adjacent free memory blocks to form a larger free block, completing the memory release and recovery process for subsequent reallocation.
[0090] In this embodiment, for the second type of modules whose work is not affected by memory allocation failure, a memory heap management mode is used to dynamically manage the memory of this type of functional modules, which can reduce the situation of extra memory occupation and improve the utilization rate of memory resources. For the first type of modules that have abnormal work after memory allocation failure, a memory pool management mode is used to manage the memory of this type of functional modules, which can ensure that this type of modules can successfully apply for memory even when the system is under high load or heap fragmentation is serious, realize real-time and stable memory allocation, reduce the situation of system instability caused by abnormal work due to memory allocation failure, and ensure the stability of system operation. The target system in this embodiment is based on a hierarchical memory management mode for different types of functional modules, combining the advantages of pre-allocated memory pool and dynamic memory allocation, and can meet the memory management needs of different functional modules. It not only strengthens the overall stability of the system, but also achieves a balance between system performance and memory resource utilization, and improves system reliability.
[0091] In one embodiment, the memory management device can update the module types of different functional modules to the memory configuration information of the corresponding functional modules, and generate a source file for memory initialization based on the memory configuration information of each functional module. After the target system is started, the memory management device automatically executes the source file for memory initialization to read the memory configuration information of the source file to determine the module type of each functional module, and then implements the memory initialization strategy for different types of functional modules. Among them, for the first type of modules, the memory management device initializes the memory pool for the first type of modules to allocate a corresponding preset memory pool for the first type of modules, so as to facilitate the subsequent memory allocation and release operations based on the preset memory pool for the first type of modules. For the second type of modules, no actual memory pool is allocated for the second type of modules. The memory management device allocates and releases memory for the second type of modules based on the memory heap mode based on the remaining memory space in the target system. By recording the memory configuration information in the source file, different modules are classified and initialized separately, which can be automatically executed after the target system is started, which is simple and convenient. Among them, the process of memory pool initialization, memory allocation and release operations is described above and will not be repeated here.
[0092] In one embodiment, if Figure 3 As shown, before step S10, that is, before determining the module types of different functional modules in the target system, the method further includes the following steps:
[0093] S01: After the target system is started, a memory descriptor template of each functional module is generated according to the memory configuration information of the functional module.
[0094] After the target system is booted, the memory management device obtains memory configuration information for each functional module in the target system. This memory configuration information may be pre-stored at a designated storage address. The memory configuration information includes the module type and memory description information, such as a pre-defined memory name, memory pool size or memory heap size, memory block size and number, and memory statistics. The memory management device then performs different descriptor generation operations for different types of functional modules based on the memory configuration information to generate memory descriptor templates for each functional module.
[0095] The memory descriptor template is a data structure used to describe the memory description information of a functional module. The parameters contained in the memory descriptor templates of different types of functional modules are different. The memory descriptor template can be a memp_desc structure.
[0096] For example, the parameters in the memory descriptor template for the first type of module include the memory name, memory pool size, memory block size and number, starting address, memory statistics, and a memory pool pointer variable used to point to the preset memory pool actually allocated to the first type of module. The parameters in the memory descriptor template for the second type of module include the memory name, memory heap size, memory statistics, and other parameters. Because the second type of module uses a memory heap model for memory management, there is no need to allocate actual memory space and specify a starting address for it. Therefore, the memory descriptor template does not include the aforementioned memory pool pointer variable and starting address.
[0097] S02: Determine the module types of different functional modules in the target system according to the memory descriptor templates of the functional modules, and execute different memory management strategies for different types of functional modules.
[0098] After generating the memory descriptor templates of each functional module, the memory management device can determine the module types of different functional modules in the target system according to the memory descriptor templates of each functional module, and implement different memory management strategies for different types of functional modules.
[0099] For example, the memory descriptor templates of each functional module can be filled into an array to obtain the global descriptor array of the target system. Then, based on the memory descriptor templates of each functional module in the global descriptor array, the module types of different functional modules in the target system can be determined, and different memory management strategies can be implemented for different types of functional modules. In the memory initialization phase, the memory initialization interface is called to traverse all functional modules in the global descriptor array. For each functional module, the memory descriptor template of the functional module in the global descriptor array is read to determine the module type of the functional module to determine whether a memory pool initialization operation needs to be performed on the functional module. The memory descriptor templates of all functional modules are centrally stored in the global descriptor array. The memory information of the functional modules can be quickly found through the global descriptor array, thereby realizing unified access management of the memory configuration and status of each functional module.
[0100] Among them, when it is determined that the module type of the functional module is a second-category module, the memory initialization interface exits, the memory pool of the functional module is not initialized, and the memory descriptor template of the functional module is directly used as the memory descriptor of the functional module. When it is determined that the module type of the functional module is a first-category module, the memory initialization interface initializes the memory pool of the functional module. That is, the memory initialization interface is called to allocate a fixed preset memory space (which can be specified by the parameters of the memory descriptor template) to the functional module according to the parameters in the memory descriptor template of the functional module, and divide it into multiple memory blocks of the same size, and organize multiple memory blocks in the form of a linked list to form a preset memory pool of the functional module. Among them, the size and number of memory blocks in the preset memory pool are determined according to the memory size and number in the memory descriptor template. Then, based on the base address of the preset memory pool (i.e., the first address) and the first free memory block in the preset memory pool, a memory pool pointer pointing to the preset memory pool is generated, and the memory pool pointer is updated to the memory descriptor template of the functional module to generate the memory descriptor of the functional module, so that different memory allocation and release operations can be performed on different types of functional modules based on the memory descriptors of each functional module in the global descriptor array.
[0101] Memory descriptors enable memory management between functional modules to have a structured and standardized data interface, which is conducive to the addition, deletion or replacement of modules. When upgrading the system or expanding new functional modules, only new template types and their strategies need to be defined, and the original structure does not need to be significantly changed, which is conducive to system maintenance and expansion.
[0102] In this embodiment, after the target system is started, a memory descriptor template is generated for each functional module based on the functional module's memory configuration information. The memory descriptor template is a data structure used to describe the functional module's memory description information. The parameters contained in the memory descriptor templates vary for different types of functional modules. Based on the memory descriptor templates for each functional module, the module type of each functional module in the target system is determined, and different memory management policies are implemented for each type of functional module. By defining different parameter information for different types of functional modules using the memory descriptor templates and generating memory descriptors with different information based on the memory descriptors for memory management, the flexibility and convenience of memory management can be improved, thereby enhancing system reliability.
[0103] In one embodiment, if Figure 4 As shown, in step S01, that is, after the target system is started, a memory descriptor template of each functional module is generated according to the memory configuration information of the functional module, which specifically includes the following steps:
[0104] S011: After the target system starts, read the header file and enable macros in the memory management source file.
[0105] The memory management source file is a preconfigured source file called memp.c, which is used to manage the functions of each functional module. Memory management for each functional module, including memory initialization, memory allocation, and memory release, can be implemented by executing the source file memp.c. The header file memp_std.h for this memory management source file contains the memory configuration information for each functional module in the target system. This header file memp_std.h is used to generate memory descriptor templates for each functional module. That is, executing this header file memp_std.h automatically generates memory descriptor templates for each functional module.
[0106] After the target system is started, the pre-configured memory management source file is obtained, and the header file and the enabling macro in the memory management source file are read. The enabling macro is used to manage whether each functional module is enabled.
[0107] S012: Based on the macro enablement, the header file is executed in a macro expansion manner to perform different descriptor generation operations on multiple functional modules of different types according to the module type in the memory configuration information of each functional module to generate a memory descriptor template for each functional module.
[0108] Based on the macro enablement, the header file is executed in a macro expansion manner to perform different descriptor generation operations on multiple functional modules of different types based on the module type and memory description information in the memory configuration information of each functional module, thereby generating a memory descriptor template for each functional module. Compared with the memory descriptor of the second type of module, the memory descriptor of the first type of module additionally includes a pointer variable for pointing to the memory pool actually allocated for the first type of module, so that the preset memory pool is allocated to the first type of module based on the memory pool pointer variable in the memory descriptor, and memory allocation based on the preset memory pool is performed on the first type of module.
[0109] Specifically, the process of generating the memory descriptor template of each functional module includes the following steps:
[0110] S0121: Turn off all first-class modules in the target system by enabling macros to stop declaring first-class modules, and enable all second-class modules to declare second-class modules.
[0111] Specifically, after determining whether a functional module is a first-category module based on the module type in the functional module's memory configuration information, the memory management device resets (i.e., sets the enable macro to 0) for all first-category modules in the target system, disables the first-category modules to stop declaring the first-category modules, and sets (i.e., sets the enable macro to 1) for all second-category modules in the target system, enabling the second-category modules to declare the second-category modules. Since there are a large number of second-category modules, disabling first-category modules can reduce the number of subsequent module settings and the amount of data processing.
[0112] In other embodiments, all second-category modules in the target system may be enabled and disabled by enabling a macro, so as to distinguish between first-category modules and second-category modules. That is, the enable macros of all first-category modules in the target system are set (i.e., set to 1), enabling the first-category modules to declare the first-category modules, and the enable macros of all second-category modules in the target system are reset (i.e., set to 0), disabling the second-category modules to stop declaring the second-category modules.
[0113] S0122: Execute the header file memp_std.h to perform the following process:
[0114] For the second type of module declared, a memp_desc structure excluding the memory pool pointer variable is generated according to the memory configuration information of the functional module, and a memory descriptor template of the functional module is obtained;
[0115] S0222: For the undeclared first-category module, generate a memp_desc structure including a memory pool pointer variable according to the memory configuration information of the functional module, and obtain a memory descriptor template of the functional module.
[0116] In this embodiment, after the target system is started, the header file and enable macro in the memory management source file are read. The header file of the memory management source file contains the memory configuration information of each functional module in the target system. Based on the enable macro, the header file is executed using macro expansion to perform different descriptor generation operations for multiple functional modules of different types based on the module type in the memory configuration information of each functional module, thereby generating memory descriptor templates for each functional module. Through the header file + preset macro + macro expansion mechanism, this solution realizes the automated and structured generation of memory descriptor templates for functional modules, effectively improving system development efficiency, configuration flexibility, and initialization performance.
[0117] In one embodiment, if Figure 5 As shown, in step S10, the module types of different functional modules in the target system are determined, which specifically includes the following steps:
[0118] S11: Obtain preset fields pre-defined for the first type of module.
[0119] After the target system is started, the memory management device executes the header file in the memory management source file to generate a memory descriptor template for each functional module. The memory descriptor is generated based on the memory configuration information of the functional module, and the parameters contained in the memory descriptor of different types of functional modules are different.
[0120] After obtaining the memory descriptor templates for each functional module, the memory management device retrieves a preset field predefined for the first-category module. This preset field can be any predefined field that distinguishes the first-category module from the second-category module, such as the memory pool name in the memory descriptor template (memory names differ for different functional modules) or the memory pool pointer variable in the memory descriptor template. This type of preset field can be pre-stored at a specified address for subsequent call.
[0121] S12: Determine the module types of different functional modules in the target system according to the matching conditions between the parameter fields in the memory descriptor templates of the functional modules and the preset fields.
[0122] After obtaining the preset fields pre-defined for the first type of modules and the memory descriptor template of each functional module, the parameter fields in the memory descriptor template of each functional module can be matched with the preset fields, and the module types of different functional modules in the target system can be determined based on the matching between the parameter fields in the memory descriptor template of each functional module and the preset fields.
[0123] Specifically, taking the preset field as the memory pool name as an example, for each functional module, the memory management device reads the memory name field in the memory descriptor template of the functional module, and determines whether the memory name field in the memory descriptor template matches the memory pool name of the first category module; when the memory name field in the memory descriptor template matches the memory pool name of the first category module, the module type of the functional module is determined to be a first category module; when the memory name field in the memory descriptor template does not match the memory pool name of the first category module, the module type of the functional module is determined to be a second category module.
[0124] In this embodiment, preset fields pre-defined for the first type of modules are obtained, and the module types of different functional modules in the target system are determined based on the matching between the parameter fields in the memory descriptor template of each functional module and the preset fields; the module type is automatically identified through field matching, and there is no need to explicitly declare the module type in the configuration, which reduces the configuration complexity and realizes intelligent and automated module classification, thereby improving the accuracy of the memory management strategy, the system's adaptability, scalability and maintenance efficiency.
[0125] In one embodiment, if Figure 6 As shown, in step S20, that is, when the module type of the functional module indicates that the functional module is a first-category module, memory management is performed on the functional module according to the memory pool management mode, so as to allocate a corresponding preset memory pool to the functional module, and memory allocation is performed on the functional module based on the preset memory pool, specifically including the following steps:
[0126] S21: When the type of the functional module indicates that the functional module is a first-category module, a memory pool initialization operation is performed on the functional module to allocate a preset memory space for the functional module, and the allocated preset memory space is divided into multiple fixed-size memory blocks to obtain a preset memory pool corresponding to the functional module.
[0127] When the type of the functional module indicates that the functional module is a first-class module, the memory management device calls the memory initialization interface to perform an initialization operation on the memory pool of the functional module to allocate a preset memory space for the functional module, and divides the allocated preset memory space into multiple fixed-size memory blocks to obtain a preset memory pool corresponding to the functional module.
[0128] Among them, when the type of the functional module indicates that the functional module is a first-class module, the memory management device obtains a pre-generated global descriptor array of the target system, which includes the memory descriptor templates of each functional module; then, the memory management device calls the memory initialization interface, traverses all the memory descriptor templates in the global descriptor array, and determines whether the functional module is a first-class module according to the memory descriptor template, so as to initialize the memory pool for the first-class module. When it is determined that the corresponding functional module is a second-class module according to the memory descriptor template, the memory initialization interface is exited, and there is no need to call the memory initialization interface to perform the memory pool initialization operation on the functional module, and the memory descriptor template of the functional module is directly used as the memory descriptor of the functional module. When it is determined that the corresponding functional module is a first-class module according to the memory descriptor template, the memory initialization interface is called to perform the memory pool initialization operation on the functional module, and a preset memory pool is allocated to the functional module. The memory pool initialization process of the first-class module is described above and will not be repeated here.
[0129] S22: Generate a memory descriptor of the functional module according to the structure information of the preset memory pool.
[0130] When the type of the functional module indicates that the functional module is a first-category module, after initializing the memory pool for the functional module to allocate a preset memory pool corresponding to the functional module, the memory management device generates a memory pool pointer for executing the preset memory pool based on the structural information of the preset memory pool, and updates the memory pool pointer to the memory descriptor template of the functional module in the global descriptor array to obtain a memory descriptor for the functional module. The memory pool pointer can be at least one of a base address pointer pointing to the preset memory pool and a memory block pointer pointing to the first free memory block.
[0131] S23: When the functional module has storage requirements, allocate an idle target memory block to the functional module in the preset memory pool according to the memory descriptor of the functional module, and store the data to be stored of the functional module in the target memory block.
[0132] After obtaining the updated global descriptor array of memory descriptor template information, that is, after obtaining the memory descriptors of each functional module, if a memory request from a functional module is received, that is, when it is determined that the functional module has storage requirements, the memory management device determines whether it is a first-class module based on the memory descriptor of the functional module.
[0133] When determining that the functional module is a first-category module, the memory management device determines a preset memory pool pre-allocated for the functional module based on the memory pool pointer in the memory descriptor of the functional module, and allocates an idle target memory block to the functional module in the preset memory pool, so as to store the data to be stored of the functional module in the target memory block. The target memory block may be one or more consecutive idle memory blocks.
[0134] When it is determined that the functional module is a second-category module, the memory management device performs memory allocation based on the memory heap for the functional module according to the memory heap information (such as the memory heap name or pointer) in the memory descriptor of the functional module, that is, finds a free memory block in the memory heap that meets the memory size applied for, processes its memory size so that the memory block size is the same as the applied memory size, obtains the target memory block and allocates it to the functional module to store the data to be stored of the functional module.
[0135] In this embodiment, when the type of the functional module indicates that the functional module is a first-class module, the memory pool of the functional module is initialized to allocate a preset memory space for the functional module, and the allocated preset memory space is divided into multiple fixed-size memory blocks to obtain a preset memory pool corresponding to the functional module; a memory descriptor of the functional module is generated based on the structural information of the preset memory pool; when the functional module has storage requirements, an idle target memory block is allocated to the functional module in the preset memory pool based on the memory descriptor of the functional module, and the data to be stored of the functional module is stored in the target memory block. Each first-class module can have an independent preset memory pool, avoiding problems such as application failure or memory leaks during operation, thereby improving the real-time performance, reliability, and maintainability of the system; the structural information of each module's memory pool (such as the starting address, block size, total number of blocks, free block list, etc.) is recorded through the memory descriptor, forming a standardized memory access interface, which is conducive to the unified scheduling, debugging, and monitoring of memory status of the system, and enhancing the manageability of the system.
[0136] In one embodiment, step S22, i.e., generating a memory descriptor of the functional module according to the structure information of the preset memory pool, specifically includes the following steps:
[0137] S221: Determine the base address of the preset memory pool and the first free memory block in the preset memory pool.
[0138] When the type of the functional module indicates that the functional module is a first type module, after initializing the memory pool for the functional module to allocate a preset memory pool corresponding to the functional module, the memory management device determines the base address of the preset memory pool and the first free memory block in the preset memory pool.
[0139] S222: Generate a base address pointer pointing to the preset memory pool and a memory block pointer pointing to the first free memory block according to the base address of the preset memory pool and the first free memory block, and obtain a memory descriptor of the functional module.
[0140] The memory management device generates a base address pointer pointing to the preset memory pool based on the base address of the preset memory pool, and generates a memory block pointer pointing to the first free memory block based on the position of the first free memory block in the preset memory pool, and obtains the pointer information of the memory descriptor of the functional module.
[0141] Specifically, the memory management device updates the corresponding pointer variable in the memory descriptor template of the functional module according to the base address of the preset memory pool and the position of the first free memory block, that is, updates the base address of the preset memory pool to the first address pointer field for pointing to the preset memory pool in the memory descriptor template of the functional module, generates a base address pointer pointing to the preset memory pool, and updates the position of the first free memory block (such as the position in the linked list composed of multiple memory blocks) to the pointer field for pointing to the first free memory block in the preset memory pool in the memory descriptor template, generates a memory block pointer pointing to the first free memory block, and thus updates the memory descriptor of the functional module.
[0142] In this embodiment, the base address of the preset memory pool and the first free memory block in the preset memory pool are determined; based on the base address of the preset memory pool and the first free memory block, a base address pointer pointing to the preset memory pool and a memory block pointer pointing to the first free memory block are generated to obtain a memory descriptor of the functional module; the base address of the preset memory pool and the address of the first free block are encapsulated as pointers and stored in the memory descriptor, so that the functional module can access and manage its private memory resources in a structured and unified manner, thereby improving memory access efficiency and improving the module's resource scheduling capabilities.
[0143] In one embodiment, in step S23, i.e., when a functional module has a storage requirement, a free target memory block is allocated to the functional module in a preset memory pool according to the memory descriptor of the functional module, specifically comprising the following steps:
[0144] S231: When a functional module has storage requirements, a memory descriptor of the functional module is obtained.
[0145] When the type of the functional module indicates that the functional module is a first-category module, after initializing the memory pool for the functional module to allocate a preset memory pool and generating a memory descriptor for the functional module, the memory management device obtains the memory descriptor for the functional module when the functional module has a storage requirement. For example, upon receiving a memory request from the functional module and determining that the functional module has a storage requirement, the memory management device reads the memory descriptor for the functional module from the global descriptor array to quickly obtain the memory descriptor for the functional module.
[0146] S232: Determine the first free memory block in the preset memory pool according to the base address pointer and the memory block pointer in the memory descriptor to obtain the target memory block.
[0147] The memory management device determines the preset memory pool pointed to by the base address pointer in the memory descriptor, and determines the memory block pointed to by the memory block pointer in the memory descriptor, and can obtain the first free memory block in the preset memory pool, use the first free memory block as the target memory block, or use the first free memory block and the subsequent consecutive free memory blocks as the target memory blocks, and store the data to be stored of the functional module in the target memory block.
[0148] In this embodiment, when a functional module has a storage requirement, a memory descriptor of the functional module is obtained. The memory descriptors of different types of functional modules contain different information. The memory descriptor of the first type of module additionally contains a base address pointer pointing to the preset memory pool actually allocated to the first type of module, and a memory block pointer pointing to the first free memory block in the preset memory pool. Based on the base address pointer and memory block pointer in the memory descriptor, the first free memory block in the preset memory pool is determined to obtain the target memory block. By directly storing the base address pointer of the preset memory pool and the pointer to the first free memory block in the memory descriptor of the first type of module, when a functional module has a storage requirement, it is possible to quickly locate available memory blocks without searching, thereby achieving timely and accurate memory allocation operations, improving the real-time performance and response speed of memory allocation, and reducing the possibility of memory allocation failure in the first type of module, thereby improving system operation stability and reliability.
[0149] In one embodiment, the target system can support memory management modes including a single memory heap management mode, a single memory pool management mode, and a hybrid management mode. The hybrid management mode is a hierarchical memory management mode based on a memory heap and a memory pool provided in this embodiment. The target system's memory management source file is defined by a first preset macro
[0150] The values of MEMP_MEM_MALLOC and the second preset macro MEM_USE_POOLS are used to adjust the memory management mode executed after the target system is started next time. The first preset macro MEMP_MEM_MALLOC defines whether the memory heap management mode is used to implement memory management, that is, when the functional module applies for memory, it is allocated from the memory block in the memory heap. The default value of the first preset macro MEMP_MEM_MALLOC is 0, indicating that the memory heap management mode is not used. The second preset macro MEM_USE_POOLS defines whether the memory pool management mode is used to implement memory management, that is, when the functional module applies for memory, it is allocated from the memory block in the memory pool. The default value of the second preset macro MEM_USE_POOLS is 0, indicating that the memory pool management mode is not used.
[0151] After the target system is started, the memory management device reads the first preset macro and the second preset macro in the memory management source file of the target system, and determines the memory management mode executed after the target system is started this time according to the values of the first preset macro and the second preset macro. Figure 7 As shown, when the first preset macro MEMP_MEM_MALLOC and the second preset macro MEM_USE_POOLS are both 0, it means that a hybrid management mode is adopted to perform memory management on each functional module in the target system, that is, the memory management device determines to adopt the memory management method provided in this embodiment to perform memory management on each functional module in the target system to meet the needs of different functional modules and improve the stability and reliability of the system while ensuring the utilization rate of memory resources. Figure 7 As shown, when the first preset macro MEMP_MEM_MALLOC_ is 1 and the second preset macro MEM_USE_POOLS is 0, the memory management device uses the memory heap management mode to manage the memory of each functional module in the target system. When the first preset macro MEMP_MEM_MALLOC_ is 0 and the second preset macro MEM_USE_POOLS is 1, the memory management device uses the memory pool management mode to manage the memory of each functional module in the target system.
[0152] Among them, in the memory initialization stage, the first preset macro and the second preset macro in the memory management source file are read to execute different memory management processes according to the values of the first preset macro and the second preset macro MEM_USE_POOLS. Among them, when the first preset macro MEMP_MEM_MALLOC_ is 1 and the second preset macro is 0, the memory initialization process is directly terminated without the need to perform memory initialization operations. When the first preset macro MEMP_MEM_MALLOC_ is 0 and the second preset macro MEM_USE_POOLS is 1, the memory initialization interface is called to perform memory pool initialization on the total memory space of the target system to divide the total memory space into multiple memory blocks of preset sizes to form a total memory pool, and based on the total memory pool, memory allocation and release of the memory pool are performed on all functional modules in the target system.
[0153] When the first preset macro MEMP_MEM_MALLOC_ is 0 and the second preset macro MEM_USE_POOLS is 0, the memory initialization interface is called to traverse all the functional modules in the global descriptor array. For each functional module, the memory descriptor template of the functional module in the global descriptor array is read to determine the module type of the functional module to determine whether it is necessary to perform a memory pool initialization operation on the functional module. When it is determined that the module type of the functional module is a second-class module, the memory initialization interface exits, does not initialize the memory pool for the functional module, and directly uses the memory descriptor template of the functional module as the memory descriptor of the functional module. When it is determined that the module type of the functional module is a first-class module, the memory initialization interface initializes the memory pool for the functional module.
[0154] In this embodiment, the target system can support three memory management modes, and define the memory management mode of the target system through the values of two preset macros. The target system can be triggered to execute different memory management modes by modifying the values of the preset macros, which can adapt to the memory management needs of different users and improve the flexibility and adaptability of the target system's memory management.
[0155] It should be understood that the size of the serial numbers of the steps in the above embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0156] In one embodiment, a memory management device is provided, which corresponds one-to-one to the memory management method in the above embodiment. Figure 8 As shown, the memory management device includes a determination module 801, a first management module 802, and a second management module 803. The functional modules are described in detail as follows:
[0157] Determination module 801 is used to determine the module type of different functional modules in the target system, where the module type is used to indicate whether the functional module is a first type module that works abnormally after memory allocation fails;
[0158] a first management module 802 configured to, when the module type of the functional module indicates that the functional module is a first-category module, perform memory management on the functional module according to the memory pool management mode, allocate a corresponding preset memory pool to the functional module, and perform memory allocation on the functional module based on the preset memory pool;
[0159] The second management module 803 is configured to manage the memory of the functional module according to the memory heap management mode when the module type of the functional module indicates that the target module is a second type module, so as to allocate memory to the functional module based on the memory heap.
[0160] In one embodiment, the first management module 802 is specifically configured to:
[0161] When the type of the functional module indicates that the functional module is a first-category module, performing a memory pool initialization operation on the functional module to allocate a preset memory space for the functional module, and dividing the allocated preset memory space into a plurality of fixed-size memory blocks to obtain a preset memory pool corresponding to the functional module;
[0162] Generate memory descriptors of functional modules based on the structure information of the preset memory pool;
[0163] When a functional module has storage requirements, an idle target memory block is allocated to the functional module in a preset memory pool according to the memory descriptor of the functional module, and the data to be stored of the functional module is stored in the target memory block.
[0164] In one embodiment, the first management module 802 is further configured to:
[0165] Determine the base address of the preset memory pool and the first free memory block in the preset memory pool;
[0166] According to the base address of the preset memory pool and the first free memory block, a base address pointer pointing to the preset memory pool and a memory block pointer pointing to the first free memory block are generated to obtain a memory descriptor of the functional module.
[0167] In one embodiment, the first management module 802 is further specifically used to: obtain the memory descriptor of the functional module when there is a storage requirement for the functional module. The information contained in the memory descriptors of different types of functional modules is different. The memory descriptor of the first type of module additionally includes a base address pointer pointing to the preset memory pool actually allocated to the first type of module, and a memory block pointer pointing to the first free memory block in the preset memory pool; according to the base address pointer and memory block pointer in the memory descriptor, determine the first free memory block in the preset memory pool to obtain the target memory block.
[0168] In one embodiment, the memory management device further includes a generation module, the generation module being configured to generate a memory descriptor template for each functional module based on the memory configuration information of the functional module before determining the module type of each functional module in the target system after the target system is started. The memory descriptor template is a data structure for describing the memory description information of the functional module. The memory descriptor templates for different types of functional modules contain different parameters.
[0169] The first management module 802 and the second management module 803 are used to determine the module types of different functional modules in the target system according to the memory descriptor templates of the functional modules, and to execute different memory management strategies.
[0170] In one embodiment, the determination module 801 is specifically used to: obtain preset fields pre-defined for the first type of modules; and determine the module types of different functional modules in the target system based on the matching between the parameter fields in the memory descriptor template of each functional module and the preset fields.
[0171] In one embodiment, the preset field includes a memory pool name, and the determination module 801 is specifically used to: for each functional module, read the memory name field in the memory descriptor template of the functional module; when the memory name field in the memory descriptor template matches the memory pool name of the first category module, determine that the module type of the functional module is a first category module; when the memory name field in the memory descriptor template does not match the memory pool name of the first category module, determine that the module type of the functional module is a second category module.
[0172] In one embodiment, the generation module is specifically used to: after the target system is started, obtain and read the header file and enable macro in the memory management source file, the header file of the memory management source file contains the memory configuration information of each functional module in the target system; based on the enable macro, execute the header file in a macro expansion manner, so as to perform different descriptor generation operations on multiple functional modules of different types according to the module type in the memory configuration information of each functional module, so as to generate a memory descriptor template for each functional module; wherein, compared with the memory descriptor of the second type of module, the memory descriptor of the first type of module additionally includes a pointer variable for pointing to the memory pool actually allocated for the first type of module, so as to allocate the preset memory pool to the first type of module based on the memory pool pointer variable in the memory descriptor, and perform memory allocation based on the preset memory pool on the first type of module.
[0173] It should be noted that the information interaction, execution process, etc. between the above-mentioned devices / units are based on the same concept as the method embodiment of this application. Their specific functions and technical effects can be found in the method embodiment section and will not be repeated here.
[0174] Those skilled in the art can clearly understand that, for the convenience and brevity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In actual applications, the above-mentioned functions can be distributed and completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated into one processing unit, or each unit can exist physically alone, or two or more units can be integrated into one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, which will not be repeated here.
[0175] The present application also provides an electronic device, such as Figure 9 As shown, the electronic device 9 includes: at least one processor 91, a memory 92, and a computer program 93 stored in the memory 92 and capable of running on at least one processor. When the processor 91 executes the computer program 93, the steps in any of the above-mentioned method embodiments are implemented, or when the processor 91 executes the computer program 93, the functions of each module / unit in the above-mentioned device embodiments are implemented.
[0176] For example, the computer program 93 may be divided into one or more modules / units, one or more of which are stored in the memory 92 and executed by the processor 91 to implement the present application. The one or more modules / units may be a series of computer program instruction segments capable of implementing specific functions, and the instruction segments are used to describe the execution process of the computer program 93 in the electronic device 8.
[0177] Those skilled in the art will understand that Figure 8 These are merely examples of electronic devices and do not constitute a limitation of the electronic device. The electronic device may include more or fewer components than shown in the figure, or a combination of certain components, or different components. For example, the electronic device may also include input and output devices, network access devices, buses, etc.
[0178] The processor described above may be a central processing unit, or other general-purpose processor, digital signal processor, application-specific integrated circuit, field programmable gate array or other programmable logic device, discrete gate or transistor logic device, discrete hardware component, etc. The general-purpose processor may be a microprocessor, or the processor may be any conventional processor, etc. The memory may be an internal storage unit of an electronic device, such as a hard disk or memory of the electronic device. The memory may also be an external storage device of the electronic device, such as a plug-in hard disk equipped in the electronic device, a smart memory card, a secure digital card, a flash memory card, etc. Furthermore, the memory may include both the internal storage unit of the electronic device and an external storage device.
[0179] An embodiment of the present application further provides a readable storage medium, which stores a computer program. When the computer program is executed by a processor, the steps in the above-mentioned method embodiments can be implemented.
[0180] An embodiment of the present application provides a computer program product. When the computer program product is run on an electronic device, the electronic device can implement the steps in the above-mentioned method embodiments when executing the computer program product.
[0181] If the integrated unit is implemented as a software functional unit and sold or used as a standalone product, it can be stored in a computer-readable storage medium. Based on this understanding, the present application can implement all or part of the process steps in the above-mentioned method embodiments by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium. When executed by a processor, the computer program can implement the steps of each of the above-mentioned method embodiments. The computer program includes computer program code, which can be in source code form, object code form, executable file, or some intermediate form. Computer-readable media can include at least: any entity or device capable of carrying computer program code to a camera / terminal device, recording medium, computer memory, read-only memory, random access memory, electric carrier signal, telecommunication signal, and software distribution medium. Examples include USB flash drives, removable hard drives, magnetic disks, or optical disks. In some jurisdictions, based on legislation and patent practice, computer-readable media cannot be electric carrier signals or telecommunication signals.
[0182] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, please refer to the relevant description of other embodiments. Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented with electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0183] In the embodiments provided in the present application, it should be understood that the disclosed apparatus / equipment and method can be implemented in other ways. For example, the apparatus / equipment embodiments described above are merely schematic. For example, the division of functional modules or units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of the apparatus or unit, which can be electrical, mechanical or other forms. The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the scheme of this embodiment.
[0184] The above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the various embodiments of the present application, and should all be included in the scope of protection of the present application.
Claims
1. A memory management method, characterized in that: include: Determining module types of different functional modules in the target system, wherein the module types are used to indicate whether the functional modules are first-class modules that work abnormally after a memory allocation failure; When the module type of the functional module indicates that the functional module is a first-category module, memory management is performed on the functional module according to a memory pool management mode, so as to allocate a corresponding preset memory pool to the functional module, and memory allocation is performed on the functional module based on the preset memory pool; When the module type of the functional module indicates that the target module is a second-category module, memory management is performed on the functional module according to a memory heap management mode, so as to perform memory allocation based on the memory heap on the functional module.
2. The memory management method according to claim 1, wherein: When the module type of the functional module indicates that the functional module is a first type module, performing memory management on the functional module according to the memory pool management mode to allocate a corresponding preset memory pool to the functional module, and performing memory allocation on the functional module based on the preset memory pool, including: When the type of the functional module indicates that the functional module is a first-type module, performing a memory pool initialization operation on the functional module to allocate a preset memory space for the functional module, and dividing the allocated preset memory space into a plurality of memory blocks of fixed sizes to obtain a preset memory pool corresponding to the functional module; Generate a memory descriptor of the functional module according to the structural information of the preset memory pool; When the functional module has storage requirements, an idle target memory block is allocated to the functional module in the preset memory pool according to the memory descriptor of the functional module, and the data to be stored of the functional module is stored in the target memory block.
3. The memory management method according to claim 2, wherein: Generating the memory descriptor of the functional module according to the structural information of the preset memory pool includes: Determining a base address of the preset memory pool and a first free memory block in the preset memory pool; According to the base address of the preset memory pool and the first free memory block, a base address pointer pointing to the preset memory pool and a memory block pointer pointing to the first free memory block are generated to obtain a memory descriptor of the functional module.
4. The memory management method according to claim 2, wherein: When the functional module has a storage demand, allocating an idle target memory block to the functional module in the preset memory pool according to the memory descriptor of the functional module includes: When the functional module has a storage demand, obtaining a memory descriptor of the functional module, wherein the memory descriptors of different types of functional modules contain different information, and the memory descriptor of the first type of module additionally contains a base address pointer pointing to a preset memory pool actually allocated to the first type of module, and a memory block pointer pointing to the first free memory block in the preset memory pool; According to the base address pointer and the memory block pointer in the memory descriptor, the first free memory block in the preset memory pool is determined to obtain the target memory block.
5. The memory management method according to any one of claims 1 to 4, wherein: Before determining the module types of different functional modules in the target system, the method further includes: After the target system is started, a memory descriptor template of each functional module is generated according to the memory configuration information of the functional module, wherein the memory descriptor template is a data structure for describing the memory description information of the functional module, and the parameters included in the memory descriptor template of different types of functional modules are different; The module types of different functional modules in the target system are determined according to the memory descriptor templates of the functional modules, and different memory management strategies are executed for different types of functional modules.
6. The memory management method according to claim 5, wherein: After the target system is started, generating a memory descriptor template for each functional module according to the memory configuration information of the functional module includes: After the target system is started, reading a header file and an enabling macro in a memory management source file, wherein the header file of the memory management source file includes memory configuration information of each of the functional modules; Based on the enabling macro, executing the header file in a macro expansion manner, so as to perform different descriptor generation operations on the plurality of functional modules of different types according to the memory configuration information of each functional module, so as to generate a memory descriptor template for each functional module; Among them, compared with the memory descriptor of the second type of module, the memory descriptor of the first type of module additionally includes a pointer variable for pointing to the memory pool actually allocated for the first type of module, so that based on the memory pool pointer variable in the memory descriptor, the preset memory pool is allocated to the first type of module, and memory allocation based on the preset memory pool is performed on the first type of module.
7. A memory management device, characterized in that: include: a determination module, configured to determine module types of different functional modules in a target system, wherein the module types are used to indicate whether the functional modules are first-class modules that work abnormally after a memory allocation failure; a first management module configured to, when the module type of the functional module indicates that the functional module is a first-category module, perform memory management on the functional module according to a memory pool management mode, allocate a corresponding preset memory pool to the functional module, and perform memory allocation on the functional module based on the preset memory pool; The second management module is configured to perform memory management on the functional module according to a memory heap management mode when the module type of the functional module indicates that the target module is a second type module, so as to perform memory allocation based on the memory heap on the functional module.
8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the steps of the memory management method according to any one of claims 1 to 6 are implemented.
9. A readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the memory management method according to any one of claims 1 to 6 are implemented.
10. A computer program product comprising a computer program, characterized in that When the computer program is executed, the memory management method according to any one of claims 1 to 6 is executed.