STL Source Code Analysis--the bottom realization of Space Configurator (II.)

Source: Internet
Author: User

STL Source Analysis-Spatial Configurator

Space Configurator in the doorway

In the STL container all uses the unified Space Configurator, the Space Configurator is manages allocates the memory and destroys the memory. In the STL will create an object in the heap space is divided into two steps, the first is to request a piece of memory, the second is to initialize an object in this block of memory. The first application space is provided by malloc and is managed by constructor when initializing an object. Destroying an object is also done by two steps, the first is to destroy the object on the space, and the second is to release the memory.

At the same time, the STL's Space Configurator is divided into two levels of memory, if the requested memory space is greater than 128KB, then use the first-level space configuration, if less than, then remove a suitable block of memory from the idle list for use.

The name of the Space Configurator in STL is Alloc, with only two functions associated with this configurator allocate () and deallocate () functions, one is memory application, one is memory release, we don't care about the implementation of two functions, We only need to know the two functions of a corresponding memory of the request for a corresponding memory release. In use, we have provided a template package that provides both the overloads of the above functions:

Template <class T,class alloc>

Class simple_alloc{

Public:

statict* Allocate (size_t N)

{return 0==n?: (t*) alloc::allocate (n*sizeof (T));}

Static t* Allocate (void)

{return 0==n?: (t*) alloc::allocate (sizeof (T));}

Static void deallocate (t* p,size_t N)

{if (0! = N) (t*) Alloc::d eallocate (p,n*sizeof (T));}

Static void Allocate (t* p)

{Alloc::d eallocate (p,sizeof (T));}

};

After such a package, the alloc can be a space configurator in the STL, which can also be used in each container, and is also a static function, very convenient.

In addition to these two functions, there are also five functions that are always

Constructor () and Destroy () one is the construction of an object that is destroyed by read and write.

Uninitialized_copy ()

Uninitialized_file ()

Uninitialized_fill_n ()

These three functions are equivalent to the construction of the memory space, the above three functions are extracted, after the special processing, the efficiency is very high. The so-called extraction is to determine whether this type of its construction copy is a useful function, according to this interpretation either use constructor to construct, or use STL upper layer simpler method.

There are a total of seven functions, seven functions is the key to the space configuration, but also the upper layer of the spatial configuration of the external interface, in fact, the extraction function of the STL is very important, such as the extraction here can determine whether the use of the object requires a structure or need to memmove. Specificity is to int* char* these basic to the intensive care. The maximum possible efficiency is provided.

Understand that the constructor function in the space Configurator uses the placement new operation. This is another way to use it.

New (P) t (value), where p is a pointer to memory space and constructs an object of type T on the space that P points to. The new is used to include the memory of the application and initialization of the object, but there is another way to use the existing memory to construct the object

With the application and destruction of space, the construction and destruction of objects, as well as the replication of large numbers of objects, and the use of extraction and specificity in C + + to make the highest possible efficiency, it is these basic functions that make the upper operation simple and efficient.

STL Source Code Analysis--the bottom realization of Space Configurator (II.)

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.