Welcome Reprint Opendevkit Article, article original address: http://www.opendevkit.com/?e=56
Dlmalloc is currently a very popular memory allocator, written by Doug Lea since 1987, and so far the latest version is 2.8.3, due to its high efficiency and other characteristics are widely used. The version of Dlmalloc used on U-boot is 2.6.6. Android Bionic C library is also used to implement the malloc allocation for C libraries. Android, source location bionic/libc/upstream-dlmalloc/malloc.c uboot source COMMON/DLMALLOC.C.
1. Basic Concepts
Chunk "block", in effect, refers to a piece of memory, including management information.
Bins "cabinet", the Dlmalloc algorithm, is used to quickly locate the required chunk
#define TOP (bin_at (0)->FD)//The topmost chunk */special bin, pointing to the top of the memory chunk
#define Last_remainder (Bin_at (1))/* Remainder from the last split */special bin, pointing to the most recently assigned chunk remaining chunk.
SBRK "System call" to move the boundaries of the heap seen by user space
Call_morecore is actually the implementation of operating system allocation calls on different platforms, and Linux is brk.b
2. Algorithmic logic
(1) Chunk management
There are two types of chunk, the allocated chunk and the unassigned chunk, which are interleaved and occupy the entire heap space. Note that there are no adjacent two unassigned chunk, because Dlmalloc merges any adjacent idle chunk when you call free to release the chunk that was used. The staggered two chunk look like this.
(2) Top Chunk
Top is originally called the wilderness chunk, which points to the chunk of the top end of the dlmalloc available memory, because on the boundary, top is the only block that can be arbitrarily expanded. Top is special, it is not managed by any sub-bins and will only be used when there are no chunk available in the other sub-bins. When Dlmalloc initialization is complete, the entire memory managed by Dlmalloc is a chunk,top that points to the chunk.
(3) Last_remainder Chunk
Last_remainder always points to the remaining part of the recently split chunk. If malloc does not find a "exact match" block at the time of allocation, it takes precedence to see if the last_remainder is sufficient. In terms of locality, the code that continuously applies for allocating memory tends to have a common life cycle, and the chunk that they release have a great chance of merging into a large chunk.
3. Algorithm overview
(1) Find in Smapllbin, this search is quick and there is a return
(2) Look in the tree bin, this lookup is slightly slower, there is a return
(3) in the large block chain in order to find, this slowest, there is the return
(4) View top chunk, you can divide the words, the decomposition of the top chunk
(5) Sys_alloc, Sys_alloc Linux is a BRK system call.
MEM = Sys_alloc (GM, NB), GM is the global static struct Malloc_state _gm_, which manages this entire malloc state.
In the Sys_alloc, the initialization is included, and if it is the first assignment, it is initialized, assigning the memory block to top and assigning the user the desired size from top. The Init_top interface is used to initialize top.
(6) When Free is released, it merges adjacent chunk and, if appropriate, calls BRK, allowing the kernel to release the page.