A Small-Scale CPU Memory Management Method
By designing the memory pool structure and management mechanism in small CPUs, the memory fragmentation problem is solved, and the efficiency of memory management and system stability are improved.
Patent Information
- Application Number
- CN202211387670.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-11-07
- Publication Date
- 2025-06-17
- Estimated Expiration
- 2042-11-07
AI Technical Summary
When a small CPU manages memory, due to the lack of a memory management mechanism on the hardware, frequent memory requests and releases lead to serious memory fragmentation, which leads to system operation crashes.
Design a memory pool structure type strupool, and define the memory pool g_pool variable in the CPU memory space. By initializing, applying and releasing the memory pool, efficient memory management and fragmentation avoidance are achieved.
It improves the efficiency of memory application and re-arranges the space when memory is freed, avoiding the generation of memory fragmentation, thereby ensuring the stable operation of the system.
Smart Images

Figure CN115658316B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of computer control, and relates to a memory management method for a small CPU. Background Art
[0002] Memory management is a technology for computer memory allocation and usage during the operation of a computer system. The purpose of memory management is to allocate memory more efficiently and quickly, and recycle the allocated memory for the next use when the memory is no longer needed. For large CPUs that can load Windows / linux operating systems, the operating system provides mature and reliable memory management methods, such as the stationary management of the memory continuous allocation management method, and the paging management mechanism, segmentation management mechanism, and page-segmentation management mechanism of the memory discontinuous management method.
[0003] When a small CPU, such as a microcontroller or a processor in some specific function chips, uses the dynamic memory application function malloc and memory release free in the standard library for memory management during the memory management process, since there is no memory management mechanism in the hardware of such processors, frequent application and release of memory easily cause the memory fragmentation to increase sharply, resulting in system operation crashes. Summary of the Invention
[0004] (1) Invention Objectives
[0005] The objective of the present invention is to provide a memory management method for a small CPU, which opens up a section of space in the memory. By managing this section of space, the memory application efficiency is high. At the same time, when the memory is released, the memory in this section of space is rearranged to avoid the generation of memory fragmentation.
[0006] (2) Technical Solutions
[0007] To solve the above technical problems, the present invention provides a memory management method for a small CPU, which includes the following steps:
[0008] S1: Design a memory pool structure type strupool;
[0009] S2: Define a memory pool g_pool variable in the CPU memory space;
[0010] S3: Perform an initialization operation on the memory pool g_pool variable defined in S2;
[0011] S4: Define a memory information management structure struinfo;
[0012] S5: Perform a memory application operation;
[0013] S6: Perform a release operation on the applied memory;
[0014] S7 opens the critical section, puts the system into the kernel state, disables interrupts and scheduling operations, and completes the release of the memory space.
[0015] (III) Beneficial Effects
[0016] The small CPU memory management method provided by the above technical solution allocates a section of space in the memory. By managing this section of space, the memory application efficiency is high. At the same time, when the memory is released, the memory in this section of space is rearranged to avoid the generation of memory fragmentation. Description of the Drawings
[0017] Figure 1 : A flowchart of memory application for a small CPU;
[0018] Figure 2 : A flowchart of memory release for a small CPU;
[0019] Figure 3 : A memory allocation diagram for a small CPU. Detailed Embodiments
[0020] To make the objectives, content, and advantages of the present invention clearer, the following further describes in detail the specific embodiments of the present invention with reference to the drawings and embodiments.
[0021] Refer to Figures 1 to 3 As shown, the small CPU memory management method in this embodiment includes the following steps:
[0022] S1: Design a memory pool structure type strupool
[0023] The memory pool structure type strupool includes: ① A pointer memaddr to the currently available memory, of the type unsigned char pointer, and this memory space is operated in a heap manner. The growth space is upward. ② An information management address infoaddr for the applied space, of the type unsigned char pointer, and this memory space is operated in a stack manner, and the growth space is downward. ③ The number cnt of the already applied memory blocks, of the type unsigned char. ④ The length len of the available memory, of the type unsigned short. ⑤ The memory pool space, which is managed by an array mempool, of the type unsigned char. The length is SIZE (set to 1024 in this example).
[0024] S2: Define a memory pool g_pool variable in the CPU memory space
[0025] The memory pool g_pool variable is a static variable, and the variable type is the memory pool structure type strupool defined in S1.
[0026] S3: Initialize the memory pool g_pool variable defined in S2
[0027] The initialization operations include: ① Set the pointer memaddr of the currently available memory to the address of the array mempool in S1. ② Set the information management address infoaddr of the allocated space to the address of the last element of the array mempool in S1. ③ Set the number cnt of the allocated memory blocks to 0. ④ Set the length len of the available memory to the length SIZE of the array mempool in S1. ⑤ Set all values in the memory pool space to 0 by using the memory setting function memset for operation.
[0028] S4: Define the memory information management structure struinfo
[0029] The structure struinfo includes three member functions in total: ① The address ptr of the memory pointer to be allocated, with the variable type being an untyped double pointer (void**). ② The address addr of the memory to be allocated. ③ The length length of the memory to be allocated.
[0030] S5: Perform the memory allocation operation
[0031] Define the double pointer variable mclptr, and store the allocated memory pointer in the double pointer variable mclptr. The allocated length is size, and the type is unsigned short integer. The length of this structure is set to STRUINFO_LEN (the calculation method is calculated by sizeof()).
[0032] S5_1: First, judge whether size is greater than or equal to the length SIZE of the array mempool defined in S1. If so, directly return a null pointer, indicating that this allocation fails. If not, go to S5_2
[0033] S5_2: Make the system enter the critical section, make the system enter the kernel state, and perform operations such as closing interrupts and scheduling.
[0034] S5_3: Move the information management address infoaddr of the allocated space in g_pool downward by the length of STRUINFO_LEN, and the updated value of infoaddr is infoaddr minus STRUINFO_LEN.
[0035] S5_4: Force the type conversion of the information management address infoaddr of the already applied space in g_pool to the memory information management structure struinfo type in S4 and perform the assignment operation. ① Set the value of ptr in this structure to the address of the pointer variable mclptr. ② Assign the value of memaddr in g_pool to the address addr of the memory to be applied in this structure. ③ Assign the length length of the memory to be applied in this structure to size. At this time, the value of memaddr in g_pool is the space to be applied, and this value is assigned to ptr.
[0036] S5_5: Offset the value of memaddr in g_pool upward by size. The updated value of memaddr is memaddr plus size. Update the value of the available memory length len in g_pool to len minus size and then minus STRUINFO_LEN. Increment the number cnt of the already applied memory blocks in g_pool by one.
[0037] S6: Perform the release operation on the applied memory.
[0038] The release operation is performed according to the secondary pointer variable mclptr in S5.
[0039] S6_1: If the value of the number cnt of the already applied memory blocks in g_pool is 0 at this time or the secondary pointer variable mclptr is a null pointer, then directly return, indicating that the release operation fails.
[0040] S6_2: Make the system enter the critical section, make the system enter the kernel state, and there are no operations such as interrupts and scheduling. Define a pointer variable curinfo of the memory information management structure type in S4 to represent the information management data being processed currently, and set the initial value to the value of the information management address infoaddr of the already applied space in g_pool.
[0041] S6_3: Define a variable i, starting from 0, traverse the memory pool, and judge whether the value of i is less than the number cnt of the already applied memory blocks in g_pool. If the judgment is successful, then go to S6_4; if the judgment fails, then directly return, indicating an error.
[0042] S6_4: Obtain the address ptr value of the memory pointer to be applied for in the curinfo of the S6_2 structure. Compare whether the value of ptr is equal to the mclptr value. If they are equal, define a variable freesize of the unsigned short integer type and assign it the length length of the memory to be applied for in curinfo. Then go to S6_5. If they are not equal, first update the curinfo value to the value of curinfo plus STRUINFO_LEN, increment the variable i defined in S6_3 by one, and then go to S6_3.
[0043] S6_5 sets the address ptr of the memory pointer to be applied for in the curinfo structure to null. According to the current value of i, it will loop to move the data and the memory information management structure in the memory from 0 to i.
[0044] S6_5_1 determines that i is not 0 and goes to S6_5_2;
[0045] S6_5_2 decrements i by one;
[0046] S6_5_3 defines a variable dstmem, and sets the value of this variable to the address addr of the memory to be applied for in the structure variable curinfo.
[0047] Define a variable srcmem to represent the data address to be copied. Define a variable srclen to represent the length of the data to be copied. The values of the variable srcmem and the variable srclen are respectively set to the memory information management structure struinfo pointed to by the structure pointer variable curinfo minus STRUINFO_LEN when the address addr of the memory to be applied for and the length length of the memory to be applied for.
[0048] S6_5_4 copies the data with srcmem as the starting address and srclen as the length to the space with dstmem as the starting address and srclen as the length.
[0049] S6_5_5 updates the value of the structure pointer variable curinfo
[0050] Set the value of ptr, which is the address of the memory pointer to be applied in the structure pointer variable curinfo, to the value of the address of the memory pointer to be applied in the memory information management structure struinfo pointed to by subtracting STRUINFO_LEN from the structure pointer variable curinfo, and update the data stored at the address pointed to by ptr in the updated curinfo to the current value minus the length length of the memory to be applied in the structure pointer variable curinfo; Set the address addr of the memory to be applied in the structure pointer variable curinfo to the value of the address addr of the memory to be applied in the memory information management structure struinfo pointed to by subtracting STRUINFO_LEN from the structure pointer variable curinfo minus the length length of the memory to be applied in curinfo.
[0051] Set the length length of the memory to be applied in the structure pointer variable curinfo to the length length of the memory to be applied in the memory information management structure struinfo pointed to by subtracting STRUINFO_LEN from the structure pointer variable curinfo.
[0052] S6_5_6 Update the value of curinfo to the value of curinfo minus STRUINFO_LEN.
[0053] S6_6 Update the information of the memory pool
[0054] Decrease the number cnt of the memory blocks already applied in the memory pool g_pool by one, and update the length len of the available memory in the memory pool g_pool to the value of len plus STRUINFO_LEN plus the value of freesize in S6_4.
[0055] Update the pointer memaddr of the currently available memory in the memory pool g_pool to the current value minus the value of freesize in S6_4, and update the information management address infoaddr of the applied space in the memory pool g_pool to the current value plus the value of STRUINFO_LEN.
[0056] S7 Open the critical section, make the system enter the kernel state, close the interrupt and scheduling operations, and the memory space release is completed.
[0057] The above are only the preferred embodiments of the present invention. It should be pointed out that for those of ordinary skill in the art of this technology, without departing from the technical principle of the present invention, several improvements and deformations can still be made, and these improvements and deformations should also be regarded as the protection scope of the present invention.
Claims
1. A small CPU memory management method, characterized in that, It includes the following steps: S1: Design a memory pool structure type strupool; S2: Define a memory pool g_pool variable in the CPU memory space; S3: Perform an initialization operation on the memory pool g_pool variable defined in S2; S4: Define a memory information management structure struinfo; S5: Perform a memory application operation; S6: Perform a release operation on the allocated memory; S7 Open the critical section, make the system enter the kernel state, close interrupts and scheduling, etc. The memory space release is completed; In step S1, the memory pool structure type strupool includes: ① A pointer memaddr to the currently available memory, of type unsigned char pointer, and this memory space is operated in a heap manner with the growth space upwards; ② The information management address infoaddr of the allocated space, of type unsigned char pointer, and this memory space is operated in a stack manner with the growth space downwards; ③ The number cnt of allocated memory blocks, of type unsigned char; ④ The length len of the available memory, of type unsigned short integer; ⑤ The memory pool space, which is managed by an array mempool, of type unsigned char, with a length of SIZE; In step S4, the structure struinfo includes a total of three member functions: ① The address ptr of the memory pointer to be allocated, of type void double pointer; ② The address addr of the memory to be allocated; ③ The length length of the memory to be allocated; In step S6, the release operation is performed according to the double pointer variable mclptr in S5; In step S6, the release operation includes the following sub-steps: S6_1: If the value of the number cnt of allocated memory blocks in g_pool is 0 or the double pointer variable mclptr is a null pointer at this time, directly return and prompt that the release operation fails; S6_2: Make the system enter the critical section, make the system enter the kernel state, without interrupts and scheduling, etc.; Define a pointer variable curinfo of the memory information management structure type in S4 to represent the information management data being processed, and set its initial value to the value of the information management address infoaddr of the allocated space in g_pool; S6_3: Define a variable i, starting from 0, traverse the memory pool, and judge whether the value of i is less than the number cnt of allocated memory blocks in g_pool; If the judgment is successful, go to S6_4, if the judgment fails, directly return and prompt an error; S6_4: Obtain the value of the address ptr of the memory pointer to be allocated in the structure curinfo in S6_2; Compare whether the value of ptr is equal to the value of mclptr. If they are equal, define a variable freesize of type unsigned short integer, assign it the length length of the memory to be allocated in curinfo, and go to S6_5; If they are not equal, first update the value of curinfo to the value of curinfo plus STRUINFO_LEN, increment the variable i defined in S6_3 by one, and then go to S6_3; S6_5 sets the address ptr of the memory pointer to be applied in the structure curinfo to null, and according to the current value of i, it will loop to move the data in the memory from 0 to i and the memory information management structure; S6_5_1 determines that i is not 0 and goes to S6_5_2; S6_5_2 performs a decrement operation on i; S6_5_3 defines a variable dstmem, and the value of this variable is set to the address addr of the memory to be applied in the structure variable curinfo; Define a variable srcmem to represent the data address to be copied; define a variable srclen to represent the length of the data to be copied; the values of the variable srcmem and the variable srclen are respectively set to the memory information management structure struinfo pointed to by the structure pointer variable curinfo minus STRUINFO_LEN, where the address addr of the memory to be applied and the length length of the memory to be applied; S6_5_4 copies the data with srcmem as the starting address and srclen as the length to the space with dstmem as the starting address and srclen as the length; S6_5_5 updates the value of the structure pointer variable curinfo; Set the value of the address ptr of the memory pointer to be applied in the structure pointer variable curinfo to the value of the address ptr of the memory pointer to be applied in the memory information management structure struinfo pointed to by the structure pointer variable curinfo minus STRUINFO_LEN, and update the data stored in the address pointed to by ptr in the updated curinfo to the current value minus the length length of the memory to be applied in the structure pointer variable curinfo; set the address addr of the memory to be applied in the structure pointer variable curinfo to the value of the address addr of the memory to be applied in the memory information management structure struinfo pointed to by the structure pointer variable curinfo minus STRUINFO_LEN minus the length length of the memory to be applied in curinfo; Set the length length of the memory to be applied in the structure pointer variable curinfo to the length length of the memory to be applied in the memory information management structure struinfo pointed to by the structure pointer variable curinfo minus STRUINFO_LEN; S6_5_6 updates the curinfo value to the curinfo value minus STRUINFO_LEN; S6_6 updates the information of the memory pool Decrement the number cnt of the memory blocks already applied in the memory pool g_pool, and update the length len of the available memory in the memory pool g_pool to the len value plus STRUINFO_LEN plus the freesize value in S6_4; Update the pointer `memaddr` of the current available memory in the memory pool `g_pool` to the current value minus the value of `freesize` in S6_4, and update the information management address `infoaddr` of the allocated space in the memory pool `g_pool` to the current value plus the value of `STRUINFO_LEN`.
2. The small CPU memory management method according to claim 1, characterized in that, In step S1, the memory pool space length `SIZE` is set to 1024.
3. The small CPU memory management method according to claim 1, characterized in that, In step S2, the memory pool variable `g_pool` is a static variable, and its variable type is the memory pool structure type `strupool` defined in S1.
4. The small CPU memory management method according to claim 3, characterized in that, In step S3, the initialization operations include: ① Set the pointer `memaddr` of the current available memory to the address of the array `mempool` in S1; ② Set the information management address `infoaddr` of the allocated space to the address of the last element of the array `mempool` in S1; ③ Set the number `cnt` of the allocated memory blocks to 0; ④ Set the length `len` of the available memory to the length `SIZE` of the array `mempool` in S1; ⑤ Set all values in the memory pool space to 0, and the operation method is to use the memory setting function `memset` for the operation.
5. The small CPU memory management method according to claim 4, characterized in that, In step S5, during the memory allocation operation, define a secondary pointer variable `mclptr`, store the allocated memory pointer in the secondary pointer variable `mclptr`, the allocated length is `size`, the type is unsigned short integer, and the length of this structure is set to `STRUINFO_LEN`, and the calculation method is calculated by `sizeof()`.
6. The small CPU memory management method according to claim 5, characterized in that, In step S5, the memory allocation operation includes the following steps: S5_1: First, determine whether `size` is greater than or equal to the length `SIZE` of the array `mempool` defined in S1; if so, directly return a null pointer, indicating that this allocation fails; if not, go to S5_2; S5_2: Make the system enter the critical section, make the system enter the kernel state, and perform operations such as disabling interrupts and scheduling; S5_3: Move the information management address `infoaddr` of the allocated space in `g_pool` downward by the length of `STRUINFO_LEN`, and the updated value of `infoaddr` is `infoaddr` minus `STRUINFO_LEN`; S5_4: Force the type conversion of the information management address `infoaddr` of the allocated space in `g_pool` to the memory information management structure type `struinfo` in S4 and perform the assignment operation. ① Set the value of `ptr` in this structure to the address of the pointer variable `mclptr`; ② Assign the address `addr` of the memory to be allocated in this structure to the value of `memaddr` in `g_pool`; ③ Assign the length `length` of the memory to be allocated in this structure to `size`; at this time, the value of `memaddr` in `g_pool` is the space to be allocated, and assign this value to `ptr`; S5_5: The value of memaddr in g_pool is offset upward by size; the updated value of memaddr is memaddr plus size; the value of len, the length of the available memory in g_pool, is updated to len minus size and then minus STRUINFO_LEN; the number of memory blocks already allocated in g_pool, cnt, is incremented by one.
Citation Information
Patent Citations
Quick-speed application EMS memory method
CN101140531A
Memory fragment management system and method
CN108536609A