1

Malloc memory block layout under VC6

image.png

Corresponding to the top-down memory consumption in the figure [debug mode]:
malloc(0x0c) ==>

4 + (4 * 8) + 0x0C + 4 + (2 * 4) + 4 = 0x40 byte
上下 cookes 的数值 0x41 说明: 0x41 = 0x40 + 1b

 16 byte(00010000b)对齐时低 4 位一定为 0,借助最后一位标记此块内存是否被使 01000000b + 1b = 01000001
  • Considering the occupancy ratio of cookies, in actual projects, there are often a large number of small blocks, and cookies will occupy a higher space ratio.
  • Conditions for removing cookies: the size of managed elements is the same

Implementation of VC6 Standard Distributor

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

The standard library attached to VC6, its std::allocator is implemented as follows (<xmemory>)

image.png

Implementation of BC5 Standard Distributor

BC5's allocator only completes allocate() and deallocate() with ::operator new and ::operator delete, without any special design

The standard library attached to BC5, its std::allocator is implemented as follows (memory.stl)

image.png

Implementation of G2.9 Standard Distributor

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

The standard library attached to G2.9, its std::allocator is implemented as follows (<defalloc.h>)

image.png

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

The allocator used by G2.9 containers is not std::allocator but std::alloc

image.png

Are you here now? (G2.9)std::alloc VS (G4.9)__poll_alloc

image.png

image.png

Implementation of G4.9 Standard Distributor

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

image.png

4.9 Pool allocator use case

Note: continuous memory interval is 8 bytes, which means that there is no cookie

image.png

#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

TianSong
734 声望138 粉丝

阿里山神木的种子在3000年前已经埋下,今天不过是看到当年注定的结果,为了未来的自己,今天就埋下一颗好种子吧