STL - Notes from the Space Configurator

What is a space configurator? ? ? ?
The space configurator is one of the six components of STL, and it is a component that provides configuration space for the container. (Containers such as vector, list, map, etc. will not write a separate blog as a basis)

So before talking about the space adapter, we need to understand such preparatory knowledge
. The new operation of c++ is essentially divided into two steps: 1. Applying for memory 2. Calling the constructor
delete is also two steps: 1. Calling the destructor 2. Release RAM

Then, in order to divide labor closely, STL
separates puts them in the stl_construct.h header file and the stl_alloc.h header file respectively.
In the header file of stl_alloc.h, there is a space configurator that we care about.



Features of the space configurator The
space configurator is divided into a first-level space configurator and a second-level space configurator. The first-
level space configurator encapsulates the malloc and free functions, which are the application and release of large memory blocks.
The secondary space configurator uses the memory pool strategy. It is a management method for small memory. The following describes the characteristics of the space configurator in detail through the process.



What is the process for using the space configurator? ? ? ?
First, according to the size of the configuration area, select the allocation strategy,
if: when the configuration area exceeds 128bytes, call the first-level space configurator.
The first-level space configurator first calls malloc. If it cannot be allocated, it calls oom_malloc. oom_malloc first checks whether the "insufficient memory processing routine" is enabled. An exception occurs, or the program is terminated with exit(1).

If: When the configuration area is less than 128bytes, the secondary space adapter is called.
The secondary space adapter maintains a 16-length array, which stores the free_lists pointer. That is to say, it points to an array of 16 free linked lists. The size of the free linked list ranges from 8 to 128 bytes. If the required free configuration space is used in the free_lists linked list, it is directly taken away. If not, it needs to be filled from the pool (memory pool).
The memory pool records its size with start _ free and end _ free, and is used to save the blocks that are not linked to the free list.
Assume that the user needs n blocks, and there are no free linked lists, so the system will apply to the memory pool for the required configuration space with 8-byte alignment * 20 memory blocks.
If memory pool is larger than the above required size, it will be taken directly from the memory pool.
If the memory pool is smaller than the size required above, but is larger than a required configuration space and is aligned with 8 bytes, then the maximum number of blocks that can be allocated in memory is given to the free linked list.
If the memory pool cannot provide even the size of a block , then first hang the remaining fractions of the memory pool on the free linked list, and then apply for space to the system heap. If the application is successful, it will be returned. If the application fails, it will go to its own free linked list to see if there are any available blocks to return. The free linked list is gone, and finally the first-level configurator will be called




Why use a space configurator? ? ? ?
This problem is also to realize the advantages of the space configurator.
1. Solve the problem of external memory fragmentation.
2. Consider the countermeasures for insufficient memory
3. Separate the construction of the object from the application space operation. In some scenarios, the number of CPU operations can be reduced.
4. The overhead caused by frequent calls to malloc and free (the usage scenario of the multi-level space configurator)



Any well-written code or tool always has advantages, is there no disadvantage? ? ?
I think there is, whether it is the memory management strategy of the operating system (by page), the memory allocation strategy of ptmalloc, or the pool strategy of the secondary configurator of the space configurator of stl, which greatly reduces external fragmentation, But internal debris is also difficult to avoid. . . . . .



Think new ones will be added. . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325774265&siteId=291194637