Build--sgi-stl space Configurator from wheels

Source: Internet
Author: User
Introduction

People often say, do not rebuild from the wheel, to stand on the shoulders of giants. In the face of these STL components that play the role of the wheel, is it necessary to delve into the design principles or implementation details? The answer varies from person to person. From an application perspective, you do not need to explore the implementation details (however, to a considerable extent, understand the underlying implementation of the practical use of the absolute help). From the point of view of technical research and essential ascension, the details can give you a thorough grasp of everything: not to relive data structures and algorithms, or to play the role of the wheel, or to further expand the wheels of others, can be a solid foundation for this.

The world's great things must be done in detail.

                                                                                                                                                                                         -- Excerpt from "STL Source analysis" HouJie

Read the Houtie master source analysis of a book, deep and profound STL. The design idea of the space Configurator is more shocking. The quickest way to make progress is to imitate it first. Therefore, it is decided to analyze the principle of the thorough STL space Configurator and realize it. Design Philosophy

I chose the SGI version of the STL, later also are directly called STL, do not distinguish. This version of the philosophy of design is: application space to System heap consider multithreaded state (I'm only concerned about the principle of space configuration, not care about this) considering the contingency measures in the case of insufficient memory consider too much "small chunks" may cause memory fragmentation problems (this is an external fragment, STL introduces the internal fragmentation problem, said later) Outer Fragment

First, the introduction of STL from outer fragments: Frequent application and release of small chunks on the heap can cause the problem of external debris. So what happens when the debris is out.

Assume 60B of memory, first apply 20B, and then apply for 16B, and then apply for 20B, the final release of the space 16B. As shown in figure:


Now the 16B has been released, the total remaining 20B space, but now want to allocate 17B space is not distributed, because discontinuous. This obviously, but because the discontinuous and unable to allocate the memory of the situation is called the outer fragments. First- level space Configurator

The outer fragment is due to frequent application and release of Small block of memory, the application of large chunks of memory will not have this problem, so for large memory applications, STL call the first-level space configurator to allocate.

The STL considers a block of memory larger than 128 bytes to be large. First-level space Configurator is not difficult, it simply encapsulates the malloc and free, and simulates the implementation of C + + operator new Set_new_handler (specific introduction can see "effective C + +" book). What is Set_new_handdler? is the internal maintenance of a function pointer, when the malloc request memory failure, and do not want to return directly, but want to try to call the other way to see if there is any way to release a part of the memory, so that malloc success, This method is set by using this handler pointer, which is NULL initially. So the first-level space Configurator does that when the malloc fails and the handler function is not NULL, the loop calls the function and then malloc until it succeeds to return the requested memory address. If the handler function is Null,malloc and fails, then a Bad_alloc exception is thrown.

But no one uses it, because there is no effective way to free up memory. Level Two Space Configurator

It maintains a pool of memory and a free list:


Initially, memory pool memory is obtained by malloc, is a contiguous chunk of memory, and has a _start_free identity beginning, _end_free identifies the last memory unit next.

Free list is empty at the beginning, it is 8,16,24,...,128 from left to right, and the memory block with corresponding size is hanging underneath. If the requested memory is 1~8 byte size, then the 8 byte size is uniformly allocated. For example, to request a size of 4 bytes, you would first go up to the minimum multiple of 8, and then take the memory block back (the allocated memory block is an integer multiple of 8). There are 4 bytes wasted here, which is called an inner fragment.

Previously said, more than 128 bytes to find a first-level space configurator allocation, less than or equal to 128 bytes of the first found free linked list. Set the size of the requested memory, if the size is greater than 128, then find a level of space configurator allocation, if size is less than or equal to 128, and the following:

Find the size in the free list of the corresponding subscript, see whether the facet of this position is hanging memory, if there is, then the head Delete, remove the first block return; if there is no block of memory in the current list, then call the refill function.

The refill function is to fill the free list, first calculate 20 times times the size, and allocate the size of 20 objects (why 20 times times). Because if you have just applied for such a large chunk of memory, you may soon have to apply for the same size of memory block, so for efficiency, the size of the 20 objects to be hung on the linked list for a rainy time, and then from the memory pool:

If the memory pool is left with enough memory to allocate the size of 20 objects, take out such a large chunk of memory, cut to 20 pieces, the first one returns, and the remaining 19 blocks are then hung to the corresponding position of the free link table;

If the memory pool memory is not enough to allocate the size of 20 objects, but at least one, then can apply how many applications, the first return, the remaining hanging to the free list corresponding to the position below;

If the memory pool's remaining memory is not allocated to the size of an object, call malloc to the operating system. But before calling malloc, you have to put the remaining memory of the memory pool on the free list corresponding position.

Look for operating system malloc, but the operating system does not necessarily have memory, but can not easily give up, there is no memory block below the current list location, but higher places may be, and then to the free chain table in the direction of the big, if there is, then the head Delete, take out the first block, as a new memory pool, and then the memory pool has memory, The next logical repetition is good, but this time it will be able to get memory from the memory pool.

But if there is no memory behind the free list, it is really the end of the rope, but still can not give up lightly. Don't forget, we still have a level of space configurator. However, the malloc has not been available for memory, find a level of space Configurator What is the use of it. This is because it is possible to set the handler function in a primary space configurator, so you can only catch the last straw, even if you do not set the handler function, you should leave the exception.
Source Code

Source code hosted to the github above, open source sharing: https://github.com/Fireplusplus/Data-Structure/blob/master/my_allocator.h test

Write a function to test, the code is also attached to the top, where the test results are posted:


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.