Just a quick word about STL memory management

  • 2020-04-02 01:29:07
  • OfStack

1. An overview of the
The STL Allocator is the memory manager of the STL, one of the lowest keys, and you may have used the STL for three years without knowing what it is.

The STL standard introduces Allocator as follows
The STL includes some low level mechanisms for allocating and deallocating memory. Allocators are very specialized, And you can safely ignore them for almost all purposes. Allocators encapsulate allocation and deallocation of the memory. They dojo.provide a low level interface that permits efficient allocation of many small Objects; Different allocator types represent different schemes for memory management.
< STL source code analysis > It is described as a spatial configurator because allocator can use other storage media, such as hard disks, as storage space for the STL container. Since memory is the main part of allocator management, this article introduces allocator based on STL memory management.

Allocator is right here with us, usually using the STL:
# include < The vector >
STD: : vector < int > Array (100);

Essentially, the call is:

# include < The vector >
STD: : vector < Int, STD: : allocator > Array (100);
STD ::allocator is simply an allocator

2. Use
According to different applications, the STL implements different Allocator, as follows (GCC - 3.4: (link: http://www.cs.huji.ac.il/~etsman/Docs/gcc-3.4-base/libstdc++/html/20_util/allocator.html)) :

__gnu_cxx: : new_allocator < T > Simply wraps ::operator new and ::operator delete.
__gnu_cxx: : malloc_allocator < T > Simply wraps malloc and free. There is also a hook for an out-of-memory handler
__gnu_cxx: : debug_allocator < T > A wrapper around an allocator a. It passes on slightly increased size requests to A, and USES the extra memory to store size information.
__gnu_cxx: : __pool_alloc < Bool, int > A high-performance, single pool allocator. The reusable memory is Shared among identical instantiations of this type.
__gnu_cxx: : __mt_alloc < T > A high - performance fixed - size allocatorthat was initially developed specifically to suit the needs of multi threaded applications
_gnu_cxx::bitmap_allocato A high-performance allocator that USES A bit-map to keep track of the used and unused memory locations

For example, in a multi-threaded environment, you can use:

#include <vector>  
#include <mt_allocator.h>  
std::vector<int, __gnu_cxx::__mt_alloc<int>> Array(100);  

3. A simple implementation of Allocator
We can implement our own allocator

#include <memory>  

template<class T>  
class my_allocator : public std::allocator<T>  
typedef std::allocator<T> base_type;  

//Must redefine & NBSP;
template<class Other>  
struct rebind  
typedef my_allocator<Other> other;  
//The allocation and release of memory can be implemented as a custom algorithm & NBSP;
pointer allocate(size_type count)  
return (base_type::allocate(count));  

void deallocate(pointer ptr, size_type count)  
base_type::deallocate(ptr, count);  

//Constructor & NBSP;

my_allocator(my_allocator<T> const&)  

my_allocator<T>& operator=(my_allocator<T> const&)  
return (*this);  

template<class Other>  
my_allocator(my_allocator<Other> const&)  

template<class Other>  
my_allocator<T>& operator=(my_allocator<Other> const&)  
return (*this); }  

Related articles: