Memory configurator (allocator) and stlallocator of the STL Library

Source: Internet
Author: User

Memory configurator (allocator) and stlallocator of the STL Library

I am learning. If there are any mistakes, please give me more advice. According to our constant understanding, we will make changes. The changes will be retained. recording errors is the greatest improvement. Sorry!

SGI space configurator with sub-configuration force (SGI is a version of STL, and there are other versions)

Here, I will not post the implementation of specific members and interfaces. The source code of STL can be found on the Internet.

In C ++, a new variable can be divided into two phases: 1. Allocate space 2. Call constructor; delete variable can also be divided into two steps: 1. Call destructor 2. Release Space

The alloc of SGI separates the two parts, so that space allocation and release and structure are called by different functions to differentiate their operations.

The alloc: alloclate () alloc: dealloclate () is used to construct and analyze the space. The alloc: construct () alloc: destory () function is responsible for the space allocation and release, they are in the header file

1 # include <stl_alloc.h> // responsible for memory space configuration and Release 2 # include <stl_construct.h> // responsible for object content Construction and Analysis. Both header files are included in # include <memory> medium

First, let's talk about the general idea of structure and structure.

    • Here we refer to the concept _ type_traits <T> (this is a template). First, we use value_type () to obtain the type of the object to which the iterator points, and then use _ type_traits to identify the type, whether the object is trivial (irrelevant) destructor. My understanding here is that when the iterator refers to an object of the POD (Plain Old Data) type (PS: the POD type can be understood as the traditional C language type, that is, the int type) does not need to call the destructor. Wait until the system automatically releases the space occupied by the int, however, when the type of the object referred to by the iterator is a string object (or another class object you write yourself), you cannot rely on the system. You need to call a dedicated destructor to analyze the structure. After determining the type, you can improve the work efficiency. (Destory () also features char * and other types)
    • The constructor adopts generic construction and new construction.
  • The following describes my understanding of space configuration and release.
    • C ++ relies on the following global functions for memory configuration: operator new () and: operator delete (). These two functions only play the role of allocating and releasing memory, the constructor and destructor are not called. They are equivalent to the malloc () and free () functions in C language. SGI is managing space with malloc () and free ().
    • In order to solve the problem of memory fragmentation (when malloc space is constantly found, enough space will be used for use, and memory fragments will inevitably be generated ), so we have the concept of a memory pool (the memory pool is implemented by the system to open up a large space to wait for use. When space is required, it is directly obtained from the memory pool, when the memory in the memory pool is not enough, open a new space in Heap and inject it into the memory pool)

Below is more important. The SGI space configurator uses two levels of configurator, namely the first level configurator and the second level configurator. The first level Configurator is the malloc () the second-level configurator adopts the memory pool concept;

First, let's talk about the first-level provisioner. The implementation of the first-level provisioner is the use of malloc () (because it is very important to say a few times). It will constantly try to open up memory, if there is no memory available for opening up, it will try to release the space and then try to open up the space, the first-level Configurator is more important to implement a new-handler mechanism similar to that in C ++ to handle insufficient memory.

The memory pool is implemented using the linked list structure (I personally think it is similar to the hash bucket method), instead of using the sequence table, which can better solve the problem of memory fragmentation.

Note that freelists here is a linked list!

Note! It is not clear here that freelists is a sequence table, and each unit is connected to this linked list. When the block is used by the client, it will dial out the block as it is, then change the pointer to the next node (that is, use the header to delete)

The block size managed by each linked list node is also different. The first node manages 8 bytes and the second node manages 16 bytes... Similarly, when the last node manages 128 bytes, the second-level configurator cannot process more than 128 bytes, you have to call the first-level provisioner (the first-level provisioner will also be called when the memory is insufficient, because the first-level provisioner contains a memory insufficiency processing routine)

The following describes the implementation of linked list nodes.

1 union obj2 {3     union obj *free_list_link;4     char client_data[1];//The client sees this;5 };   

The Consortium pointer inside is the pointer pointing to the next node. The char is the pointer pointing to the actual memory block. Using a consortium can reduce memory consumption without having to maintain a pointer pointing to the next node.

When the memory block referred to by a node is an idle block, obj is regarded as a pointer pointing to the next node. When the node has been allocated with memory, it is regarded as a pointer pointing to the actual block.

When the allocation function allocate () (the memory applying function in alloc) finds no available blocks, it will apply for a new block in the memory pool (call the chunk_alloc () function ), by default, 20 blocks are applied. When the available memory in the memory pool is less than 20 blocks, how many blocks are allocated? If the memory in one block is insufficient, by injecting memory into the memory pool (that is, opening up some memory pools), the system will traverse in free_lists to find the nodes allocated to the free linked list but in idle state by the memory pool, return the storage space of these nodes to a memory pool and use the chunk_alloc () function recursively to determine whether the space is sufficient for allocation. If it is unfortunate that it is not enough, pay attention to it !! The remaining small space in the memory pool will be allocated to the free linked list (that is, if you need to apply for a 16-byte node block, it is not enough, but the memory pool has 8 bytes of space, of course, you can't waste it. allocate it quickly.) then inject the memory into the memory pool (that is, open some memory pools ).

This is an example in STL source code analysis.

When the system heap memory is insufficient, chunk_malloc () will look for available memory and try to release some useless space, if it still cannot be found, call the first-level configurator because the first-level configurator has a memory insufficiency processing routine.

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.