Memory Management-STL space Configurator

Source: Internet
Author: User

The following describes the memory space configurator implemented in STL.

Key points:

1) Considering the memory fragmentation problem caused by small blocks, a dual-layer Configurator is designed.

When the configuration block exceeds bytes, the first-level Configurator is called. The first-level configurator directly uses malloc () and free ().

When the configuration block is less than bytes, the second-level Configurator is called and the complicated memory pool sorting method is used to reduce the additional overhead.

2) The memory pool management mode is to configure a large block of memory each time and maintain the corresponding free linked list.

If memory of the same size is allocated, memory blocks are directly allocated from the free linked list.

If the memory is released, it is recycled to the free linked list by the configurator.

The second-level configurator will take the initiative to adjust the memory demand for any small block to a multiple of 8.

3). The structure of the free-list node is as follows:

Using the feature of union, OBJ can be regarded as a pointer pointing to another OBJ in the same form from the first field.

In the second field, OBJ can be regarded as a pointer pointing to the actual block.

It is a waste of memory to maintain the pointer required by the linked list.

DetailsCodeFor implementation, see STL source code. The following figure is provided.

Relationship between Level 1 configurator and level 2 configurator:

When the second-level configurator allocates memory, the free linked list changes:

When the second-level configurator releases memory, the free linked list changes:

When the second-level configurator allocates memory, the specific steps are as follows:

1) Determine whether the memory block size is greater than 128 bytes. If the size is greater than bytes, call the first-level configurator. If the size is smaller than bytes, perform step 2 ).

2). select a suitable free linked list based on the memory block size from 16 free linked lists.

3) Determine whether the free linked list is empty. If it is empty, refresh the free linked list. Otherwise, perform step 4 ).

4). Adjust the current free linked list to point to a memory block and return the current memory block (similar to the delete operation of the linked list)

When the second-level configurator releases the memory, the specific steps are as follows:

1) Determine whether the memory block size is greater than 128 bytes. If the size is greater than bytes, call the first-level configurator. If the size is smaller than bytes, perform step 2 ).

2). select a suitable free linked list based on the memory block size from 16 free linked lists.

3). Adjust the current free linked list to reclaim the current memory block (similar to the insert operation of the linked list)

Actual Operation Result of the memory pool:

Memory Pool Management:

1). Three variables are used to identify the memory pool usage and size, start_free, end_free, heap_size.

2). When getting space from the memory pool to the free linked list, there are three main situations.

A) if the remaining space in the memory pool fully meets the demand, modify the value of start_free directly and return the corresponding block.

B). The remaining space in the memory pool cannot fully meet the demand. However, if one or more blocks are provided, the number of allocated blocks is reduced and then allocated.

C). If the remaining space in the memory pool cannot be provided for a block, use malloc to increase the memory space.

If malloc is successful, modify start_free, end_free, heap_size, call itself recursively, and re-allocate the block.

If malloc fails, search for the free linked list to see if there are enough blocks.

If yes, use the corresponding block as the memory pool, adjust start_free and end_free, call itself recursively, and re-allocate the block.

If it is not stored, the first-level Configurator is called.

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.