Memory allocation method of embedded real-time operating system

By allocating independent memory space for each CPU core in an embedded real-time operating system, and combining multiple memory allocation algorithms, the problem of insufficient memory waste and flexibility is solved, efficient memory management is achieved, and the real-time performance of the system is improved and reliability.

CN119105863APending Publication Date: 2024-12-10EAST CHINA INST OF COMPUTING TECH
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411099988.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-12
Publication Date
2024-12-10

AI Technical Summary

Technical Problem

Traditional embedded real-time operating systems have problems of memory waste and insufficient flexibility in memory management. Static memory allocation causes the system to lose flexibility, while dynamic memory allocation may lead to uncertain response time and serious memory fragmentation.

Method used

Using a combination of multiple memory allocation algorithms, including allocating independent space for each CPU core in the MMU map, using a combination of static allocation and dynamic allocation, the BSS segment and core heap is managed through the pool allocator and TLSF algorithm, using partners The algorithm manages the user heap to achieve flexible allocation and recycling of memory.

Benefits of technology

Effectively reduces memory fragmentation rate, reduces memory waste, improves the efficiency of memory application and release, ensures the time certainty of memory allocation, improves the real-time and flexibility of the system, and provides security protection and improves reliability. sex.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119105863A_ABST
    Figure CN119105863A_ABST
Patent Text Reader

Abstract

The invention relates to a memory allocation method for an embedded real-time operating system, which is used for separating the memory space of each CPU (central processing unit) core of the embedded real-time operating system, and respectively mapping the space allocated to each core in MMU (memory management unit) mapping, and comprises the following steps of: calculating the address space sizes of a reserved area, a code segment, a data segment and a BSS (base station server) segment of each core; setting a core heap address size of each core; calculating a memory address space required by a running environment of each core, and realizing static distribution in a compiling and linking process; managing a BSS segment address space by adopting a pool allocator algorithm, and forming a memory pool for a core to use by adopting a TLSF distributor for a core heap address space; managing a user heap address space by adopting a partner algorithm; the problems of memory waste of an embedded system and poor flexibility of a whole embedded real-time operating system are solved, multiple memory allocation algorithms are comprehensively adopted, the memory allocation performance is improved, and the real-time performance of the system is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to the field of embedded operating system memory management, in particular to a method for memory allocation of an embedded real-time operating system. Background Art

[0002] Memory management is an indispensable part of the current operating system, and its design is also one of the most complex contents of the operating system. Memory management is mainly responsible for how to reasonably allocate memory space to meet the memory needs of the program when it is running. Most desktop operating systems use virtual memory, but traditional embedded real-time operating systems usually do not use this technology. Most of them achieve the purpose of memory management by combining static memory allocation with dynamic memory allocation, and some of them provide memory protection functions through MMU (Memory Management Unit). With the improvement of embedded processor performance and the emergence of new technologies such as embedded virtualization, the kernel of embedded real-time operating system will use MMU unit to realize virtual memory allocation management.

[0003] The memory management module in the kernel of an embedded real-time operating system is mainly to facilitate user use and improve memory utilization, and to realize functions such as memory space allocation and recovery, address conversion, storage protection and storage sharing, and memory space expansion. The principles of memory management in an embedded real-time operating system are minimum fragmentation, minimum management load, and certain allocation time. For code segments, BSS segments, and data segments such as interrupt vector tables and operating system images, the size of which can be determined during compilation and linking, embedded real-time operating systems generally use static allocation. Embedded real-time operating systems generally support dynamic memory allocation according to the needs of system operation. Whether to support dynamic allocation is mainly based on two considerations: first, the requirements of real-time and reliability, and second, the requirements of cost. For systems with extremely high requirements for real-time and reliability (hard real-time systems), delays or allocation failures are not allowed, and static allocation solutions must be adopted. The memory required when the program is compiled has been allocated. For example, embedded systems on avionics must adopt static allocation solutions. In such applications, cost is not a priority, and real-time and reliability must be guaranteed.

[0004] The commonly used memory allocation management algorithms in embedded operating systems include the buddy algorithm, the first adaptive algorithm (First-Fit), the best adaptive algorithm (Best-Fit), the TLSF algorithm (two-level Segregated Fit) and other algorithms.

