STL: Two-level space Configurator

Source: Internet
Author: User

When we were writing programs, we needed memory, and our first response was malloc, but it was so easy to make the inner slices unusable.

The space adapter is mentioned in the STL, which is divided into two levels: the first-level space-appropriate configurator and the two-level space Configurator. The first-level space adapter is a simple wrapper over malloc, and its internal allocate () and reallocate () are called after malloc () and ReAlloc () are unsuccessful, and then Oom_malloc () and Oom_realloc () "Clean memory ", after repeated multiple times, if it is unsuccessful, call _throw_bad_alloc and throw out the Bad_alloc exception information.

Second-level Space Configurator:
The memory management of the secondary space Configurator reduces the memory fragmentation caused by the small two chunks, which is mainly: if the space to be requested is greater than 128 bytes, it is delivered directly to the space Configurator, and if it is less than 128 bytes, a two-level space Configurator is used to manage it with a free list of 16 elements. The size is mounted at each location (8, 16, 24, 32, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128 bytes), each time increasing the required memory to a multiple of 8.
Free_list its node structure as a common body:

union obj{    union obj * free_list_link;    char client_data[1];};

The second-level space Configurator structure is shown in:

Blocks in a free list:

This saves space while the work is done. Before the space is allocated, the space is stored in the address of the next node pointed to, when the space is allocated, let the corresponding my_free_list point to its next node, and then change his internal value to put into the user's data value,

Its memory allocation is mainly divided into the following four scenarios:
1, the required memory connected to the block and memory pool one of which is empty
(1), when the memory pool is empty, but the required space is increased to a multiple of 8 (in need of 13bytes space, we will assign it 16bytes size) corresponding to the free_list is not empty, it will be as shown directly from the corresponding free_list pull out.

if(n>__max_bytes)//required memory is greater than 128bytes, if greater than 128bytes is called and the Space configurator. {returnMalloc_alloc:: Allocate(n);}//If less than 128bytes, call the two-level space Configurator and find out when 16 free_list in aMy_free_list=Free_list+Freelist_index (n); Result= *My_free_list;//If empty in free_list, prepare the fill (fill function as in the following cases, explained in detail below)if(Result== 0{void *R=Refill (ROUND_UP (n));returnR;}//The corresponding free_list have free blocks to adjust the free_list, pulling out a node to be allocated. *My_free_list=Result -Free_list_link;returnResult

(2), when the corresponding free_list is empty, but the memory pool is not empty:
(a) First verify that the remaining space is sufficient for 20 node size (that is, the desired memory size (after Ascension) * 20), the 20 node size space is taken out directly from the memory pool, one is assigned to the user, and the other 19 blocks in the free list are hung under the corresponding free_list.

//The parameter Nobjs value in the Shunk_alloc () function is called.Template<BOOLThreadsintInst>Char* __default_alloc_template<threads, Inst>::chunk_alloc (size_t size,int&AMP;NOBJS) {Char*result; size_t total_bytes = size * NOBJS;total memory Size allocated in//memory poolsize_t bytes_left = End_free-start_free;amount of space left in//memory pool//Determine if the remaining space in the memory pool is sufficient for the total space required    if(Bytes_left >= total_bytes) {//If enough, change the starting pointer of the memory pool to point to the first address of the memory pool at this time and return the assigned first address of the space (that is, the original start address of the memory pool)result = Start_free; Start_free + = Total_bytes;returnResult After returning the assigned first address, take out a node to assign it to the user, the remaining +Nodes are hung in the corresponding free_list.

(b) If the size of the 20 node is not enough, then see if it can meet the size of 1 nodes, if enough, then directly take out a allocation to the user, and then allocate as many nodes as possible from the remaining space to hang in the corresponding free_list

elseifsize)     //判断内存池中剩余内存大小够不够一个节点空间{    size;  //计算内存池中剩余空间最多可以分配出多少个节点    size//计算出内存池一共需要分配出多大内存    result = start_free;        //记录分配出去空间的首地址    start_free += total_bytes;  //改变内存池起始地址的指向    return result;              //返回申请内存的首地址}

