Method and apparatus for dynamic management of extended memory
By using memory management control blocks and linear linked list node structures in embedded systems, combined with the first-fit algorithm, the problem of efficient dynamic management of external memory is solved, achieving low fragmentation and efficient memory allocation, thus improving the real-time performance and stability of the system.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEIJING ETERNAL CREATIVE TECH CO LTD
- Filing Date
- 2025-09-30
- Publication Date
- 2026-05-01
AI Technical Summary
In embedded systems, the address mapping of external memory requires setting arrays of the same size, which increases data read and write latency. Frequent dynamic allocation is troublesome and has a high development threshold. In multi-task concurrent scenarios, memory contention and fragmentation are serious problems, affecting the real-time performance and stability of the system.
It employs a memory management control block structure and a linear linked list node structure. It uses the first-fit algorithm to locate contiguous free memory regions that meet the requested size requirements, and combines the memory management linked list for efficient allocation and release, supporting dynamic management of external memory expansion.
It achieves efficient and low-fragmentation memory management, reduces traversal overhead, improves system resource utilization and development efficiency, supports dynamic expansion of multiple regions, and is compatible with standard dynamic memory management interfaces.
Smart Images

Figure CN121326570B_ABST
Abstract
Description
External memory dynamic management method and device Technical Field
[0001] This disclosure relates to the field of memory management technology, and in particular to a method and apparatus for dynamic management of external memory, which is applicable to the efficient dynamic allocation and reclamation of external memory in resource-constrained embedded systems such as microcontrollers. Background Technology
[0002] In the field of embedded systems, microcontrollers are widely used in industrial control, smart homes, medical devices, and other scenarios. However, they are limited by the internal memory of the microcontroller, such as the capacity of Static Random Access Memory (SRAM), and the memory requirements of dynamic data structures (such as linked lists and trees) or bursty tasks. Traditional solutions expand storage capacity by adding external memory, such as external SRAM or Dynamic Random Access Memory (DRAM), but this faces multiple technical bottlenecks.
[0003] In related technologies, external memory address mapping requires setting up arrays of the same size for each address, leading to a significant increase in data read / write latency, and frequent dynamic allocation is also relatively cumbersome. Furthermore, the dynamic memory management APIs provided by the C library, such as the memory allocation function Malloc and the memory deallocation function Free, typically only support internal memory. Developers must implement the underlying logic for external memory address mapping, error detection, and defragmentation themselves, resulting in a high development threshold and poor fault tolerance. Especially in multi-task concurrent scenarios, memory contention and fragmentation issues are further exacerbated, severely restricting system real-time performance and stability.
[0004] Therefore, there is an urgent need for an efficient and low-fragmentation method for dynamic management of external memory to solve the above-mentioned technical problems. Summary of the Invention
[0005] The purpose of this disclosure is to provide a method and apparatus for dynamic management of external memory, achieving efficient and low-fragmentation memory management.
[0006] To achieve the above objectives, this disclosure provides the following technical solution:
[0007] First, a method for dynamic management of external memory expansion is provided, including:
[0008] Create a memory management control block structure and a linear linked list node structure. The memory management control block structure includes a start address field and a size field. The linear linked list node structure includes a memory management control block data area and a pointer to the next node.
[0009] Based on the memory management control block structure and the linear linked list node structure, a memory management linked list is created, and the starting address of the head node of the memory management linked list is set to the starting address of the external memory, and the size field is initialized to 0;
[0010] When a memory request is received, the current length of the memory management linked list is obtained, and a new linear linked list node is created.
[0011] The memory management linked list is traversed from the head node, and the first-fit algorithm is used to locate the contiguous free memory region that meets the requested size requirement. The starting address of the free memory region is then calculated.
[0012] Based on the starting address of the free memory region, set the memory management control block starting address field and size field of the new linear linked list node, insert the new linear linked list node into the corresponding position of the memory management linked list, clear the allocated memory region, and return the starting address of the free memory region.
[0013] When a memory release request is received, the memory management linked list is traversed and searched. The linear linked list node corresponding to the memory to be released is located by address matching. The linear linked list node is deleted from the memory management linked list, and the passed pointer is set to null.
[0014] As an optional technical solution of this disclosure, the creation of a memory management control block structure and a linear linked list node structure includes:
[0015] Based on memory management requirements, define the memory management control block structure type and set the start address field and size field for unsigned integers;
[0016] Configure the fields of the memory management control block structure, using the start address field to identify the physical start position of the memory block and the size field to record the total number of bytes in the memory block;
[0017] Based on the field configuration, the memory management control block structure is created, and the memory management control block structure is obtained.
[0018] As an optional technical solution of this disclosure, the creation of a memory management control block structure and a linear linked list node structure includes:
[0019] Based on the linked list indexing requirements, define the linear linked list node structure type and set the memory management control block data area and node pointer area;
[0020] The linear linked list node structure is configured by using the memory management control block as the node data area and the pointer to the next node as the node pointer area.
[0021] Based on the aforementioned structural configuration, the linear linked list node structure is created, resulting in the linear linked list node structure.
[0022] As an optional technical solution of this disclosure, a memory management linked list is created based on the memory management control block structure and the linear linked list node structure. The starting address of the head node of the memory management linked list is set to the physical starting address of the external memory, and the size field is initialized to 0, including:
[0023] Perform memory allocation operations on the linear linked list node structure to create the linked list head node;
[0024] The memory management linked list is initialized based on the head node of the linked list;
[0025] The parameters of the memory management control block of the head node of the memory management linked list are set: the starting address field is set to the physical starting address of the external memory, the size field is set to 0, and the next node pointer is set to null, so as to obtain the memory management linked list that has been initialized.
[0026] As an optional technical solution of this disclosure, the memory management linked list is traversed from the head node, and the first-fit algorithm is used to locate the contiguous free memory region that meets the requested size requirement. The starting address of the free memory region is calculated, including:
[0027] Based on the head node of the memory management linked list, set the linked list traversal pointer to point to the next node after the head node;
[0028] The memory management linked list is traversed sequentially to calculate the free memory size between the current node and the next node, and to determine whether the free memory size is greater than the requested memory size.
[0029] Based on the comparison between the free memory size and the requested memory size, when the free memory size is greater than the requested memory size, a free memory region that meets the conditions is found. The starting address of the free memory region is calculated by adding the starting address of the current node to the size of the current node.
[0030] As an optional technical solution of this disclosure, based on the starting address of the free memory region, the memory management control block starting address field and size field of the new linear linked list node are set, the new linear linked list node is inserted into the linked list position corresponding to the free memory region of the memory management linked list, the allocated memory region is cleared, and the starting address of the free memory region is returned, including:
[0031] Based on the starting address of the free memory region, the memory management control block starting address field of the new linear linked list node is set to the starting address of the free memory region, and the size field is set to the requested memory size;
[0032] Perform a node insertion operation on the memory management linked list, insert the new linear linked list node into the linked list position corresponding to the free memory area, and update the pointer relationship between adjacent nodes;
[0033] Based on the starting address field and size field of the memory management control block of the new linear linked list node, the allocated memory region is cleared byte by byte, and all data in the memory region is set to 0x00.
[0034] The new linear linked list node is verified. After confirming that the node was successfully inserted, the starting address of the free memory region is returned, thus obtaining the starting address of the allocated memory.
[0035] As an optional technical solution of this disclosure, based on the start address field and size field of the memory management control block of the new linear linked list node, the allocated memory region is cleared byte by byte, and all data in the memory region is set to 0x00, including:
[0036] Based on the memory management control block start address field and size field of the new linear linked list node, calculate the range of memory regions that need to be cleared;
[0037] The memory region is traversed in a loop, with the starting address offset incremented from 0 to the size field minus 1.
[0038] Based on the starting address offset, the starting address plus the data at the offset position is set to 0x00 to obtain the memory area after the clearing process is completed.
[0039] As an optional technical solution of this disclosure, when receiving a memory release request, the memory management linked list is traversed and searched to locate the linear linked list node corresponding to the memory to be released through address matching, the linear linked list node is deleted from the memory management linked list, and the input pointer is set to null, including:
[0040] Receive a memory release request containing the address of the memory to be released, and obtain the current length of the memory management linked list;
[0041] The memory management linked list is traversed starting from the head node, and the memory address to be released is compared and matched with the memory management control block start address field of each node;
[0042] Based on the address comparison and matching results, when a node whose starting address field is equal to the address of the memory to be released is found, the position of the node in the linked list is recorded;
[0043] Based on the node position, the linked list deletion operation is invoked to remove the linear linked list node from the memory management linked list, thereby releasing the memory space occupied by the node;
[0044] The passed memory pointer is set to NULL to obtain the release completion status.
[0045] As an optional technical solution of this disclosure, when the memory management linked list is traversed to the last node and the remaining memory space is less than the requested memory size, it includes:
[0046] The memory management linked list is traversed to determine whether the current node is the last node. If the pointer of the next node is null, it is determined to be the last node.
[0047] Based on the start address field and size field of the memory management control block of the last node, the remaining available memory space is calculated as the external memory capacity minus the end address of the last node.
[0048] The remaining available memory space is compared with the requested memory size. If the remaining available memory space is less than the requested memory size, it is determined that there is insufficient memory.
[0049] Based on the memory shortage determination result, the memory space of the newly created node is released, and a null pointer NULL is returned to indicate that the memory allocation failed, thus obtaining the memory shortage handling result.
[0050] Corresponding to the above-described method for dynamic management of external memory, this disclosure also provides a device for dynamic management of external memory, briefly described as follows:
[0051] The data structure creation module is used to create memory management control block structures and linear linked list node structures;
[0052] The memory management linked list initialization module is used to create a memory management linked list and perform initialization settings based on the memory management control block structure and the linear linked list node structure.
[0053] The memory allocation processing module is used to receive memory allocation requests, create new linear linked list nodes, and use the first-fit algorithm to find free memory regions.
[0054] The memory allocation module is used to set node parameters, insert into the linked list, clear memory, and return the allocation address.
[0055] The memory release processing module is used to receive memory release requests, locate and delete nodes by matching addresses, and set pointers to null.
[0056] This disclosure also provides an external memory dynamic management device, including a processor and a memory. The memory stores machine-readable instructions executable by the processor. The processor is used to execute the machine-readable instructions stored in the memory. When the machine-readable instructions are executed by the processor, the processor performs the steps of the external memory dynamic management method described above.
[0057] This embodiment of the disclosure uses the first-fit algorithm for external memory expansion, making memory management more efficient. Each search starts from a low address, reserving a large block of free memory at a high address for subsequent use, avoiding memory allocation failures due to insufficient memory. By decoupling physical address management and logical index maintenance through a two-layer structure of memory management control block and linear linked list nodes, combined with the first-fit algorithm, memory allocation efficiency is improved, which can greatly reduce traversal overhead compared to the traditional singly linked list scheme.
[0058] In addition, the solution based on the embodiments of this disclosure can use an API compatible with the standard dynamic memory management interface, so that developers can seamlessly manage external memory without having to worry about hardware differences;
[0059] Furthermore, the embodiments disclosed herein can support dynamic expansion of multiple regions, adapt to external memory of different capacities, and significantly improve system resource utilization and development efficiency. Attached Figure Description
[0060] To more clearly illustrate the technical solutions of the embodiments of this disclosure, the accompanying drawings used in the embodiments will be briefly described below. These drawings are incorporated in and constitute a part of this specification. They illustrate embodiments conforming to this disclosure and, together with the specification, serve to explain the technical solutions of this disclosure. It should be understood that the following drawings only show some embodiments of this disclosure and should not be considered as limiting the scope. Those skilled in the art can obtain other related drawings based on these drawings without creative effort.
[0061] Figure 1 is a flowchart of memory management in an embodiment of this disclosure;
[0062] Figure 2a is one of the schematic diagrams of linked list operation in the embodiments of this disclosure;
[0063] Figure 2b is a second schematic diagram of linked list operation in an embodiment of this disclosure;
[0064] Figure 3 is a flowchart of memory creation in an embodiment of this disclosure;
[0065] Figure 4 is a schematic diagram of a computer device (external memory dynamic management device) used for dynamic management of external memory in an embodiment of this disclosure. Detailed Implementation
[0066] To make the objectives, technical solutions, and advantages of the embodiments of this disclosure clearer, the technical solutions of the embodiments of this disclosure will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this disclosure, and not all of them. The components of the embodiments of this disclosure described and shown in the accompanying drawings can generally be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of this disclosure provided in the accompanying drawings is not intended to limit the scope of the claimed disclosure, but merely represents selected embodiments of this disclosure. All other embodiments obtained by those skilled in the art based on the embodiments of this disclosure without inventive effort are within the scope of protection of this disclosure.
[0067] It should be noted that similar labels and letters in the following figures indicate similar items. Therefore, once an item is defined in one figure, it does not need to be further defined and explained in subsequent figures.
[0068] In this document, the term "and / or" merely describes a relationship, indicating that three relationships can exist. For example, A and / or B can represent three cases: A alone, A and B simultaneously, and B alone. Furthermore, the term "at least one" in this document means any combination of at least two of any one or more elements. For example, including at least one of A, B, and C can mean including any one or more elements selected from the set consisting of A, B, and C.
[0069] The present disclosure will be further described below with reference to the accompanying drawings and embodiments.
[0070] Example 1
[0071] As shown in Figure 1, a method for dynamic management of external memory expansion is provided, including:
[0072] Step S1: Create a memory management control block structure and a linear linked list node structure. The memory management control block structure includes a start address field and a size field. The linear linked list node structure includes a memory management control block data area and a pointer to the next node.
[0073] Here, by creating a two-layer structure including a memory management control block structure and a linear linked list node structure, physical address management and logical index maintenance can be decoupled, which can greatly reduce the subsequent traversal overhead compared to the traditional singly linked list scheme.
[0074] Figures 2a and 2b are schematic diagrams of linked list operations; Figure 2a is a schematic diagram of the memory management control card structure and the linear linked list structure, and Figure 2b is a schematic diagram of the linked list; the subsequent description of this disclosure can be understood in conjunction with these operation schematic diagrams.
[0075] In practice, as shown in Figure 3, the creation process may include:
[0076] Step S1.1: Based on memory management requirements, define the memory management control block structure type and set the start address field and size field of the unsigned integer.
[0077] Step S1.2: Configure the fields of the memory management control block structure, using the start address field to identify the physical start position of the memory block and the size field to record the total number of bytes in the memory block.
[0078] Step S1.3: Based on the field configuration, complete the creation of the memory management control block structure to obtain the memory management control block structure.
[0079] In this embodiment, the specific implementation of the memory management control block structure is as follows:
[0080] typedef struct Memory_Man_Block_Str { unsigned int start_addr; / / Start address of the memory region unsigned int size; / / Size of the memory block} Memory_Man_Block_Str; / / Memory control block for memory management (each memory allocation requires a memory control block to describe the current memory region)
[0081] The code above defines a structure named `Memory_Man_Block_Str`, containing two unsigned integer fields: `start_addr` to store the starting address of the memory region, and `size` to record the size of the memory block. Whenever a block of memory is allocated, a memory control block instance is created to describe the attributes of that memory region.
[0082] Step S1.4: Based on the linked list index requirements, define the linear linked list node structure type and set the memory management control block data area and node pointer area.
[0083] Step S1.5: Configure the structure of the linear linked list node structure, using the memory management control block as the node data area and the pointer to the next node as the node pointer area.
[0084] Step S1.6: Based on the structure configuration, complete the creation of the linear linked list node structure to obtain the linear linked list node structure.
[0085] Step S1 involves creating the memory management control block structure and the linear linked list node structure. This step is fundamental to the entire external memory dynamic management method because it defines the core data structures required to manage external memory. The memory management control block structure describes the physical attributes of each memory block, while the linear linked list node structure is used to construct the linked list index system that manages these memory blocks.
[0086] Step S1.1 involves defining the memory management control block structure type based on memory management requirements. In embedded systems, the management of external memory requires precise recording of the physical location and size of each memory block. Therefore, we need to design a structure containing start address and size information. Unsigned integers are chosen as the field type because memory addresses and sizes are non-negative values; using unsigned integers provides a larger address space range while avoiding unnecessary sign bit checks, thus improving system efficiency.
[0087] Step S1.2 configures the fields of the memory management control block structure. The start address field (start_addr) is designed to identify the physical starting location of the memory block, which directly corresponds to a location in the physical address space of the external memory. The size field (size) records the total number of bytes in the memory block, representing the size of the continuously allocated memory space starting from the start address. These two fields together define a complete memory region, enabling the system to accurately track the boundaries of each allocated memory block.
[0088] Step S1.3 completes the creation of the memory management control block structure. In this step, the system generates the final memory management control block structure type definition at compile time based on the previously defined structure and configuration. This structure will serve as the basic unit for subsequent memory management operations. Whenever the system allocates a new block of memory, an instance of this structure will be created to record the attribute information of this memory block.
[0089] Step S1.4 Defines the linear linked list node structure type based on the linked list index requirements. To efficiently manage multiple memory blocks, the system needs a dynamic data structure to organize all memory control blocks. A linear linked list is an ideal choice because it supports dynamic addition and deletion of nodes, suitable for frequent memory allocation and deallocation operations. Therefore, we need to design a linked list node structure containing a data area and a pointer area, where the data area stores the memory control blocks and the pointer area points to the next node.
[0090] Step S1.5 configures the structure of the linear linked list node. In this step, the system sets the memory management control block as the node's data area (block) to store specific memory block information; simultaneously, it sets a pointer (next) to the next node as the node's pointer area to construct the linked list structure. This design organically combines the physical attribute information of the memory block with the logical structure information of the linked list, forming a complete memory management index system.
[0091] Step S1.6 completes the creation of the linear linked list node structure. In this step, based on the previously defined structure and configuration, the system generates the final linear linked list node structure type definition at compile time and defines the corresponding pointer type alias (ListP) for subsequent use. This structure will serve as the basic unit for constructing the memory-managed linked list. Each node contains attribute information of a memory block and is connected to other nodes through pointers, forming a complete linked list structure.
[0092] In this embodiment, the specific implementation of the linear linked list node structure is as follows:
[0093] typedef struct ListNode_Struct { Memory_Man_Block_Str block; / / The data area of the node, which is a memory control block struct ListNode_Str *next; / / The node pointer area, pointing to the next node} ListNode_Struct, *ListP; / / The node structure of the linear linked list
[0094] The code above defines a structure named `ListNode_Struct` as a node in a linear linked list. This structure contains two main parts: a `Memory_Man_Block_Str` field, which serves as the node's data area and stores memory control block information; and a pointer `next`, pointing to the next node, used to construct the linked list structure. `ListP` is also defined as a pointer type alias for `ListNode_Struct` for convenient subsequent use.
[0095] Step S2: Based on the memory management control block structure and the linear linked list node structure, create a memory management linked list, set the starting address of the head node of the memory management linked list to the starting address of the external memory, and initialize the size field to 0.
[0096] Step S2 involves creating a memory management linked list based on the previously created memory management control block structure and linear linked list node structure. The core objective of this step is to establish an initialized linked list structure as the foundation for all subsequent memory management operations. The memory management linked list is the core data structure of the entire external memory dynamic management system. It records and tracks all allocated memory blocks through ordered nodes, achieving efficient memory allocation and reclamation. Initially, this linked list contains only a head node, whose starting address is set to the physical starting address of the external memory, and its size field is initialized to 0, indicating that no memory has been allocated yet.
[0097] Step S2.1: Perform memory allocation operations on the linear linked list node structure to create the head node of the linked list.
[0098] Step S2.1 allocates memory for the linear linked list node structure and creates the head node. In this step, the system first needs to allocate memory for the head node. The head node is the starting point of the linked list and the entry point for all subsequent operations. The system allocates sufficient memory for the ListNode_Struct type by calling a memory allocation function (such as malloc) and creates a new node instance. This node will serve as the head node of the linked list; it does not store actual memory block information but acts as a sentinel node to facilitate the implementation of linked list operations. If memory allocation fails, the system will return an error status indicating that the memory management system initialization failed.
[0099] Step S2.2: Initialize the memory management linked list based on the head node of the linked list.
[0100] Step S2.2 initializes the memory management linked list based on the head node. After successfully creating the head node, the system sets this node as the starting point of the memory management linked list. This step essentially assigns the previously allocated head node pointer to a global variable (such as memory_mang_list), making it the unified entry point for the entire system to access the linked list. Through this global variable, other parts of the system can easily access and manipulate the memory management linked list, performing functions such as memory allocation, querying, and deallocation. This design gives the memory management system good modularity, facilitating system integration and maintenance.
[0101] Step S2.3: Set the parameters of the memory management control block of the head node of the memory management linked list, set the starting address field to the physical starting address of the external memory, set the size field to 0, and set the next node pointer to null, so as to obtain the memory management linked list after initialization.
[0102] Step S2.3 sets the parameters of the memory management control block of the head node of the memory management linked list. In this step, the system needs to initialize the memory management control block fields in the head node. Specifically, the start address field is set to the physical start address of the external memory (usually a predefined constant, such as MEMORY_MANG_START_ADDR), which is the base address of the external memory in the physical address space. The size field is set to 0, indicating that no memory has been allocated initially. The next node pointer is set to NULL, indicating that there is only one node in the linked list for the time being. After these initialization settings are completed, the memory management linked list is in a ready state and can begin to accept memory allocation and deallocation requests. This initialized linked list has a simple structure but complete functionality, providing a solid foundation for subsequent dynamic memory management operations.
[0103] In this embodiment, the specific implementation of creating the memory management linked list is as follows:
[0104]
[0105]
[0106] The code above first defines a static global variable `memory_mang_list` as the head pointer of the memory management linked list. The `Memory_Mang_Init` function initializes the memory management system. It creates a new linked list by calling the `Linear_List_Create` function and assigns it to `memory_mang_list`. If the linked list creation fails (returns NULL), the function returns error code 1; otherwise, it sets the memory control block start address of the linked list head node to the predefined external memory start address (`MEMORY_MANG_START_ADDR`), initializes the size field to 0, indicating that no memory has been allocated initially, and then returns success code 0.
[0107] Step S3: When a memory request is received, obtain the current length of the memory management linked list and create a new linear linked list node.
[0108] Step S3 involves obtaining the current length of the memory management linked list and creating a new linear linked list node upon receiving a memory allocation request. When an application needs to use external memory, it sends a memory allocation request to the memory management system, typically containing information about the required memory size. Upon receiving this request, the system first needs to understand the current state of the memory management linked list, especially its length. This helps the subsequent memory allocation algorithm make better decisions. The linked list length actually reflects the number of allocated memory blocks and is an important indicator of memory usage. Simultaneously, the system needs to create a new linear linked list node to record information about the memory block to be allocated. This new node will be inserted into the memory management linked list after successfully finding a suitable free memory region, becoming part of the list. If node creation fails (e.g., due to insufficient internal system memory), the entire memory allocation process will fail, and the system will return an error status. This step is the preparation phase of the memory allocation process, preparing for subsequent free memory search and allocation operations.
[0109] Step S4: Traverse the memory management linked list starting from the head node, use the first-fit algorithm to locate the contiguous free memory region that meets the requested size requirement, and calculate the starting address of the free memory region.
[0110] Step S4.1: Based on the head node of the memory management linked list, set the linked list traversal pointer to point to the next node after the head node.
[0111] Step S4.2: Perform sequential traversal of the memory management linked list, calculate the free memory size between the current node and the next node, and determine whether the free memory size is greater than the requested memory size.
[0112] Step S4.3: Based on the comparison result of the free memory size and the requested memory size, when the free memory size is greater than the requested memory size, it is determined that a free memory region that meets the conditions has been found. The starting address of the free memory region is calculated as the starting address of the current node plus the size of the current node, thus obtaining the starting address of the free memory region.
[0113] In one embodiment, step S4 involves traversing the memory management linked list from its head node, using the first-fit algorithm to locate a contiguous free memory region that meets the requested size requirement, and calculating the starting address of the free memory region. This step is the core of memory allocation, determining where the newly requested memory block will be placed in the external memory. The first-fit algorithm is a classic memory allocation strategy that starts searching from the lowest memory address and selects the first sufficiently large free region for allocation. This algorithm is simple to implement and fast, making it particularly suitable for resource-constrained embedded systems. In this method, by traversing the memory management linked list, the system can track the gaps between allocated memory blocks and find a contiguous free memory region that meets the requested size requirement.
[0114] Step S4.1: Based on the head node of the memory management linked list, set the linked list traversal pointer to point to the next node after the head node. Before starting the search process, the system needs to initialize a traversal pointer for moving within the linked list. Since the head node typically does not store actual memory block information (it is a sentinel node), traversal begins from the next node after the head node. This traversal pointer will continuously move forward during the subsequent search process until a suitable free area is found or the end of the linked list is reached. This head-first traversal method meets the requirements of the first-fit algorithm, ensuring that the search for free memory areas starts from low addresses.
[0115] In step S4.2, the memory management linked list is traversed sequentially, the size of free memory between the current node and the next node is calculated, and it is determined whether the free memory size is greater than the requested memory size. During the traversal, the system needs to check whether the space between any two adjacent nodes is large enough. Specifically, the system calculates the difference between the end address of the current node (the start address of the current node plus the size) and the start address of the next node; this difference is the size of free memory between the two allocated memory blocks. Then, the system compares this free memory size with the requested memory size to determine whether the requirement is met. This calculation method fully utilizes the characteristic that nodes in the memory management linked list are arranged in address order, and can accurately identify all possible free memory regions.
[0116] Regarding step S4.3, based on the comparison between the free memory size and the requested memory size, if the free memory size is greater than the requested memory size, a free memory region that meets the conditions is found. The starting address of the free memory region is calculated as the starting address of the current node plus the size of the current node. When the system finds a sufficiently large free region, it stops traversing and determines this region as the allocation location for a new memory block. The starting address of the free memory region is the ending address of the current node, i.e., the starting address of the current node plus its size. This calculation ensures that the newly allocated memory block immediately follows the currently allocated memory block, avoiding memory fragmentation. Furthermore, because the first-fit algorithm is used, the system always selects the first free region that meets the conditions, which helps to preserve large, contiguous blocks of free memory in high-address areas, preparing for potential large memory demands in the future.
[0117] Step S5: Based on the starting address of the free memory region, set the memory management control block starting address field and size field of the new linear linked list node, insert the new linear linked list node into the corresponding position of the memory management linked list, clear the allocated memory region, and return the starting address of the free memory region.
[0118] Step S5.1: Based on the starting address of the free memory region, set the memory management control block starting address field of the new linear linked list node to the starting address of the free memory region, and set the size field to the requested memory size.
[0119] Step S5.2: Perform a node insertion operation on the memory management linked list, insert the new linear linked list node into the linked list position corresponding to the free memory area, and update the pointer relationship between adjacent nodes.
[0120] Step S5.3: Based on the memory management control block start address field and size field of the new linear linked list node, the allocated memory region is cleared byte by byte, and all data in the memory region is set to 0x00.
[0121] Step S5.4: Verify the new linear linked list node. After confirming that the node was successfully inserted, return the starting address of the free memory region to obtain the starting address of the allocated memory.
[0122] In this embodiment, the specific implementation of memory allocation is as follows:
[0123]
[0124]
[0125] The code above implements a memory allocation function. First, it obtains the length of the current memory management linked list and creates a new linked list node to record the information of the memory block to be allocated. If node creation fails, it returns NULL to indicate that the memory allocation has failed. Then, it traverses the linked list starting from the head, using the first-fit algorithm to find a contiguous free memory region that meets the requested size requirement. During the traversal, it calculates the free memory size between the current node and the next node to determine if the allocation requirement is met. Once a suitable free region is found, it sets the starting address of the new node's memory control block to the ending address of the current node, sets its size to the requested memory size, and inserts the new node into the corresponding position in the linked list. Finally, it clears the allocated memory region to ensure a clean initial state and returns the starting address of the allocated memory.
[0126] Step S5 involves setting the memory management control block start address field and size field of the new linear linked list node based on the starting address of the free memory region, inserting the new linear linked list node into the corresponding position in the memory management linked list, clearing the allocated memory region, and returning the starting address of the free memory region. This step completes the final stage of memory allocation, including updating the memory management data structure, initializing the allocated memory region, and returning the available memory address to the caller. These operations ensure that the allocated memory region is correctly recorded in the memory management system and is in a clean initial state, making it safe for application use.
[0127] Step S5.1: Based on the starting address of the free memory region, set the memory management control block start address field of the new linear linked list node to the starting address of the free memory region, and set the size field to the requested memory size. After finding a suitable free memory region, the system needs to update the newly created linear linked list node to record the information of the memory block to be allocated. Specifically, set the node's memory management control block start address field to the previously calculated starting address of the free memory region, and set the size field to the requested memory size. This information will be used for subsequent memory management operations, such as locating the corresponding memory block when releasing memory. This precise recording of each memory block's attributes is key to achieving efficient memory management, enabling the system to accurately track each allocated memory region.
[0128] Step S5.2 performs a node insertion operation on the memory management linked list, inserting the new linear linked list node into the linked list position corresponding to the free memory region and updating the pointer relationships of adjacent nodes. After setting up the new node, the system needs to insert it into the appropriate position in the memory management linked list. Since the nodes in the memory management linked list are sorted according to the starting address of the memory block, the new node should be inserted between the current node and the next node. This requires updating the pointer relationships of related nodes: setting the next pointer of the new node to the next node of the current node, and then setting the next pointer of the current node to the new node. This operation maintains the order of the linked list, facilitating subsequent memory management operations. If the insertion operation fails (e.g., due to an internal system error), the entire memory allocation process will fail, and the system will return an error status.
[0129] Step S5.3, based on the start address and size fields of the memory management control block of the new linear linked list node, performs byte-by-byte zeroing on the allocated memory region, setting all data in the memory region to 0x00. After successfully inserting a new node, the system needs to initialize the allocated memory region. Zeroing is a common initialization method; it sets every byte of the memory region to 0, ensuring no residual data remains. This method has two main advantages: first, it improves security and prevents sensitive data leakage; second, it provides a predictable initial state for the application, reducing potential errors. Zeroing is achieved by looping through each byte of the memory region and setting it to 0x00. Although this process is simple, it is crucial for ensuring system stability and security.
[0130] S5.3 includes:
[0131] Step S5.3.1: Based on the memory management control block start address field and size field of the new linear linked list node, calculate the range of memory regions that need to be cleared.
[0132] Step S5.3.2: Loop through the memory region range, incrementing the starting address offset from 0 to the size field minus 1.
[0133] Step S5.3.3: Based on the starting address offset, add the data at the offset position to the starting address and set it to 0x00 to obtain the memory area after the clearing process is completed.
[0134] Step S5.4 verifies the new linear linked list node. After confirming successful insertion, it returns the starting address of the free memory region, obtaining the starting address of the allocated memory. After completing all the aforementioned operations, the system performs a final verification to ensure the memory allocation process is successful. If everything is normal, the system returns the starting address of the allocated memory region to the caller. This address is the entry point for the application to access the allocated memory; the application can start from this address and use a contiguous block of memory (size bytes). If any problems are found during the verification process, the system returns an error status (such as a NULL pointer), indicating that memory allocation has failed. This rigorous verification mechanism ensures the reliability of the memory management system and prevents potential memory access errors.
[0135] Step S6: When a memory release request is received, the memory management linked list is traversed and searched. The linear linked list node corresponding to the memory to be released is located by address matching. The linear linked list node is deleted from the memory management linked list, and the passed pointer is set to null.
[0136] Step S6.1: Receive a memory release request containing the address of the memory to be released, and obtain the current length of the memory management linked list.
[0137] Step S6.2: Traverse the memory management linked list starting from the head node, and compare the memory address to be released with the memory management control block start address field of each node.
[0138] Step S6.3: Based on the address comparison and matching results, when a node whose starting address field is equal to the address of the memory to be released is found, record the position of the node in the linked list.
[0139] Step S6.4: Based on the node position, call the linked list deletion operation to remove the linear linked list node from the memory management linked list and release the memory space occupied by the node.
[0140] Step S6.5: Set the incoming memory pointer to NULL to obtain the release completion status.
[0141] Step S6.1 involves receiving a memory release request containing the address of the memory to be released and obtaining the current length of the memory management linked list. When an application finishes using a block of memory, it sends a memory release request to the memory management system, typically containing information about the memory address to be released. Upon receiving this request, the system first needs to understand the current state of the memory management linked list, particularly its length. The linked list length reflects the number of currently allocated memory blocks, which helps the system assess the complexity and potential impact of the release operation. If the linked list length is 0, it means there are currently no allocated memory blocks; in this case, the release operation is invalid, and the system returns an error status. Obtaining the linked list length is usually achieved by traversing the linked list and counting, a basic linked list operation that prepares for subsequent node searches.
[0142] Step S6.2 involves traversing the memory management linked list from the head node, comparing the address of the memory to be freed with the memory management control block start address field of each node. After confirming that the linked list is not empty, the system needs to find the linked list node corresponding to the memory to be freed. This requires starting from the head node and checking each node's memory management control block start address field to see if it matches the address of the memory to be freed. This traversal search is a linear search process, and its time complexity is proportional to the length of the linked list. During the traversal, the system simultaneously records the position of the currently checked node; this position information will be used for subsequent node deletion operations. This precise matching ensures that the system only frees the specified memory block, avoiding the risk of accidentally freeing other memory blocks.
[0143] Step S6.3 is the process of recording the position of a node in the linked list when a node whose starting address field matches the memory address to be freed is found, based on the address comparison and matching results. When the system finds a node whose memory management control block starting address field matches the memory address to be freed during traversal, it indicates that the target node has been found. At this time, the system records the position of this node in the linked list (usually an index value starting from 0) and then stops traversing. This position information is a key parameter for subsequent deletion operations, telling the system which node in the linked list should be deleted. If no matching node is found after traversing the entire linked list, it means that the memory address to be freed was not allocated through this memory management system, or has already been freed. In this case, the system may return an error status or ignore the request.
[0144] Step S6.4 involves removing a linear linked list node from the memory management list based on its position using a linked list deletion operation, thus releasing the memory space occupied by the node. After finding the target node and recording its position, the system needs to delete the node from the linked list. This is typically achieved by calling a dedicated linked list deletion function (such as `Linear_List_Delete_Node`), which takes the head pointer of the linked list and the position of the node to be deleted as parameters. The deletion operation involves adjusting the pointer relationships between adjacent nodes so that the `next` pointer of the previous node directly points to the next node, thereby "disconnecting" the target node from the linked list. Then, the system releases the memory space occupied by the node, usually by calling a memory release function (such as `free`). This operation not only logically removes the node but also reclaims the system resources occupied by the node itself, preventing memory leaks.
[0145] Step S6.5 involves setting the incoming memory pointer to NULL, thus completing the memory release process. After deleting the node and reclaiming the memory, the system still needs to process the incoming memory pointer. To prevent dangling pointers, the system sets the incoming pointer to NULL. This approach has two main advantages: first, it prevents the application from continuing to use the pointer after it has been released, which could lead to serious memory access errors; second, it allows the application to determine whether the memory has been released by checking if the pointer is NULL. This pointer-setting process is a defensive programming practice that helps improve system stability and security. After completing all these operations, the system returns a success status, indicating that the memory release operation has been successfully completed.
[0146] Furthermore, in this embodiment, the specific implementation of memory release is as follows:
[0147]
[0148] The code above implements memory release functionality. First, it retrieves the current length of the memory management linked list. If the length is 0 (indicating the list is empty), it returns error code 1. Then, it traverses the list starting from the head node, comparing the memory address to be released with the starting address of the memory control block of each node. When a matching node is found, its position in the list is recorded, and the `Linear_List_Delete_Node` function is called to remove the node from the list. Finally, the passed pointer is set to NULL to prevent dangling pointer issues, and a success code 0 is returned to indicate that memory release is complete.
[0149] Example 2
[0150] When the memory management linked list is traversed to the last node and the remaining memory space is less than the requested memory size, including:
[0151] Step A1: Traverse the memory management linked list and determine whether the current node is the last node. If the pointer of the next node is null, it is determined to be the last node.
[0152] Step A1 involves traversing the memory-managed linked list and determining if the current node is the last node. The process of identifying the last node as null when the next node's pointer is null is called the final node. During memory allocation, if the system fails to find a sufficiently large free area after traversing the entire linked list, it checks the remaining memory space after the end of the list. To determine if the current node is the last node, the system checks if its next pointer is NULL. In linked list data structures, the next pointer of the last node is typically set to NULL, indicating the end of the linked list. This check is a common linked list operation used to identify the boundary conditions of the linked list. Once the system determines that the current node is the last node, it proceeds to the next step, calculating the remaining available memory space to determine if there is enough space to meet the allocation requirements.
[0153] Step A2: Based on the start address field and size field of the memory management control block of the last node, calculate the remaining available memory space size as the external memory capacity minus the end address of the last node.
[0154] Step A2 calculates the remaining available memory space based on the start address and size fields of the memory management control block of the last node. This is the process of subtracting the end address of the last node from the total external memory capacity. After determining that the current node is the last node, the system needs to calculate how much available memory space remains after the end of the linked list. This remaining space is equal to the total external memory capacity minus the end address of the last node (i.e., the start address of the last node plus its size). The total external memory capacity is usually a predefined constant representing the physical size limit of the external memory. This calculation method considers all allocated memory blocks, ensuring that the physical boundaries of the external memory are not exceeded. The calculated remaining available memory space is a key basis for determining whether the current request can be satisfied.
[0155] Step A3: Compare the remaining available memory space with the requested memory size. If the remaining available memory space is less than the requested memory size, it is determined that there is insufficient memory.
[0156] Step A3 compares the remaining available memory space with the requested memory size. If the remaining available memory space is less than the requested memory size, it's considered insufficient memory. After calculating the remaining available memory space, the system compares it with the requested memory size. If the remaining space is less than the requested size, it means that even using all the remaining space after the end of the linked list cannot meet the current request, and the system will determine that there is insufficient memory. This situation usually occurs when external memory is highly fragmented, or when the requested memory block is too large and exceeds the system's capacity. Insufficient memory is a common resource limitation, and the system needs an appropriate handling mechanism to deal with this situation and prevent more serious system problems caused by memory allocation failures.
[0157] Step A4: Based on the memory shortage determination result, release the memory space of the newly created node, return a null pointer NULL to indicate that the memory allocation failed, and obtain the memory shortage handling result.
[0158] Step A4, based on the insufficient memory determination result, releases the memory space occupied by the newly created node and returns a null pointer (NULL) to indicate that the memory allocation failed, thus obtaining the insufficient memory handling result. When the system determines that there is insufficient memory to meet the current allocation demand, appropriate error handling is required. First, the system releases the memory space occupied by the newly created node during the allocation process to prevent memory leaks. Then, the system returns a null pointer (NULL) to the caller, indicating that the memory allocation failed. This return value convention allows the application to detect allocation failure and take appropriate error handling measures, such as retrying, reducing the allocation size, or reporting the error to the user. This elegant failure handling mechanism is an important part of robust system design, ensuring that the system can maintain stable operation even under resource constraints and will not crash due to memory allocation failure.
[0159] In this embodiment, the specific implementation of the insufficient memory handling is as follows:
[0160] if ((list_p->next == NULL) && ((list_p->block.start_addr + list_p->block.size + size) > (MEMORY_MANG_START_ADDR + MEMORY_MANG_SIZE))) { return NULL; / / Return if there is insufficient memory remaining}
[0161] The code snippet above handles insufficient memory situations. When the linked list reaches its last node (list_p->next is NULL) and the remaining available memory is insufficient to meet the request (the end address of the last node plus the requested size exceeds the total capacity of the external memory), the function will return NULL, indicating that the memory allocation failed. In this case, the system will release the newly created node to prevent memory leaks and notify the caller of the allocation failure so that it can take appropriate error handling measures.
[0162] Example 3
[0163] In this embodiment, the specific implementation of creating a linked list node is as follows:
[0164]
[0165] The code above implements the functionality of creating a linked list node. The function first dynamically allocates a memory space of size `ListNode_Struct` using the `malloc` function and assigns the returned pointer to the `node` variable. If memory allocation fails (returns NULL), the function directly returns NULL to indicate that node creation failed. Otherwise, the newly created node is initialized: the `size` and `start_addr` fields of the memory control block are both set to 0, the `next` pointer is set to NULL, and then the pointer to the created node is returned. This ensures that the newly created node is in a clean initial state, facilitating subsequent operations.
[0166] The specific implementation of creating a linked list is as follows:
[0167]
[0168] The code above implements the functionality of creating a linked list. The function creates a new linked list node as the head node by calling the `Linear_List_Create_Node` function, and then assigns the returned pointer to the `List_P` type and stores it in the `list` variable. If node creation fails (returns NULL), the function directly returns NULL to indicate that linked list creation failed. Otherwise, it returns a pointer to the created head node. This function creates an empty linked list with only a head node, preparing for subsequent node insertion operations.
[0169] The specific implementation for inserting a node into a linked list is as follows:
[0170]
[0171] The code above implements the function of inserting a node into a linked list. The function takes three parameters: a pointer to the head of the linked list (`list`), the node to be inserted (`node`), and the insertion position (`position`). First, it checks if the linked list is empty; if so, it returns error code 1. Then, it gets the current length of the linked list; if the insertion position exceeds the list length, it returns error code 2. Next, it iterates through the list, moving the pointer `list_p` to the node preceding the insertion position. Finally, it adjusts the pointer relationships to complete the insertion: it sets the `next` pointer of the new node to the node following the current node, then sets the `next` pointer of the current node to the new node, and returns a success code 0 to indicate successful insertion.
[0172] The specific implementation of deleting a node in a linked list is as follows:
[0173]
[0174] The code above implements the function to delete a node from a linked list. The function takes two arguments: a pointer to the head of the linked list (`list`) and the position of the node to be deleted (`position`). First, it checks if the linked list is empty; if so, it returns error code 1. Then, it gets the current length of the linked list; if the deletion position exceeds the list length or is 0 (the head node cannot be deleted), it returns error code 2. Next, it decrements the position by 1 (to find the node preceding the node to be deleted), and then uses a loop to move the `list_p` pointer to the node preceding the node to be deleted. Then, it stores the node to be deleted in `list_code`, adjusts the `next` pointer of the preceding node to point to the next node, uses the `free` function to release the memory space occupied by the node to be deleted, and finally returns a success code 0 to indicate successful deletion.
[0175] The above description is merely a specific embodiment of this disclosure, but the scope of protection of this disclosure is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this disclosure should be included within the scope of protection of this disclosure. Therefore, the scope of protection of this disclosure should be determined by the scope of the claims.
[0176] This disclosure also provides a device for dynamic management of external memory, including:
[0177] The data structure creation module is used to create memory management control block structures and linear linked list node structures;
[0178] The memory management linked list initialization module is used to create a memory management linked list and perform initialization settings based on the memory management control block structure and the linear linked list node structure.
[0179] The memory allocation processing module is used to receive memory allocation requests, create new linear linked list nodes, and use the first-fit algorithm to find free memory regions.
[0180] The memory allocation module is used to set node parameters, insert into the linked list, clear memory, and return the allocation address.
[0181] The memory release processing module is used to receive memory release requests, locate and delete nodes by matching addresses, and set pointers to null.
[0182] In this embodiment, the data structure creation module includes a memory management control block creation unit and a linear linked list node creation unit. The memory management control block creation unit is responsible for defining the memory management control block structure type, setting the start address field and size field of the unsigned integer, and completing the creation of the memory management control block structure. The linear linked list node creation unit is responsible for defining the linear linked list node structure type, setting the memory management control block data area and node pointer area, and completing the creation of the linear linked list node structure.
[0183] The memory management linked list initialization module includes a linked list head node creation unit and a parameter setting unit. The linked list head node creation unit is responsible for performing memory allocation operations on the linear linked list node structure, creating the linked list head node, and initializing the memory management linked list. The parameter setting unit is responsible for setting the parameters of the memory management control block of the head node of the memory management linked list, setting the start address field to the physical start address of the external memory, setting the size field to 0, and setting the next node pointer to null.
[0184] The memory allocation processing module includes a request receiving unit, a node creation unit, and a free region search unit. The request receiving unit is responsible for receiving memory allocation requests and obtaining the current length of the memory management linked list. The node creation unit is responsible for creating new linear linked list nodes. The free region search unit is responsible for traversing the memory management linked list from the head node, using the first-fit algorithm to locate a contiguous free memory region that meets the allocation size requirement, and calculating the starting address of the free memory region.
[0185] The memory allocation module includes a node parameter setting unit, a linked list insertion unit, a memory clearing unit, and an address return unit. The node parameter setting unit is responsible for setting the start address field and size field of the memory management control block for a new linear linked list node based on the start address of the free memory region. The linked list insertion unit is responsible for performing node insertion operations on the memory management linked list, inserting the new linear linked list node into the linked list position corresponding to the free memory region of the memory management linked list, and updating the pointer relationships of adjacent nodes. The memory clearing unit is responsible for clearing the allocated memory region byte by byte, setting all data in the memory region to 0x00. The address return unit is responsible for verifying the new linear linked list node, and returning the start address of the free memory region after confirming successful node insertion.
[0186] The memory release processing module includes a release request receiving unit, a node search unit, a node deletion unit, and a pointer NULL setting unit. The release request receiving unit receives memory release requests containing the address of the memory to be released and obtains the current length of the memory management linked list. The node search unit traverses the memory management linked list from the head node, comparing the address of the memory to be released with the memory management control block start address field of each node. The node deletion unit, based on the address comparison results, records the node's position in the linked list when a node with a start address field equal to the address of the memory to be released is found, and calls the linked list deletion operation to remove the linear linked list node from the memory management linked list, releasing the memory space occupied by the node. The pointer NULL setting unit sets the passed memory pointer to NULL.
[0187] Furthermore, the specific implementation methods of the above modules are described in detail in the aforementioned method section, and will not be repeated here.
[0188] The modules described above can be implemented using hardware circuits or by running software code on a processor. In the software implementation, each module corresponds to a different functional part of a computer program, and its function is achieved by the processor executing the corresponding program code. In the hardware implementation, each module can be implemented using application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices.
[0189] As shown in Figure 4, this embodiment of the present disclosure also provides a computer device, which can also be called another external memory dynamic management device, including a processor 41 and a memory 42. The memory 42 stores machine-readable instructions that can be executed by the processor 41. The processor 41 is used to execute the machine-readable instructions stored in the memory 42. When the machine-readable instructions are executed by the processor 41, the processor 41 performs the steps of the external memory dynamic management method described above.
[0190] The aforementioned memory 42 includes a main memory 421 and an external memory 422; the main memory 421, also known as internal memory, is used to temporarily store the computational data in the processor 41, as well as the data exchanged with external memory 422 such as a hard disk. The processor 41 exchanges data with the external memory 422 through the main memory 421.
[0191] This disclosure also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, performs the steps of the external memory dynamic management method described in the above-described method embodiments. The storage medium can be a volatile or non-volatile computer-readable storage medium.
[0192] Furthermore, this disclosure also provides a computer program product storing a computer program. When the computer program is run by a processor, it executes the steps of the external memory dynamic management method provided in any of the above embodiments of this disclosure. For details, please refer to the above method embodiments, which will not be repeated here.
[0193] The aforementioned computer program product can be implemented through hardware, software, or a combination thereof. In one optional embodiment, the computer program product is specifically embodied in a computer storage medium, which can be a volatile or non-volatile computer-readable storage medium. In another optional embodiment, the computer program product is specifically embodied in a software product, such as a software development kit (SDK), etc.
[0194] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the devices and apparatuses described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here. In the several embodiments provided in this disclosure, it should be understood that the disclosed devices, apparatuses, and methods can be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. Furthermore, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Another point is that the displayed or discussed mutual coupling or direct coupling or communication connection may be through some communication interfaces; the indirect coupling or communication connection of devices or units may be electrical, mechanical, or other forms.
[0195] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0196] In addition, the functional units in the various embodiments of this disclosure can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0197] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a processor-executable, non-volatile, computer-readable storage medium. Based on this understanding, the technical solution of this disclosure, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this disclosure. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0198] Finally, it should be noted that the above-described embodiments are merely specific implementations of this disclosure, used to illustrate the technical solutions of this disclosure, and not to limit it. The protection scope of this disclosure is not limited thereto. Although this disclosure has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that any person skilled in the art can still modify or easily conceive of changes to the technical solutions described in the foregoing embodiments, or make equivalent substitutions for some of the technical features, within the scope of the technology disclosed in this disclosure; and these modifications, changes, or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this disclosure, and should all be covered within the protection scope of this disclosure. Therefore, the protection scope of this disclosure should be determined by the protection scope of the claims.
Claims
1. A method for dynamic management of external memory, characterized in that, include: Create a memory management control block structure and a linear linked list node structure. The memory management control block structure includes a start address field and a size field. The linear linked list node structure includes a memory management control block data area and a pointer to the next node. Based on the memory management control block structure and the linear linked list node structure, a memory management linked list is created, the starting address of the head node of the memory management linked list is set to the starting address of the external memory, and the size field is initialized to 0; when a memory request is received, the current length of the memory management linked list is obtained, and a new linear linked list node is created. The memory management linked list is traversed from the head node, and the first-fit algorithm is used to locate the contiguous free memory region that meets the requested size requirement. The starting address of the free memory region is then calculated. Based on the starting address of the free memory region, set the memory management control block starting address field and size field of the new linear linked list node, insert the new linear linked list node into the corresponding position of the memory management linked list, clear the allocated memory region, and return the starting address of the free memory region. When a memory release request is received, the memory management linked list is traversed and searched. The corresponding linear linked list node for the memory to be released is located by address matching. The linear linked list node is deleted from the memory management linked list, and the passed pointer is set to null. The creation of the memory management control block structure and the linear linked list node structure includes: defining the memory management control block structure type based on memory management requirements, setting an unsigned integer start address field and a size field; configuring the fields of the memory management control block structure, using the start address field to identify the physical starting position of the memory block, and using the size field to record the total number of bytes in the memory block; based on the field configuration, the creation of the memory management control block structure is completed, resulting in the memory management control block structure; defining the linear linked list node structure type based on linked list index requirements, setting a memory management control block data area and a node pointer area; configuring the structure of the linear linked list node structure, using the memory management control block as the node data area, and using the pointer to the next node as the node pointer area; based on the structure configuration, the creation of the linear linked list node structure is completed, resulting in the linear linked list node structure.
2. The method according to claim 1, characterized in that, Based on the memory management control block structure and the linear linked list node structure, a memory management linked list is created. The starting address of the head node of the memory management linked list is set to the physical starting address of the external memory, and the size field is initialized to 0. This includes: performing memory allocation operations on the linear linked list node structure to create a linked list head node; initializing the memory management linked list based on the linked list head node; setting parameters for the memory management control block of the head node of the memory management linked list, setting the starting address field to the physical starting address of the external memory, setting the size field to 0, and setting the next node pointer to null, thus obtaining the initialized memory management linked list.
3. The method according to claim 1, characterized in that, The memory management linked list is traversed from the head node to locate a contiguous free memory region that meets the requested size requirement using the first-fit algorithm. The starting address of the free memory region is calculated, including: setting the linked list traversal pointer to the next node based on the head node of the memory management linked list; sequentially traversing the memory management linked list, calculating the free memory size between the current node and the next node, and determining whether the free memory size is greater than the requested memory size; based on the comparison result of the free memory size and the requested memory size, if the free memory size is greater than the requested memory size, it is determined that a free memory region that meets the condition has been found, and the starting address of the free memory region is calculated by adding the starting address of the current node to the size of the current node.
4. The method according to claim 1, characterized in that, Based on the starting address of the free memory region, the memory management control block starting address field and size field of the new linear linked list node are set. The new linear linked list node is inserted into the linked list position corresponding to the free memory region of the memory management linked list. The allocated memory region is cleared, and the starting address of the free memory region is returned. This includes: based on the starting address of the free memory region, setting the memory management control block starting address field of the new linear linked list node to the starting address of the free memory region and setting the size field to the requested memory size; performing a node insertion operation on the memory management linked list, inserting the new linear linked list node into the linked list position corresponding to the free memory region, and updating the pointer relationship of adjacent nodes; based on the memory management control block starting address field and size field of the new linear linked list node, clearing the allocated memory region byte by byte, setting all data in the memory region to all zero values; verifying the new linear linked list node, and after confirming successful node insertion, returning the starting address of the free memory region to obtain the starting address of the allocated memory.
5. The method according to claim 4, characterized in that, Based on the starting address field and size field of the memory management control block of the new linear linked list node, the allocated memory region is cleared byte by byte, setting all data in the memory region to all zero values. This includes: calculating the range of memory regions to be cleared based on the starting address field and size field of the memory management control block of the new linear linked list node; traversing the memory region range in a loop, incrementing the starting address offset from 0 to the size field minus 1; and setting the data at the starting address plus the offset position to all zero values based on the starting address offset, thus obtaining the memory region after clearing.
6. The method according to claim 1, characterized in that, When a memory release request is received, the memory management linked list is traversed and searched. The linear linked list node corresponding to the memory to be released is located by address matching. The linear linked list node is deleted from the memory management linked list, and the input pointer is set to null. The process includes: receiving a memory release request containing the address of the memory to be released; obtaining the current length of the memory management linked list; traversing the memory management linked list from the head node; comparing the address of the memory to be released with the memory management control block start address field of each node; based on the address comparison result, when a node whose start address field is equal to the address of the memory to be released is found, recording the position of the node in the linked list; based on the node position, calling the linked list deletion operation to remove the linear linked list node from the memory management linked list and release the memory space occupied by the node; and setting the input memory pointer to null to obtain the release completion status.
7. The method according to claim 1, characterized in that, When the memory management linked list is traversed to the last node and the remaining memory space is less than the requested memory size, the process includes: traversing the memory management linked list, determining whether the current node is the last node (if the pointer of the next node is null), calculating the remaining available memory space based on the memory management control block start address field and size field of the last node (the external memory capacity minus the end address of the last node), comparing the remaining available memory space with the requested memory size, and determining that there is insufficient memory when the remaining available memory space is less than the requested memory size; based on the insufficient memory determination result, releasing the memory space of the newly created node, returning a null pointer to indicate that the memory allocation failed, and obtaining the insufficient memory processing result.
8. A dynamic management device for external memory, characterized in that, The system includes a processor and a memory, wherein the memory stores machine-readable instructions executable by the processor, and the processor executes the machine-readable instructions stored in the memory. When the machine-readable instructions are executed by the processor, the processor performs the steps of the external memory dynamic management method as described in any one of claims 1 to 7.
Citation Information
Patent Citations
Process address space management method and device based on dynamic memory allocation technology
CN115586973A
Memory management method and apparatus, and storage medium
WO2016011811A1