[0005] The buddy algorithm uses specific data structures and operation rules to enable the algorithm to respond to memory allocation requests within a predictable time. Among them, the binary buddy algorithm is a typical representative of the buddy algorithm. The buddy algorithm is widely used. For example, the current Linux kernel uses the binary buddy algorithm to implement large-block memory allocation. The algorithm uses the data structure of the interval table to divide the entire memory space into different free blocks with exponential powers of 2 as the basic unit. The free blocks that are partners must be physically adjacent and equal in size. In the buddy algorithm, bit operations are used to quickly find free blocks that meet the request. The partners of the free blocks can also be obtained through address addition and subtraction operations for merging operations. The allocation and recovery time is bounded, and the algorithm has good real-time performance. However, since the memory blocks are divided by exponential powers of 2 each time, the internal fragmentation rate can reach 50% in the worst case, which is very unsuitable for small memory block (<1KB) requests.

[0006] The First-Fit algorithm is a very simple dynamic memory allocation algorithm. It organizes the free memory in the system into a bidirectional linked list, searches for memory blocks from the free list, and then starts searching from a high address. When the first free memory block that meets the allocation request is found, the required memory is allocated and the size of the free block is modified. The remaining part of the free block is still retained in the free list. When a new memory block is allocated from a large memory block, a space at the head of the new memory block (called the "block header") is needed to store information such as allocation, recycling, and merging operations. Therefore, the actual occupied memory size is the sum of the memory size of the allocation request and the block header overhead. When releasing memory, it determines whether the adjacent memory blocks are free based on the information in the block header. If the adjacent memory blocks are free, the memory blocks will be merged and the free block length will be modified; otherwise, the newly released memory will be inserted into the free list. With the frequent allocation and recycling of the system, a large amount of memory fragmentation will be caused.

[0007] The Best-Fit algorithm is similar to the First-Fit algorithm. It finds the smallest memory block that meets the memory allocation requirements from the beginning of the free area list, which can minimize the fragmentation in the memory allocation process. From the perspective of the generated memory fragmentation, this algorithm is the best strategy because it will produce the smallest fragmentation. However, since it will traverse all blocks each time, its efficiency is relatively low. In order to solve its fragmentation problem, the memory submitted to the requester each time will be greater than or equal to its request value, but this will lead to memory waste.

[0008] The TLSF algorithm is a dynamic memory management algorithm for embedded real-time systems proposed by M. Masmano et al. (reference TLSF: A new dynamic memory allocator for real-time systems) in 2004. It combines the advantages of the DLmalloc algorithm and the semi-adaptive algorithm, and has the characteristics of good real-time performance and low memory consumption. In order to achieve the determinism of the time taken to find a memory block of a specified size, the TLSF algorithm uses a two-level linked list to index memory blocks of different sizes. The first level index (First Level Index, referred to as FLI) divides the memory blocks into 2 n The value of n can be an integer between 4 and 31, which is determined by the largest free memory block. Each FLI category is further divided into 2 according to the second level index (SLI). SLI The SLI can be statically specified based on the needs of the programmer. Considering the speed of calculation, the value of SLI must be 2 to the power of m (m is a positive integer) and the value range is between 1 and 31, so that the size of the secondary index can be calculated by simple left and right shift instructions. For the convenience of calculation, SLI is expressed as the logarithm of the number of second-level indexes to 2.

[0009] The smallest block of TLSF can be 16B. In order to ensure that a certain allocated memory block is not accidentally used by other processes, it must be mapped to a certain process space by MMU. However, the hardware limit of MMU memory mapping is generally in KB. So when a process requests 16B from the system, it will actually be allocated a memory block in KB, which causes great waste. It can be seen that TLSF is not suitable for use as a global dynamic memory algorithm.

[0010] In order to meet the requirements of memory usage in traditional embedded operating systems, static memory allocators are often used to allocate the maximum memory space required by the application. However, the use of static allocation alone makes the system lose flexibility. The required memory must be known in advance and allocated in the design stage; all possible situations must be considered in advance in the design stage; memory allocation must be configured to the maximum according to the worst case, while only a small part of it may be used during actual operation. Such an allocation plan will lead to great waste; and it is impossible to flexibly add functions to the system without changing the hardware platform, making system upgrades difficult.

[0011] Dynamic memory allocation may cause uncertain response and execution time, severe memory fragmentation, and inability to allocate large blocks of memory due to frequent allocation / release processes. However, its implementation mechanism is flexible and brings great convenience to program implementation. In some application environments, dynamic memory allocation is even indispensable. For example, the network protocol stack used in embedded systems must support dynamic memory allocation in order to flexibly adjust the functions of the system and make trade-offs between various functions in the system under a specific platform. Summary of the invention

[0012] Aiming at the problem of memory waste in embedded systems and poor flexibility of the entire embedded real-time operating system, a memory allocation method for embedded real-time operating system is proposed. A variety of memory allocation algorithms are comprehensively adopted to improve memory allocation performance and improve the real-time performance of the system.

