Allocator Builder
Policy Based C++ Template Allocator Library
|
A highly composable, policy based C++ allocator.
The layout idea of the library is was presented by Andrei Alexandrescu at the C++ and Beyond 2013 seminar and at the CppCon 2015.
The background behind the idea is to compensate the main problem of malloc and the other current standard allocators, separation of memory pointer and the allocated size. This makes it very difficult for all kind of memory management to handle in a fast way memory allocations and especially deallocations, because the memory manager must find the corresponding length of the pointer that shall be free. As well all memory handler are currently general purpose handler. This policy based approach allows to create handlers for special purpose. Additionally all users of manually raw allocated memory have to store the size anyway to ensure that no access beyond the length of the allocated buffer takes place.
An other idea behind this allocator library is, that one can compose for every use case a special designed one. Example use cases:
So the approach is, every allocator returns such a block:
And a request goes this way:
Raw memory is temporarily needed inside a function, 64-128 bytes per allocation. The fastest way would be to use ::alloca(); getting the memory from the stack. But this memory cannot be freed explicitly. But with the combination of free-list these memory block could be recycled. This approach would be must faster than getting the memory from the heap. The order of allocations and de-allocations can happen in any order.
So the code could look like this
A more advanced allocator with different sized buckets as one are used in jemalloc would look like:
Allocator | Description |
---|---|
affix_allocator | Allows to automatically pre- and sufix allocated regions. |
allocator_with_stats | An allocator that collects a configured number of statistic information, like number of allocated bytes, number of successful expansions and high tide |
bucketizer | Manages a bunch of Allocators with increasing bucket size |
fallback_allocator | Either the default Allocator can handle a request, otherwise it is passed to a fall-back Allocator |
(aligned_)mallocator | Provides and interface to systems ::malloc(), the aligned variant allocates according to a given alignment |
null_allocator | An Null allocator |
segregator | Separates allocation requests depending on a threshold to Allocator A or B |
(shared_)freelist | Manages a list of freed memory blocks in a list for faster re-usage. (The Shared variant is thread safe) |
(shared_)cascading_allocator | Manages in a thread safe way Allocators and automatically creates a new one when the previous are out of memory. (The Shared variant is thread safe, but it needs further improvements, because it does not frees unused allocators) |
(shared_)heap | A heap block based heap. (The Shared variant is thread safe manner with minimal overhead and as far as possible in a lock-free way.) |
stack_allocator | Provides a memory access, taken from the stack |
Online Documentation is available on GitHub.io as well.
There is the begin of a tutorial in the tutorial section
Felix Petriconi (felix at petriconi.net)
Gary Furnish
Comments, feedback or contributions are welcome!
Boost 1.0 License
0.1.0
Compiler | Status |
---|---|
Visual Studio 2015 x64 | All tests pass |
Debian x64, Clang 3.4 | All tests pass |
Intel XE Inspector x64 | No detections |
Clang thread sanitizer | No detections |
See issue list of open enhancements