# mem_pool **Repository Path**: liudegui/mem_pool ## Basic Information - **Project Name**: mem_pool - **Description**: 符合Misra C++标准且支持mmap的内存池管理模块 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 2 - **Created**: 2024-05-12 - **Last Updated**: 2026-02-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # mem_pool [![CI](https://github.com/DeguiLiu/mem_pool/actions/workflows/ci.yml/badge.svg)](https://github.com/DeguiLiu/mem_pool/actions/workflows/ci.yml) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/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` with placement new ## Quick Start ```cpp #include // 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 sensors; Sensor* s = sensors.Create(1, 25.5f); // placement new sensors.Destroy(s); // ~Sensor() + free ``` ## API Reference ### FixedPool | 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 | 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 ```bash 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 ```bash ./examples/mp_benchmark ``` ## Requirements - CMake 3.14+ - C++14 compiler (GCC 7+ / Clang 6+) ## Documentation - [Design Document (Chinese)](docs/design_zh.md) - [Benchmark Report](docs/benchmark_report.md) - [Changelog](CHANGELOG.md) ## License MIT License. See [LICENSE](LICENSE).