[0013] The technical solution of the present invention is:

[0014] A memory allocation method for an embedded real-time operating system separates the memory space of each CPU core of the embedded real-time operating system, and maps the space allocated to each core separately in the MMU mapping, so as to ensure that the operation and storage space of different CPU core images are independent of each other, so as to prevent memory damage caused by access conflicts, and adopts the following memory allocation method:

[0015] The first step is to calculate the address space size of the reserved area, code segment, data segment, and BSS segment of each core, and set the core heap address size of each core. Generally, the default setting is less than 512KB;

[0016] The second step is to calculate the memory address space M required by the operating environment of each core. M can be configured through the configuration file;

[0017] The third step is to statically allocate the memory address space M of each core and the memory address and size of the reserved area, code segment, and data segment in the core during the compilation and linking process of the operating system; the memory address layout starts from the low address of the memory and allocates the memory of cores 0 to n in sequence, where n is the number of CPU cores minus 1, and the memory address space size is M*(n+1); in the memory allocation space of each core, the memory space of the reserved area, code segment, data segment, BSS segment, and core heap is allocated in sequence starting from the low address;

[0018] Step 4: The pool allocater algorithm is used to manage the BSS segment address space in each core, and 16 free memory block queues are built to implement the application and recycling of fixed memory blocks. The memory blocks managed by each free queue have a fixed size and are all multiples of 8, ranging from 8 to 128 bytes, increasing in multiples of 8.

[0019] Step 5: For the core heap address space in each core, use the TLSF allocator to form a memory pool for the kernel to use;

[0020] Step 6: Use the buddy algorithm to manage the user heap address space. The user heap is a memory area prepared for users. Users can dynamically allocate and release memory through the malloc and free interfaces provided by the system.

[0021] To meet the needs of embedded applications, the allocated user heap memory can be organized into a unified memory partition to implement secondary memory allocation and release operations, so that an embedded application can use a relatively fixed memory area and improve the reliability of application operation.

[0022] Furthermore, the embedded real-time operating system kernel of a multi-core processor generally includes multi-core processor management, multi-core synchronization, multi-core scheduling, memory management and basic modules.

[0023] Furthermore, memory management provides read and write access control for each task to the memory. Through mapping management between physical memory and virtual memory, a memory management mechanism is formed to prevent the destruction of memory consistency due to incorrect memory operations during system operation, so as to achieve the purpose of early detection of faults, avoid fault propagation, and improve system reliability; incorrect memory operations include stack overflow, out-of-bounds operations on arrays, and operations on illegal pointers.

[0024] The beneficial effects of the present invention are:

[0025] The memory allocation method proposed in the present invention for the embedded real-time operating system of the embedded multi-core processor can reduce the memory fragmentation rate in the frequent allocation / release process, reduce memory waste, effectively improve the efficiency of kernel space memory application and release, ensure the time certainty of memory allocation, and improve the real-time performance and flexibility of the entire real-time operating system. At the same time, the core operating environment of each processor is allocated with an independent memory space, which does not interfere with each other, and the user-mode program cannot arbitrarily operate the kernel address space, which has a certain security protection effect and improves the reliability of the embedded real-time operating system. BRIEF DESCRIPTION OF THE DRAWINGS

[0026] Figure 1 It is the kernel diagram of the embedded real-time operating system of the present invention;

[0027] Figure 2 This is the memory allocation diagram of the system of the present invention. DETAILED DESCRIPTION

[0028] The present invention is described in detail below in conjunction with the accompanying drawings and specific embodiments. This embodiment is implemented based on the technical solution of the present invention, and provides a detailed implementation method and specific operation process, but the protection scope of the present invention is not limited to the following embodiments.

[0029] The embedded real-time operating system kernel for multi-core processors generally includes multi-core processor management, multi-core synchronization, multi-core scheduling, memory management and basic modules, such as Figure 1 shown.

[0030] Memory management provides read and write access control for each task to the memory. Through the mapping management between physical memory and virtual memory, a memory management mechanism is formed to prevent incorrect memory operations such as stack overflow, out-of-bounds operations on arrays, operations on illegal pointers, etc. from destroying memory consistency during system operation, so as to achieve the purpose of early fault detection, avoid fault propagation, and improve system reliability.

[0031] For embedded real-time operating systems with multi-core processors, in order to prevent different CPU cores from interfering with each other during operation, it is necessary to separate the memory space of each CPU core. In the MMU mapping, the space allocated to each core is mapped separately to ensure that the operation and storage space of different CPU core images are independent of each other to prevent memory corruption caused by access conflicts. Figure 2 The memory allocation method shown.

