Skip to content

DeguiLiu/mem_pool

Repository files navigation

mem_pool

CI License: MIT

Header-only C++14 memory pool library with compile-time sized blocks, O(1) allocation, and zero heap allocation.

Features

  • O(1) alloc/free via embedded free list
  • Zero heap allocation -- all storage inline
  • Thread-safe via std::mutex
  • Header-only -- single #include
  • Compile-time sized -- no runtime growth
  • -fno-exceptions -fno-rtti compatible
  • Pointer validation with OwnsPointer()
  • Type-safe ObjectPool<T> with placement new

Quick Start

#include <mp/mem_pool.hpp>

// Fixed-size raw block pool
mp::FixedPool<64, 1024> pool;  // 1024 blocks of 64 bytes

void* p = pool.Allocate();     // O(1)
pool.Free(p);                  // O(1)

// Type-safe object pool
struct Sensor { uint32_t id; float value; };
mp::ObjectPool<Sensor, 128> sensors;

Sensor* s = sensors.Create(1, 25.5f);  // placement new
sensors.Destroy(s);                     // ~Sensor() + free

API Reference

FixedPool<BlockSize, MaxBlocks>

Method Description Complexity
Allocate() Allocate a block, nullptr if full O(1)
Free(ptr) Return block to pool O(1)
OwnsPointer(ptr) Check if pointer belongs to pool O(1)
FreeCount() Number of available blocks O(1)
UsedCount() Number of allocated blocks O(1)
Capacity() Total pool capacity (constexpr) O(1)
DumpState(label) Print pool state to stdout O(1)

ObjectPool<T, MaxObjects>

Method Description Complexity
Create(args...) Construct T in pool, nullptr if full O(1)
Destroy(obj) Destruct T and return to pool O(1)
OwnsPointer(obj) Check if pointer belongs to pool O(1)

Build

mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DMP_BUILD_TESTS=ON -DMP_BUILD_EXAMPLES=ON
cmake --build . -j
ctest --output-on-failure

Build Options

Option Default Description
MP_BUILD_TESTS ON Build test suite
MP_BUILD_EXAMPLES ON Build examples

Run Benchmark

./examples/mp_benchmark

Requirements

  • CMake 3.14+
  • C++14 compiler (GCC 7+ / Clang 6+)

Documentation

License

MIT License. See LICENSE.

About

Header-only C++14 memory pool library with compile-time sized blocks, O(1) allocation, and zero heap allocation.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors