Skip to content

Latest commit

 

History

History
79 lines (56 loc) · 2.66 KB

File metadata and controls

79 lines (56 loc) · 2.66 KB

Allocator — Custom Memory Allocators

Part of Project_zero.

A header-only static library providing several custom memory allocators that are fully compatible with the C++ standard allocator interface (std::allocator_traits). They can be plugged directly into any STL container or the custom ADT containers.


Features

Memory resources (low-level)

  • Pool_resource — fixed-size block pool; allocates from a pre-allocated slab, recycles freed blocks via a free-list
  • Monotonic_resource — bump-pointer allocator; extremely fast allocation, no individual deallocation (free all at once)
  • Stack_resource — LIFO stack-based resource; deallocations must happen in reverse order

STL-compatible allocators (high-level wrappers)

  • Pool_allocator<T> — wraps Pool_resource
  • Monotonic_allocator<T> — wraps Monotonic_resource
  • Stack_allocator<T> — wraps Stack_resource
  • Segregated_allocator<T> — segregated free-list; routes allocations to size-class buckets for low fragmentation

Diagnostics

  • Debug_allocator<T> — wraps any allocator; tracks total allocations, deallocations, and currently live bytes; detects leaks at destruction

Setup & Linking

1. Include the header

#include "Allocator.h"

Add the Allocator/ folder to your include path if needed.

2. Link the static library

In Visual Studio:

  • Linker → Input → Additional Dependencies → add Allocator.lib
  • Linker → General → Additional Library Directories → add the path to x64/Release/

Quick Start

#include "Allocator.h"
#include <vector>
#include <iostream>

int main() {
    // Pool allocator with a std::vector
    Pool_allocator<int> pool_alloc;
    std::vector<int, Pool_allocator<int>> vec(pool_alloc);
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);

    // Debug allocator wrapping the default allocator
    Debug_allocator<int> dbg;
    std::vector<int, Debug_allocator<int>> dbg_vec(dbg);
    dbg_vec.push_back(10);
    dbg_vec.push_back(20);
    // On destruction, Debug_allocator will report any leaks

    // Monotonic resource for short-lived scratch allocations
    Monotonic_resource mono(1024); // 1 KB buffer
    Monotonic_allocator<double> mono_alloc(&mono);
}

Notes

  • Monotonic_resource does not support individual deallocate calls — memory is released all at once when the resource goes out of scope. It is best used for temporary/arena-style allocations.
  • Debug_allocator has a small runtime overhead and is intended for debug builds only.
  • All allocators satisfy the std::allocator_traits requirements and work with any standard or custom container.