[0032] The first step is to calculate the address space size of each core's reserved area, code segment, data segment, BSS segment, etc., and set the core heap address size of each core (generally set to less than 512KB by default).

[0033] The second step is to calculate the memory address space M required by the operating environment of each core. M can be configured through the configuration file.

[0034] The third step is to statically allocate the memory address space M of each core and the memory addresses and sizes of the reserved area, code segment, data segment, etc. in the core during the compilation and linking process of the operating system. The memory address layout starts from the low address of the memory and allocates the memory of core 0 to core n (n is the number of CPU cores minus 1) in sequence. The memory address space size is M*(n+1); in the memory allocation space of each core, the memory space of the reserved area, code segment, data segment, BSS segment, and core heap is allocated in sequence from the low address, such as Figure 2 shown.

[0035] The fourth step is to use the pool allocater algorithm to manage the BSS segment address space in each core and build 16 free memory block queues to implement the application and recycling of fixed memory blocks. The memory blocks managed by each free queue have a fixed size and are all multiples of 8, ranging from 8 to 128 bytes, increasing by multiples of 8.

[0036] The fifth step is to use the TLSF allocator to form a memory pool for the kernel to use for the core heap address space in each core.

[0037] Step 6: Use the buddy algorithm to manage the user heap address space. The user heap is a memory area prepared for users. Users can dynamically allocate and release memory through the malloc and free interfaces provided by the system.

[0038] To meet the needs of embedded applications, the allocated user heap memory can be organized into a unified memory partition to implement memory secondary allocation, release and other operations, so that an embedded application can use a relatively fixed memory area and improve the reliability of application operation.

[0039] The above-mentioned embodiment only expresses one implementation mode of the present invention, and its description is relatively specific and detailed, but it cannot be understood as limiting the scope of the invention patent. It should be pointed out that for ordinary technicians in this field, several modifications and improvements can be made without departing from the concept of the present invention, which all belong to the protection scope of the present invention. Therefore, the protection scope of the patent of the present invention shall be based on the attached claims.

Claims

1. A memory allocation method for an embedded real-time operating system, characterized in that: Separate the memory space of each CPU core of the embedded real-time operating system, and map the space allocated for each core separately in the MMU mapping to ensure that the operation and storage space of different CPU core images are independent of each other to prevent memory corruption caused by access conflicts. The following memory allocation method is used: The first step is to calculate the address space size of the reserved area, code segment, data segment, and BSS segment of each core, and set the core heap address size of each core. Generally, the default setting is less than 512KB; The second step is to calculate the memory address space M required by the operating environment of each core. M can be configured through the configuration file; The third step is to statically allocate the memory address space M of each core and the memory address and size of the reserved area, code segment, and data segment in the core during the compilation and linking process of the operating system; the memory address layout starts from the low address of the memory and allocates the memory of cores 0 to n in sequence, where n is the number of CPU cores minus 1, and the memory address space size is M*(n+1); in the memory allocation space of each core, the memory space of the reserved area, code segment, data segment, BSS segment, and core heap is allocated in sequence starting from the low address; Step 4: The pool allocater algorithm is used to manage the BSS segment address space in each core, and 16 free memory block queues are built to implement the application and recycling of fixed memory blocks. The memory blocks managed by each free queue have a fixed size and are all multiples of 8, ranging from 8 to 128 bytes, increasing in multiples of 8. Step 5: For the core heap address space in each core, use the TLSF allocator to form a memory pool for the kernel to use; Step 6: Use the buddy algorithm to manage the user heap address space. The user heap is a memory area prepared for users. Users can dynamically allocate and release memory through the malloc and free interfaces provided by the system. To meet the needs of embedded applications, the allocated user heap memory can be organized into a unified memory partition to implement secondary memory allocation and release operations, so that an embedded application can use a relatively fixed memory area and improve the reliability of application operation.

2. The memory allocation method of an embedded real-time operating system according to claim 1, characterized in that: The embedded real-time operating system kernel of a multi-core processor generally includes multi-core processor management, multi-core synchronization, multi-core scheduling, memory management and basic modules.

3. The memory allocation method of an embedded real-time operating system according to claim 1, characterized in that: Memory management provides read and write access control for each task to the memory. Through the mapping management between physical memory and virtual memory, a memory management mechanism is formed to prevent the destruction of memory consistency due to incorrect memory operations during system operation, so as to achieve the purpose of early detection of faults, avoid fault propagation, and improve system reliability; incorrect memory operations include stack overflow, out-of-bounds operations on arrays, and operations on illegal pointers.

Citation Information

Cited By

  • Multi-core deployment method for AMP architecture operating system based on Loongson platform

    CN121879782A