STL primary / secondary space configurator

The fragments: 3 bytes required, the system allocates 4 bytes, 1 byte is the remaining fragments

External fragmentation: Due to application memory and return the memory so that memory leaves a lot of small fragments of memory to be used

Action:
(1) increase the rate of code reuse, modular functions.
(2) reduce memory fragmentation issues.
(3) improve the efficiency of memory allocation.
(4) There are measures to deal with the lack of memory.
(5) to hide the actual details of the allocation and release of storage space, ensure that all allocated storage space are finally released.
(5) consider multi-threaded state.

Considering the problems of small blocks of memory fragmentation may lead, set up two space configurator. They are: a spatial configuration, a secondary space configurator. When a block is greater than 128 bytes, a call space configurator; 128 bytes or less, in order to reduce overhead, with the bottom two more complex spatial configurator.

A space configurator

It is divided into three functions

Allocate space for the distribution, if the application fails, try to re-apply with oom_alloc.
Deallocate used to free up space.
Reallocate a need to adjust their space already exists according to adjustment if the application fails, try using oom_realloc application.

 

SGI two spatial configurator principle is: when a block is less than 128 bytes, places the memory pool (memory pool) management, user management space of a returned during recovery, similar to the hash bucket. Every time a memory configuration, and maintenance free list (free_list) corresponding. To facilitate the management, SGI secondary configuration are aligned to the 8 bytes. (Example: 30 bytes of space required, is automatically adjusted to 32 bytes). Maintenance 16 free_lists, the size of each management respectively
8,16,24,32,40,48,56,64,72,80,88,96,104,112,120,128 bytes.
Memory pool start position: start_free
memory pool end position: end_free
size of space required: n
memory pool size: heap_size = end_free-start_free.

 

Example of a two space configurator

In space applications explain an example of 12 bytes during two spatial configuration of the processing space applications ------- 12 bytes are transferred to the integer multiple of 8, i.e. 16.

       First, the free list corresponding to the inside to find (aligned to 16 bytes calculated by the corresponding position of the free list No. 1), corresponding to the position of the free list to see there is no space, if any, on the use of the head mount deleted manner on the free list the first space node assigned to the user, and then the rest of the space application space continues to mount up to the end, simple and efficient. But when the number 0 if there is no space in the free list, you need to apply for a memory pool inside, if you apply for a 16-byte memory space to the pool, the next application for 16-byte space in the free list is still no space, but also to the memory pool inside the application, in every case, a lot of trouble, so, when the memory space of the application pool, not once only apply for a 16-byte space, but more than a few 16-byte application space (for example 20), to apply a 16-byte space conveniently.

       The memory pool inside space applications, there are three cases, the first case: the memory cell is still enough space inside, 16-byte space 20 may also be dispensed, then the one which is assigned to the user, and the remaining 19 attached to the free list, the next 16 bytes of the application space can be taken directly in the free list. Second case: the space inside the memory pool is not very full, block 16 does not provide 20 bytes of space-time, but may be provided five 16 byte space (not necessarily 5, as long as more than 20 hours, greater than or equal 1 on the line), then, where it will be able to provide a space 5 is returned to the user, the remaining 4 attached to the free list; the third case: the pool of memory space has been very tight, If only eight bytes, even a 16-byte space is not provided, then this time the system needs to go towards the application space to fill memory pool, because the pointer is a _start and management of the memory pool _end only a continuous space on the tag, so, before the memory pool of eight bytes remaining space is required to mount it to the free list (i.e. free list 0) corresponding to, and then apply to the system One end of the memory space into the pool, and with _start space and mark this pointer _end manage. This goes back to the first case.

       Apply for a chunk of space to the system will be able to successfully apply for it? Not necessarily, if the space inside the system have also been inadequate application will fail this time, we need to larger than 16 bytes of space inside the free list management to cut back because of uncertainty that there is space in the free list, Therefore, the need to traverse the array free list management, there is a small detail when traversal, we apply the current 16-byte space corresponding to the free list is No. 1, No. 2 from the stands to reason that we should start looking for a free list, but because need to consider multi-threaded, (it is possible in our system or application to the memory pool space when another thread has been returned attached to the space and freedom No. 1 in the list), it is still No. 1 from the beginning of the free list traversal. When traversing to a free list (if No. 3 is the free list managed in the space), it would take a spatial resolution from the list consisting of 3 nodes down (the same is deleted first embodiment) placed in memory pool , and then apply for use in memory space application space on the OK

       If you have been behind all the free list are traversed over, but could not find a space. That is all the free list, memory pool, the system can not provide the space, it can be said to be a dead end. Space all go? Has been assigned to the user, then, can only be called a space configurator, because the space has been configured at the time was unable to allocate space in memory release calls the function set by the user, will you have unused space back to the system, so there You may also apply to space, if the user does not set the memory release function, then the result of the application of space can only be a failure (throwing an exception) up.

Reference links:

【1】https://blog.csdn.net/ZWE7616175/article/details/80559884

[2] https://blog.csdn.net/guaiguaihenguai/article/details/80598998

Guess you like

Origin www.cnblogs.com/lalalatianlalu/p/11779563.html