[C + + memory management] 7_ VC6、BC5、G2. 9、G4. 9 standard distributor implementation

Time:2022-5-8

Malloc memory block layout under VC6

[C + + memory management] 7_ VC6、BC5、G2. 9、G4. 9 standard distributor implementation

Corresponding to the top-down memory consumption in the figure [debug mode]:

malloc(0x0c) ==>

4 + (4 * 8) + 0x0C + 4 + (2 * 4) + 4 = 0x40 byte
The value of upper and lower cookies 0x41 Description: 0x41 = 0x40 + 1B

 When 16 bytes (00010000b) are aligned, the lower 4 bits must be 0. Use the last bit to mark whether this block of memory is made 01000000B + 1b = 01000001
  • Considering the occupancy ratio of cookies, in practical engineering, it is often a large number of small blocks, and cookies will occupy a high space ratio
  • Consistent element size management: consistent element size

Implementation of VC6 standard distributor

The allocator of VC6 only completes allocate() and deallocate() with:: operator new and:: operator delete, without any special design

The STD:: allocator of the standard library attached to VC6 is implemented as follows (< xmemory >)

[C + + memory management] 7_ VC6、BC5、G2. 9、G4. 9 standard distributor implementation

Implementation of bc5 standard distributor

The allocator of bc5 only completes allocate() and deallocate() with:: operator new and:: operator delete, without any special design

The STD:: allocator of the standard library attached to bc5 is implemented as follows (memory. STL)

[C + + memory management] 7_ VC6、BC5、G2. 9、G4. 9 standard distributor implementation

G2. 9 implementation of standard distributor

G2. 9 allocator only completes allocate () and deallocate () with:: operator new and:: operator delete, without any special design

G2. The STD:: allocator implementation of the standard library attached to 9 is as follows (< defalloc. H >)

[C + + memory management] 7_ VC6、BC5、G2. 9、G4. 9 standard distributor implementation

(do not use this file unless you have an old container implementation that requires an allocator with an HP style interface. SGI STL uses different allocator interfaces. SGI style allocators do not have parameterization of object types, they are transferred in the void * pointer. This file is not included in other SGI STL header files)

G2. The allocator used by the 9 container is not STD:: allocator, but STD:: allocator

[C + + memory management] 7_ VC6、BC5、G2. 9、G4. 9 standard distributor implementation

And where is today? (G2.9)std::alloc VS (G4.9)__ poll_ alloc

[C + + memory management] 7_ VC6、BC5、G2. 9、G4. 9 standard distributor implementation

[C + + memory management] 7_ VC6、BC5、G2. 9、G4. 9 standard distributor implementation

G4. 9 implementation of standard distributor

G4. 9 allocator only completes allocate () and deallocate () with:: operator new and:: operator delete, without any special design

[C + + memory management] 7_ VC6、BC5、G2. 9、G4. 9 standard distributor implementation

4.9 pool allocator use case

be careful:The continuous memory interval is 8 bytes, indicating that no cookie exists

[C + + memory management] 7_ VC6、BC5、G2. 9、G4. 9 standard distributor implementation

#include <iostream>
#include <ext\pool_allocator.h>
#include <vector>

using namespace std;

template <typename Alloc>
void cookies_test(Alloc alloc, size_t n)
{
    typename Alloc::value_type *p1, *p2, *p3;

    p1 = alloc.allocate(n);
    p2 = alloc.allocate(n);
    p3 = alloc.allocate(n);

    cout << "p1=" << p1 << '\t';
    cout << "p2=" << p2 << '\t';
    cout << "p3=" << p3 << '\n';

    alloc.deallocate(p1, sizeof(typename Alloc::value_type));
}

int main()
{
    cout << sizeof(__gnu_cxx::__pool_alloc<int>) << endl;
    vector<int, __gnu_cxx::__pool_alloc<int>> vecPool;

    cookies_test(__gnu_cxx::__pool_alloc<double>(), 1);
    cookies_test(__gnu_cxx::__pool_alloc<double>(), 1);

    return 0;
}

Output:

1
p1=0xeb8128    p2=0xeb8130    p3=0xeb8138
p1=0xeb8140    p2=0xeb8148    p3=0xeb8150