Small memory management algorithm of Rt-thread kernel

Source: Internet
Author: User

First, dynamic memory management

Dynamic memory management is a real heap memory management module that allocates memory blocks of any size, depending on the needs of the user, in the case of current resource satisfaction. When users do not need to use these memory blocks again, they can be released back into the heap for use by other apps. In order to meet different requirements, Rt-thread system provides two different dynamic memory management algorithms, namely small memory management algorithm and slab memory management algorithm. Small heap memory management module is mainly for system resources less, generally used in less than 2 m memory space of the system, while the slab memory management module is mainly in the system resources are rich, provides a approximate multi-memory pool management algorithm fast algorithm.

two memory management modules can only select one of these when the system is running or do not use the dynamic heap memory manager at all. Both of these management modules provide the same API interface. because the dynamic memory manager wants to meet the security allocations in multi-threaded situations, it considers the mutex problem between multiple threads, so do not allocate or release dynamic blocks of memory in the interrupt service routine. Because it may cause the current context to be suspended waiting.

Second, small memory management algorithm

This paper mainly introduces the small memory management algorithm, and the slab memory management algorithm is introduced in the following article. The small memory management algorithm is a simple memory allocation algorithm. Initially, it is a piece of large memory. When a memory block is allocated, the matching block of memory is split from the large block of memory, and then the partitioned free memory blocks are returned to the heap management system. Each memory block contains a managed data header, which links the use of blocks to the free block in a doubly linked list, as shown in the memory block chain table diagram:

Memory management in the performance is mainly reflected in the allocation and release of memory, small memory management algorithm can be shown in the following examples.

The idle list pointer Lfree initially points to a 32-byte block of memory. When the user thread allocates a 64-byte block of memory, but the memory block that the Lfree pointer points to is only 32 bytes and does not meet the requirements, the memory manager continues to look for the next block of memory, and when it finds the next block of memory, 128 bytes, it satisfies the allocation requirement. Because the memory block is larger, the allocator will split the memory block and the remaining memory blocks (52 bytes) remain in the Lfree linked list. In addition, a 12-byte header is set aside each time a memory block is allocated for magic,used information and list node usage. The address returned to the app is actually the address of the memory block after 12 bytes, and the previous 12-byte header is the part that the user should never touch. (Note: The length of the 12-byte header differs from the system alignment difference). The reverse process is released, but the allocator looks at whether the contiguous block of memory is idle, and if it is free, merges into a large free block of memory.

Data structure: in SRC/MEM.C

#define Heap_magic 0x1ea0struct  heap_mem{    /**/    rt_uint16_t MAGIC ; // If this memory block is assigned, the 0X1EA0 is used to mark this memory block as a memory block for memory management, and also a memory-protected word: If this area is rewritten, it means that this block of memory is being illegally rewritten (under normal circumstances only                          There is a memory manager to touch this memory)    rt_uint16_t used; // 0: Unassigned; 1: Allocated       rt_size_t Next, prev; // previous memory block, after a memory block };

Three, small memory management algorithm function interface: in the SRC/MEM.C

Initialize the dynamic memory heap:voidRt_system_heap_init (void*BEGIN_ADDR,void*end_addr); When using heap memory rt_using_heap, it is necessary to initialize the heap memory when the system is initialized. This function uses the memory space of the parameter Begin_addr,end_addr area as the memory heap. The source code shows that the small memory management algorithm initializes the dynamic heap memory to two memory blocks by passing in the start address and end address: The first memory block points to the dynamic heap memory header address, the free space is the entire assignable memory (not including the two memory control block itself, minus 24 bytes), The next pointer to this memory block points to the end memory control block, the second memory block points to the last memory control block, the free space size is 0, and this memory block the previous pointer and the next pointer point to itself. allocating memory Blocks:void*Rt_malloc (rt_size_t size), Rt_malloc function will find the appropriate size of memory block from the system heap space, and then return the memory block available address to the user. To redistribute memory blocks:void*rt_realloc (void*Rmem, rt_size_t newsize); The size of the memory block (increase or decrease) is reallocated on the basis of the allocated memory block, and when the memory block is redistributed, the original memory block data remains the same (shrinking, the subsequent data is automatically truncated).
The code shows that if the current memory block available memory is more abundant, will be divided into two pieces, the latter block will try to merge with the front and back memory blocks. allocate multiple blocks of memory:void*Rt_calloc (rt_size_t count, rt_size_t size), multiple memory blocks that allocate contiguous memory addresses from the memory heap, the returned pointer points to the address of the first memory block, and all allocated memory blocks are initialized to 0. Free Memory:voidRt_free (void*Rmem); When the user thread finishes using the memory requested from the memory allocator, it must be released in a timely manner, otherwise it will cause a memory leak, and the Rt_free function will swap the memory to be freed back to the heap manager. When this function is called, the user passes a pointer to the memory block to be freed and returns if it is a null pointer.
Memory Merge: Static void plug_holes (struct heap_mem *mem); This function is called when memory is redistributed, and the latter part of the split is tried to merge with the front and back memory blocks, and when the memory is freed, the The algorithm checks the previous memory block and the next memory block for the memory to be freed, and merges if it is idle. 

Iv. Summary of the algorithm

The small memory management algorithm, in general, is to initialize a piece of memory to a static linked list to achieve, initialize only two memory blocks: the first block in addition to the memory block control block (occupies 12 bytes), but also contains the space to be allocated, this space is mem_size_aligned, It refers to the total size of the dynamic heap memory that this algorithm can allocate, any memory to be allocated cannot be larger than it, or exceeds the limit ; the second block contains only the memory control block itself (12 bytes), does not contain the space to allocate, it acts as the end of the chain, and set the use flag used to 1 (permanent use).

The entire algorithm also has a free pointer lfree, which points to the free memory, which points to the first inner block when initialized. The next step is allocating memory. Allocates memory first from the node that the free memory points to start scanning, once scanned to the size satisfies the node, then returns this node, if this node point to the space is large enough to have enough space to allocate another space, the partition this node points to the memory is two blocks, the previous memory returns, the latter memory into the idle list; When memory is freed, the algorithm checks the previous memory block and the next block of memory for the memory to be freed, and merges if it is idle.

Small memory management algorithm of Rt-thread kernel

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.