[C + + memory management] 8_ G2. 9 overview of STD:: alloc operation mode

Time:2022-5-6

summary

  • free_ The list array contains a total of [0 – 15] 16 elements, each of which corresponds to a childsingleLinked list (embedded pointers organization management)
  • Each sub linked list manages multiple memory blocks (the size of memory blocks ranges from 8 bytes to 128 bytes). For example, free_ List [0] manages the 8-byte linked list, and then the space managed by the sub linked list increases by 8 bytes in turn
  • When the memory space requested by the user is not an integer multiple of 8, the size will be adjusted to an integer multiple of 8, and then managed with the corresponding sub linked list
  • When the user requests more than 128 bytes of memory, malloc is called to allocate the management memory (malloc means cookie)

Examples

[C + + memory management] 8_ G2. 9 overview of STD:: alloc operation mode

first application

  • The customer applies for 30 bytes of space, and STD:: alloc adjusts the alignment of 8 bytes to 32 bytes, corresponding to free_ List [3] sub linked list
  • free_ List [3] is empty and does not point to the available memory block node. Call malloc to apply for large memory blocks (including cookies)

    • Memory space obtained when malloc applies: 20 x 32 bytes memory block + 20 x 32 bytesCombat readiness poolMemory blocks (20 memory blocks may be empirical values, which are organized into a single linked list by embedded pointers)
  • Give the first small memory block obtained from the application to the customer for use

Second application

  • The customer applies for 72 bytes of space, and ATD:: alloc is in free_ The 20 x 32 bytes of war readiness pool space previously applied for is found in the 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 requests 96 bytes of space, and STD:: alloc checks free_ The list has no war readiness pool memory available, so it is free_ The list [11] sub linked list calls malloc to apply for large memory blocks [20 x 96 + 20 x 96 (corresponding to the memory pool in the figure)] and passes the first memory block to the customer for use

Memory recycling

  • According to the size of the recovered memory, re mount it 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

[C + + memory management] 8_ G2. 9 overview of STD:: alloc operation mode

union obj {
    union obj *free_list_link;
    char client_ data[1];       //  Client is not used in the source code_ Data, negligible understanding
}
  • Embedded pointer, borrowing the first 4 bytes of the memory block to manage the available memory to form a single linked list [no need to occupy additional space, indirectly indicating that obj must be ≥ 4 bytes (address width) to be borrowed by the pointer]
  • Each time the container dynamically allocates a pointer, it does not know whether the memory has cookies (that is, the internal memory management method does not affect the use of customers)