From the following successive steps, we can understand the basic process of alloc working. It includes concepts such as the readiness pool, the free list of up to 20 new blocks at a time, the amount of addition, the processing of fragments, and the processing when memory is insufficient.
Start
- The free_list array contains 16 elements, and the sub-elements manage free linked lists (single linked lists) with different memory sizes.
First memory request
- The client of the dispenser is the container, that is, the dispenser serves the container
- The memory size requested by malloc may be different, so you need to use cookie to record the size information for subsequent memory release actions
- The size of all elements of the container is known and consistent, so malloc can be omitted (cookies are not used)
- in alloc, it always stores the available memory in the readiness pool first, and then cuts the appropriate memory from the readiness pool to the linked list
Supplementary description
- The container applies for 32 bytes. Since the pool is empty, malloc ( contains cookie ) is used to request memory and successfully added 32 x 20 x 2 + RoundUp(0>>4) = 1280 bytes to the pool, cut from it 1 block is returned to the container, 19 blocks are given to list#3, and the remaining 640 bytes are reserved (combat preparation pool)
- War preparation pool: described by start_free and end_free pointers
RoundUp (0>>4) : 追加量,会越来越大
RoundUp : 是一个函数,将一个数值调整到 8 的边界
0 >> 4 : 0(目前的申请总量,初次值 0) / 16
无文档可说明为什么需要使用追加量,且每次追加量值是累计申请量除以16,或许为经验值
Second memory request
Supplementary description
- The smooth connecting line indicates that the memory blocks in the figure are continuous in address
- Right-angle connecting lines indicate that the memory blocks in the figure are continuous in the free linked list
The third memory request
The fourth memory request
Supplementary description
- After the above operations, 4 types of containers have been created in the code, each of which has a different size, and is managed by the 4 linked lists in the figure.
- Use malloc twice in total
The fifth memory request
The sixth memory request
The sixth memory request
Supplementary description
- After the above operations, 7 containers have been created in the code. The size of each container element is different, and they are managed by the 7 linked lists in the figure.
- Use malloc three times in total
- In particular, if multiple containers (vector, list, queue, deque) manage the same size of objects, they will share the same free linked list
Seventh memory request
Eighth memory request
Ninth memory request
Tenth memory request
Supplementary description
In the figure, free_list[8] and free_list[9] are dotted lines, indicating an empty linked list (pointing to null) and no memory block is available
- free_list[9] Because the memory block is backfilled in the reserve pool for use by alloc
- free_list[8] Return to the client because the only memory block is used
The eleventh memory request
Twelfth memory request
Doubts
Supplementary description
Review 1 Technical difficulties:
- How to find two or more nodes of the right size and adjacent memory in the free linked list to merge
- I don’t know the length of the linked list, especially the memory block is dynamically requested and recycled
- Review 2 Follow-up explanation
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。