C + + standard library--stl Space Configurator

Source: Internet
Author: User

Statement: Source code and "STL Source Analysis" (Houtie)

Stl:

C + + Standard Template Library, high versatility.

Common data structures are encapsulated.

Provides common general-purpose algorithms.

STL six major components:

Container algorithm iterator Faux function (function object) Adapter Configurator

The role of the Space Configurator:

1. Improve the code reuse rate, the function of modularization.

2. Reduce memory fragmentation issues.

For example, we list is a chain structure, if the member is a new out, we know that the bottom implementation of new is malloc, but also must be clear, when malloc a piece of 40 bytes of space, the operating system in order to facilitate the search of this space will add a structure in this space, So the space opened up more than 40 bytes (should be 40+36,vs2013). Low space utilization.

3. Efficiency problem: The frequency of allocating a small block of memory, the efficiency is low.

4. Easy to cause memory leaks.

Unified management is more convenient.

5. Calling malloc/new each block of memory allocated to the system has some additional overhead.

6. There is also a reasonable response when space is insufficient.

First-Class Space Configurator There is a __malloc_alloc_template template class at the bottom of the source, which encapsulates some functions to cope with the lack of space.

 

7. Troubleshooting Some thread-safe issues

  I. The interior of the initial space Configurator is actually the encapsulation overloaded with the new operator.

The underlying source framework is dead, so you can only invoke related functions from the interface it gives.

For example, the allocate () function is used to allocate memory space, that is, to write dead, you can only use it.

  II. A two-level space Configurator is used to handle memory fragmentation issues. Use the concept of a memory pool.

When using the spatial configurator, there are the following procedures:

1. There is an enumeration variable __max_bytes (128) in the source code to distinguish the large memory block and the small memory block, the memory block which is larger than __max_bytes is the management category of the first-level space Configurator.

2. The bottom of the two-level space Configurator is managed by a free list (hash bucket). One of the enumeration variable __align values (8) is the interval that represents the hash bucket division.

3. The two-level space Configurator will have a corresponding algorithm to calculate the most appropriate memory block and number of memory blocks, and then query the hash bucket whether there is a corresponding memory block or memory block is sufficient, if not, ask the system to adjust the free list.

    

    How to invoke? The--refill () function.

4. When the allocate () function call finds no space in the free list, it calls the refill () function to repopulate the list. --Go find the memory pool.

    

    where does the new space come from? --Memory pool. This part of the work is given to Chunk_alloc (). Of course there is also the possibility that memory in the memory pool will not meet the requirements.

class uses two static pointers Start_free and End_free to identify a memory pool.

Chunk_alloc () uses End_free-start_free to calculate the amount of memory used to determine the memory pool:

If there is memory but not enough, give it to the first.

If there is memory and enough, just give no nonsense.

If the memory is not at all, you can only go back to malloc in the heap.

5. The last minute-if the true two-level space Configurator is not allocated a bit of memory, then it can only be found from the first-level space Configurator.

  Why is there no level two and one level?

Because there is insufficient space in the level of response measures, so is a failure, but also a first-class space Configurator to report the error.

  Doubts:

  Why is alignment round_up eight bytes instead of four bytes?

  Because we know that the content is a single-linked list, then there must be a pointer, so in order to conform to the 64-bit system, it must be 8 bytes.

  Do you use a first-level space Configurator or a two-level space Configurator by default?

Second-level space Configurator. The source code has a template class--simple_alloc.

There is a macro--__use_malloc, if the macro is defined, then it is a one-level space adapter.

If it is not defined, then the two-level space adapter is used.

This macro is the decision, whose alias is Alloc.

And Alloc is the member of the default class parameter in the STL standard library--

Template <class T,class Alloc = Alloc>

Class list{

...

};

  

   

The spatial Configurator is implemented using malloc, so what he defines is not an object (malloc does not call constructors).

  So how can the STL standard library Create a space when the object is built?

Using the positioning new expression, since a space p is opened, direct new (p) T1 (value) is available.

  So if we want to construct STL objects and destroy STL objects, divide the following sections:

1.get_node Get Space--construct construct object (position new)

2.destroy Destroy Object--put_node return space, how to return to see next question.

  The release of the second-level space configurator?

First, determine if it is greater than 128, if it is greater than the release of the first-level space Configurator, and if less than the free list is hung back.

  

We understand that construct and destroy are constructed and destroyed, so the copy construction, the whole copy, the specified number of copies, will have a definition--

i.e. Uninitialized_copy (), Uninitialized_fill (), Uninitialized_fill_n ().

See "STL Source Analysis", here is not detailed, its bottom is also a motion iterator on [first,last) traversal.

C + + standard library--stl Space Configurator

Related Article

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.