Overview
- ] 16 elements in total, each of which corresponds to a single single 160acbc9b4fa0b linked list (embedded pointers organization management)
- Each sub-linked list corresponds to manage multiple memory blocks (the size of the memory block ranges from 8 bytes to 128 bytes). For example, free_list[0] manages an 8-byte linked list, and then the size of the space managed by the sub-linked list increases by 8 bytes
- When the user applies for memory space that is not an integer multiple of 8, the size will be adjusted to an integer multiple of 8, and then the corresponding sub-linked list will be used for management
- When the user applies for memory greater than 128 bytes, malloc is called to manage memory allocation (malloc means cookie)
Example
First application
- The customer applies for 30 bytes of space, std::alloc adjusts 8 bytes to 32 bytes, corresponding to the free_list[3] sub-linked list
free_list[3] is empty, does not point to the node of the available memory block, call malloc to apply for a large memory block (including cookie)
- Memory space obtained during malloc application: 20 x 32 bytes of memory block + 20 x 32 bytes of pool memory block (20 memory blocks may be empirical values, organized by embedded pointers into a singly linked list)
- Give the first small memory block obtained by the application to the customer for use
Second application
- The customer applies for 72 bytes of space, atd::alloc finds the previously requested 20 x 32 byte combat readiness pool space available in free_list[3], this space is re-divided by 72 bytes, and the first block is passed to the customer for use
Third application
- The customer applies for 96 bytes of space, std::alloc checks that the free_list non-competitive pool memory is available, so call malloc for the free_list[11] sub-list to apply for a large memory block [20 x 96 + 20 x 96 (corresponding to the memory pool in the figure) 】, pass the first memory block to the customer for use
Memory reclamation
- According to the recovered memory size, re-mount to the corresponding sub-linked list (embedded pointers organization management)
embedded pointers
The figure below corresponds to a sub-linked list in the figure above
union obj {
union obj *free_list_link;
char client_data[1]; // 源码中并未使用 client_data, 可忽略理解
}
- Embedded pointer, use the first 4 bytes of the memory block to manage the available memory to form a singly linked list [No need to occupy additional space, indirectly means that obj must be ≥ 4 bytes (address width) to be borrowed by the pointer]
- What the container dynamically allocates every time is a pointer, and it is unknown whether this memory contains cookies (that is, the internal memory management method does not affect the use of customers)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。