1

I am not familiar with tracking down the source codes to figure out the GCC STL implementation (libstdc++) of the C++ standard allocator, and I cannot find any brief explanation, documentation and technical reports that describe what the memory model that GCC selects.

I guess that GCC uses several fixed-size buckets for storing small objects of the same size in bytes and allocate large memory space ad hoc for large objects over the specific size.

What is the specific memory model that GCC selects for the C++ standard allocator?

Kevin Dong
  • 5,001
  • 9
  • 29
  • 62
  • Why don't you look into the header? –  Jan 30 '18 at 17:06
  • @manni66 Compiler headers often call intrinsic functions instead of actually containing code. – Cpp plus 1 Jan 30 '18 at 17:07
  • Are you asking about `std::allocator` specifically or the heap? One is likely implemented on top of the other. – Useless Jan 30 '18 at 17:08
  • @Useless Yes. I am wondering how `std::allocator` is implemented. Especially, the exact number of thresholds, the bucket data structures, etc. – Kevin Dong Jan 30 '18 at 17:09

2 Answers2

3

I cannot find any brief explanation, documentation and technical reports that describe what the memory model that GCC selects

Read the code. It's open source, and it's included with the compiler as plain text.

If you're not sure where your standard headers are, you can do one of two things:

  1. learn how to ask the compiler as in this question

  2. trick the compiler into telling you, eg. by trying to do something std::allocator cannot

    #include <memory>
    
    int main() {
        std::allocator<int> a;
        a.allocate(&a);
        return 0;
    }
    

    gives

    error ... 
    In file included from \
      /usr/include/x86_64-linux-gnu/c++/6/bits/c++allocator.h:33:0,
    

When you find out that std::allocator just uses the heap to make all those decisions, you can look at the glibc source for malloc.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • Thanks for that. I found that the actual implementation is in `new_allocator.h`, and it is simply an wrapper of `new` and `delete`. ;-) – Kevin Dong Jan 30 '18 at 17:19
1

std::allocator just uses operator new and operator delete, and those in turn simply wrap malloc and free.

Thus the implementation is delegated to the C library, whichever happens to be in use.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157