(c) If the memory of a node cannot be satisfied, the remaining space in the memory pool is hung in the corresponding free_list "Ken find the corresponding free_list, because each time the memory pool application space is a multiple of the application 8, the allocation is also in multiples of 8 units, So the remaining memory in the memory pool is definitely a multiple of 8 ", and then the memory pool is loaded for memory (the second case).

2, the memory pool is empty, the required space size of the connected block is also empty
At this point, the two-level space Configurator uses malloc () to request memory from the heap, but it requests a memory size of twice times the requested memory size (that is, 2 * required node memory size (promoted) * 20), after the successful request, half (that is, 20 node space) memory into the memory pool, Then from the remaining half of a node space allocated to the user, the remaining 19 space nodes are hung on the load corresponding free_list. "The reason that the two-level space Configurator requests 40 times times the required node size space is to avoid the hassle of constantly requesting memory from the heap, but the choice of 40 times times the node size is an empirical value for the compiler. 】

3. malloc did not succeed
In the second case, if malloc () fails, there is not enough space on the heap to allocate to us, and that is, the two-level space Configurator will search from one by one of free_list that are larger than the required node space, from any free_ that is larger than the required node space. A node is removed from the list for use.

4, lookup failure, call the first-level space Configurator
In the third case, if the search fails, stating that there is no free chunk in the free_list of his big one, the first-class space Configurator oom_allocate () is mobilized, and its principle is that the phone cleans up memory almost, it is the memory that can be reclaimed in the system, and then used by the two-level space adapter.
If Oom_allocate () also fails, it means the memory is exhausted, which is only returned "out_of_memory!" Pangkor

//If the remaining space in the memory pool is not sufficient for one node, the remaining space in the memory pool is first hung in the appropriate free_list in case the memory is lost, and then a 40 times-fold node space is requested from the heapsize_t Bytes_to_get =2* total_bytes + round_up (heap_size >>4);if(Bytes_left >0)//Determine if there is room left in the memory pool{//If there is, locate the remaining memory in the corresponding free_list, hang it under it,obj *volatile* My_free_list = free_list + freelist_index (bytes_left);    ((obj*) start_free)->free_list_link = *my_free_list; *my_free_list = (obj*) Start_free;}//Let the start pointer of the memory pool point to the first address of the space requested by the heartStart_free = (Char*)malloc(Bytes_to_get);if(0= = Start_free)//Determine if the application failed{//Condition 3:malloc () failure   //If the application fails    intI obj *volatile* My_free_list, *p;//From Free_list, which is larger than size one by one, to see if there are any chunks that have been allocated     for(i = size; i<=__max_bytes; i+=__align)         {my_free_list = free_list + freelist_index (i); p = *my_free_list;if(0! = P) {///Find, unplug a chunk to use, take out the size allocated to the user, the remaining as the memory pool space*my_free_list = p->free_list_link; Start_free = (Char*) p; End_free = Start_free + i;returnChunk_alloc (SIZE,NOBJS); }} section4Scenario: Find failed://Call the first-level space ConfiguratorEnd_free =0; Start_free = (Char*) Malloc_alloc::allocate (bytes_to_get);}//If there is an improvement in the out-of-memory condition. Happy, if not successful, throws an exception "out_of_memory!"Heap_size + = Bytes_to_get; End_free = Start_free + bytes_to_get;//Recursive call yourself, as much as possible to improve this situationreturnChunk_alloc (SIZE,NOBJS);

Space Reclamation Issues:
When the memory requested by the user from the two-level space Configurator is freed, the two-level space Configurator will recycle it and then insert the corresponding free_list, as shown in the following procedure:

staticvoid deallocate(void *p,size_t n){    obj *q = (obj *)p;    volatile * my_free_list;    //判断所释放内存是否大于128bytes    if(n > __MAX_BYTES)      {           //如果大于128bytes,则用一级空间配置器回收        malloc_alloc::deallocate(p,n);        return;    }    //如果小于128bytes,则使用二级空间配置器回收。    my_free_list = free_list + FREELIST_INDEX(n);    q->free_list_link = *my_free_list;    *my_free_list = q;}

This is the two-level space Configurator of the process, as well as the application process in various cases of the application of the solution to the method, because the main idea is summarized, so the code is more fragmented, if you need complete code, you can view the Sgi-stl Source Library.

Source: http://www.sgi.com/tech/stl/download.html

STL: Two-level space Configurator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.