STL reading notes (a)

  SGI space distributor have time distribution capabilities.

  The first stage configurator to malloc (), free (), realloc (), like C function performs the actual memory configuration, released, re-configuration operation, and to achieve a mechanism similar to the C ++ new handler, yes, it can not directly use C ++ new handler mechanism, because it is not to configure memory usage :: operator new.

  The so-called C ++ new handler mechanism, you can ask the system memory configuration when demand can not be met the requirements, you call a specified function, in other words, once the operator new :: unable to complete the task, the throw std :: bad alloc before the abnormal state, it will first call processing course specified by the client. The course of treatment is generally to be known new-handler. new handler approach to solve the problem of memory has a specific pattern.

  Note, SGI to malloc () instead of :: operator new to the configuration memory (a reason I can think of is historical factors, another reason why C ++ does not provide the equivalent of realloc () memory reset operation, and therefore can not be directly C ++ use of C ++ set_new_handler (), must emulate a similar set_malloc_handle ()).

  The first is a relatively simple configuration, similar to malloc C language ();

  The second stage configurator practice, if the area is large enough, more than 128Bytes, arranged on the transfer of the first stage processing. When a block is less than 128Bytes, places the memory cell (memory poll) management, this method is also known as sub-layers configuration: Every time a large memory configuration, and to maintain the correspondence of the free list (free list). Memory requirements next time if we have the same size, was taken directly from the free list. If the release of a small passenger terminal blocks, it recovered to the free list by the configurator, yes, do not forget, in addition to the configuration is responsible for configuring, also responsible for recycling. To facilitate the management, SGI second stage will take the initiative to configure multiple of 8 was adjusted to the small block in any memory requirements (e.g., the guest in claim 30Bytes, automatically adjusted to 32bytes) 16 and maintain free lists, each management size are small blocks of 8,16,24,32,40,48,56,64,72,80,88,96,104,112,120,128Bytes. Free list node structure is as follows:

  union obj{

    union obj* free_list_link;

    char client_data[1];

}


  You might think that, in order to maintain the list (lists), each node requires additional pointer (pointing to the next node), which in turn causes another without the additional burden it? Your concern is right, but already there is a good solution. Note that the above obj union is used, due to the union, therefore, the concept of the first field, may be regarded as a pointer obj, obj another point to the same form. View of the second field, obj may be considered a pointer to the actual block. A dual purpose result is not necessary in order to maintain the list pointer lock and create a waste of memory (we are trying to save memory overhead of it), this technique does not work in strong java converter languages, but in a non forced languages ​​such as C ++ is very common.

Guess you like

Origin blog.csdn.net/wujiafei_njgcxy/article/details/78314018