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

image.png

  • 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

image.png

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

image.png

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

image.png

The fourth memory request

image.png

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

image.png

The sixth memory request

image.png

The sixth memory request

image.png

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

image.png

Eighth memory request

image.png

Ninth memory request

image.png

Tenth memory request

image.png

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

image.png

Twelfth memory request

image.png

Doubts

image.png

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

TianSong
734 声望138 粉丝

阿里山神木的种子在3000年前已经埋下,今天不过是看到当年注定的结果,为了未来的自己,今天就埋下一颗好种子吧