当前位置:网站首页>[STL source code analysis] configurator (to be supplemented)

[STL source code analysis] configurator (to be supplemented)

2022-06-25 02:17:00 Cloudeeeee

【STL Source analysis 】 Configurator ( To be added )

Number before title ( Such as “2.1 Standard interface of space configurator ” Number in 2.1 ) On behalf of the 《STL Source analysis 》 Chapter number in a book .

2.1 Standard interface of space configurator

“ Dictionaries ”:

ptrdiff_t
ptrdiff_t yes C/C++ A machine related defined in the standard library data type .ptrdiff_t Type variables are usually used to hold the results of two pointer subtraction operations .ptrdiff_t It's defined in stddef.h(cstddef) In this header file .ptrdiff_t It is usually defined as long int type .

set_new_handler(x)
Using operator new When the request for memory allocation fails , Before throwing an exception , A callback function will be called to give the programmer a chance to handle the memory allocation failure .x Represents a function pointer , Point to the programmer's own defined function when the user fails to handle memory allocation . In this code x Set to nullptr perhaps 0 It means that the programmer gives up this processing opportunity , Direct selling bad_alloc abnormal .

::operator new , ::operator delete
When we're in C++ Use in new and delete when , In fact, the implementation is global ::operator new and ::operator delete , :: Without a namespace before it, it means the default namespace of the outermost layer .

new( p ) T1(value);
It's in the pointer p The memory space pointed to creates a T1 Object of type , But the content of the object is from T2 Object of type , Is to readjust the allocated space on the basis of the existing space . This operation is to use the existing space as a buffer , This reduces the time it takes to allocate space , Because I use it directly new Operator to allocate memory , Finding enough free space in the heap is slow .

inline Inline function
Using inline functions can avoid the cost of function calls , When a function is called , In addition to the cost of function execution statements , There are many other expenses , Such as copying formal parameters 、 Restore on return, etc , When using inline functions , The program does not execute “ call ”, It is equivalent to copying the statements in the function here , Executing simple statements is faster than executing functions , Therefore, using inline functions can avoid the cost of function calls .

notes :

 Insert picture description here
 Insert picture description here

2.2.2 SGI Special space configurator std::alloc

 Insert picture description here

2.2.3 Basic tools for construction and deconstruction :construct() and destroy()

Mainly aimed at object Structure and Deconstruction of , Before object construction , Memory Has been allocated , After the object is destructed , Memory will be released .

trivial destructor
If the user does not define a destructor , It's a self-contained system , shows , Destructors are basically useless ( But by default it will be called ) We call it trivial destructor. conversely , If a destructor is defined specifically , You need to do something before releasing space , Then this destructor is called non-trivial destructor.
 Insert picture description here
 Insert picture description here

2.2.4 Allocation and release of space :std::alloc

size_t
size_t The full name is size_type, Used to indicate a certain type of size 、 Number of bytes ,size_t Is a data type of record size .

The design philosophy of space configuration and release :

  • towards system heap Space required in
  • Consider multithreading (multi-threads) The state of ( This article does not consider multithreading )
  • Consider the contingency measures in case of insufficient memory space
  • Consider the problem of too much debris space

C++ Memory configuration Basic operation ::operator new() ( Global function )
C++ Memory free Basic operation ::operator delete()( Global function )
Their bottom layer uses C Linguistic malloc() Functions and free() Function to configure and release the memory space

SGI Double stage setter for ( To solve the problem of debris space ):
First level configurator (__malloc_alloc_template): Use it directly malloc() and free() Function to allocate and free space
Second level configurator (__default_alloc_template): For debris space memory pool Sorting method of
Use policy : When the configuration block exceeds 128kb when , It is considered that the space is large enough , Directly use the first level configurator ; Otherwise, use the second level configurator .

 Insert picture description here
 Insert picture description here
 Insert picture description here

2.2.5 First level configurator __malloc_alloc_template analyse

原网站

版权声明
本文为[Cloudeeeee]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206242240240438.html