SGI second-level configurator _ default_alloc_template core memory allocation function chunk_alloc resolution

Source: Internet
Author: User

In fact, the question is a bit of an eye-catching, mainly to express the question of two points in the chunk_alloc function. I would like to ask you how to understand these two points.

The following describes the chunk_alloc code implementation, and describes the functions of the function and the functions of each code.

The memory configurator of SGI is divided into two layers: the first layer calls malloc directly, and the second layer is allocated by a memory pool. The second-layer Configurator is described as follows:

The second layer configurator first sets an array of free_list [16]. The array contains pointers pointing to a piece of memory. The pointer type is defined as follows:

Union obj {

Union obj * free_list_link;

Char client_data [1];

};

In short, the memory size of each subscript link in free_list is: 8B, 16B, 24B, 32B, 40B,..., and 128B. A total of 16.

Now, if you want to allocate a 32B memory, but the pointer to free_list [3] is NULL, that is, the idle linked list free_list does not have 32B memory, in this case, the following _ S_chunk_alloc should be used to allocate memory to free_list. By default, each allocation is to allocate 20 32-B memories.

That is, the second input value of _ S_chunk_alloc is 20, but the system may not be able to allocate 20, so reference is used. _ S_chunk_malloc:

1. The existing memory pool capacity meets your needs: 32B * 20, directly return such a large piece of memory;

2. The existing memory pool capacity cannot meet so many conditions, that is, 20 memory pools, but 1 memory pool can be satisfied. First assign a 32B value to the corresponding item of free_list;

3. If the current Memory Pool capacity cannot be met, you can only use malloc to allocate memory from the heap.

When allocating memory from the heap, first assign the remaining part of the current memory pool to free_list;

Then, modify the _ S_start_free and _ S_end_free pointers of the memory pool from the malloc memory in the heap. (These two pointers point to the start address and end address of the memory pool respectively ).

Then recursively call the _ S_chunk_malloc function.

 

My problem is: 1. Whether there is a problem when resetting the fragmented memory, as shown in the code below.

2. Check whether the code for modifying _ S_end_free is correct after the new memory is malloc.

All the code is as follows. My questions and explanations are explained below. Let's take a look at the code to understand this function, and then let's take a look at what went wrong with my ideas. Thank you!

Template <bool _ threads, int _ inst> char * _ default_alloc_template <__threads, _ inst >:: _ S_chunk_alloc (size_t _ size, int & _ nobjs) {char * _ result; size_t _ total_bytes = _ size * _ nobjs; size_t _ bytes_left = _ S_end_free-_ S_start_free; // if (_ bytes_left> = _ total_bytes) remaining space of the memory pool {// The remaining space of the memory pool fully meets the requirements _ result = _ S_start_free; _ S_start_free + = _ total_bytes; return (_ result);} else if (_ bytes_l Eft> = _ size) {// The remaining memory pool space cannot fully meet the requirements, but it is sufficient to supply more than one (inclusive) block _ nobjs = (int) (_ bytes_left/_ size); _ total_bytes = _ size * _ nobjs; _ result = _ S_start_free; _ S_start_free + = _ total_bytes; return (_ result );} else {// the size of the remaining space in the memory pool cannot be size_t _ bytes_to_get = 2 * _ total_bytes + _ S_round_up (_ S_heap_size> 4 ); // Try to make use of the left-over piece. // take advantage of the remaining small residual parts of the memory pool. If (_ bytes_left> 0) {_ Obj * _ STL_VOLATILE * _ my_free_list = _ S_free_list + _ S_freelist_index (_ bytes_left); // Is this correct? If _ bytes_left = 15, then _ S_freelist_index (15) = 1, // that is, the location of 16B, but actually only 15 B is left? (_ Obj *) _ S_start_free)-> _ M_free_list_link = * _ my_free_list; * _ my_free_list = (_ Obj *) _ S_start_free;} _ S_start_free = (char *) malloc (_ bytes_to_get); if (0 = _ S_start_free) {size_t _ I; _ Obj * _ STL_VOLATILE * _ my_free_list; _ Obj * _ p; // Try to make do with what we have. that can't // hurt. we do not try smaller requests, since that tends // to result in disaster on multi-process machines. for (_ I = _ size; _ I <= (size_t) _ MAX_BYTES; _ I + = (size_t) _ ALIGN) {_ my_free_list = _ S_free_list + _ S_freelist_index (_ I); _ p = * _ my_free_list; if (0! = _ P) {* _ my_free_list = _ p-> _ M_free_list_link; _ S_start_free = (char *) _ p; _ S_end_free = _ S_start_free + _ I; // are you sure the memory size of each block is _ I? Return (_ S_chunk_alloc (_ size, _ nobjs); // Any leftover piece will eventually make it to the // right free list .}} _ S_end_free = 0; // In case of exception. _ S_start_free = (char *) malloc_alloc: allocate (_ bytes_to_get); // This shoshould either throw an // exception or remedy the situation. thus we assume it // succeeded .} _ S_heap_size + = _ bytes_to_get; _ S_end_free = _ S_start_free + _ bytes_to_get; return (_ S_chunk_alloc (_ size, _ nobjs ));}}

My questions:

1. If _ bytes_left = 15B, then _ S_freelist_index (_ bytes_left) = 1 indicates that 15B should be placed in the item 16B in free_list. Is this good?

2. _ S_end_free = _ S_start_free + _ I; how can we determine that the size of the corresponding _ I item in free_list is _ I? Isn't there any capacity >__ I, but smaller than _ I + 8? In this case, does _ S_end_free fail?

 

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.