STL source code analysis (1)

  Recently watched "STL source code analysis," Hou Jie teacher to see the first pass when confused, repeatedly Duokanjibian, it seems to understand some of it. Therefore learned to do a record, it can be considered record their own learning process. This series of blog mainly recorded some of the macro understanding of things, or to implement specific code carefully taste the original book.

Overview

  I.e. STL C ++ Standard Template Library, mainly composed of six components, namely: a dispenser, the container, iterators, algorithms, functor adapter. This interactive relationship between the performance of the six components are: data storage vessel made by the distributor, the algorithm to access the contents of the container via iterators, functors algorithm can help complete different strategy changes, modified or socket adapter can functor , iterators, containers and the like.
  Before introducing the main components, first look at object-oriented and generic programming, object-oriented programming (Object-Oriented programming, referred OOP) attempts to put together the data and methods for processing data, for example, in C ++ classes , generally have member variables and member variables of the process member function, create an object when you want to use, and then object to call them. And generic programming (Generic Programming, referred to as GP) is a data processing method and separated, processing methods are generally global functions, for example in the STL algorithm and containers, both independently of each other, when required by the iterator to convey information.
  Well, then start from six parts, understand the internal relations of STL. The main focus of this blog record distributor.

Distributor (allocators)

  allocator mainly used to manage storage space, allocate it among () call to operator new () to allocate space in which the operator new () in turn calls the C language malloc () function, and deallocate () is called the operator delete (), which, operator delete () and call free () to free memory. Such dispensers are more to talk about implementation under the standard specifications. And there is a default SGI STL dispenser alloc, which each have a container specify default distributor space is alloc, for example, the following declaration vector:
Template <class T, the alloc class of Alloc =>
class vector {...} ;
  which is the default alloc use of the dispenser. Since calling malloc () only to find the entire piece relatively large space for some small blocks of memory may cause the problem of waste, therefore, SGI designed a two-stage divider, first-class distributor directly using malloc () and free (), the second stage distributor is optionally different strategies: when the allocation block exceeds 128 bytes, regarded as "large enough", invokes a first stage distributor block when dispensing is less than 128 bytes when, regarded as "too small", in order to reduce the additional burden (rational use of resources), on the use of second-level distributor.
  The specific implementation of the second stage of the dispenser in the form of memory pool management, every time a large memory configuration, and maintenance free list corresponding to (increase can also be cut), the next memory requirements if they have the same size , was taken directly from the free list, if a small memory release side, recovered by the allocator to the corresponding list. To facilitate the management, SGI second stage distributor will automatically be adjusted to a multiple of eight small memory requirements of any block (e.g., the user needs 30 bytes, 32 bytes is automatically adjusted), and maintain free 16 linked list, each of manageable size are: small 8,16,24,32,40,56,64,72,80,88,96,104,112,128 byte block (e.g.: a first list node to the management space for all of the cell size is 8 bytes fast, the second node manages all of the cell size of 16 bytes fast, and so on).
  Thus, the entire release process memory allocation process can be summarized as follows: the allocate function dispenser has a standard interface (), this function first determines the block size, larger than 128 bytes is called a first-stage splitter, less than 128 bytes checks the free list corresponding. If there is the free block list is available, it is directly used, if not available on the adjusted blocks, block size will be a multiple of eight, and then re-filled space. When released, the same standard interface deallocate function dispenser () first determines the block size, larger than 128 bytes is called a first-stage splitter, less than 128 bytes to find the free list corresponding to the block recovery .

Verbatim large column  https://www.dazhuanlan.com/2019/08/27/5d64c84c4c428/


Guess you like

Origin www.cnblogs.com/petewell/p/11418625.html