AllocationMemoryPool
No description available
Install / Use
/learn @Twilight-Dream-Of-Magic/AllocationMemoryPoolREADME
🛠️ Near-Industrial-Grade Self-Managed Memory Pool & Allocator
TL;DR
This is a cross‑platform memory allocator + pointer pool, self‑developed in modern C++, approaching industrial‑grade performance.
✨ Features | Main Features
| Feature | Description |
| ------------------------------- | ------------------------------------------------------------------------------------ |
| Layered architecture | Four levels of managers: Small (≤ 1 MiB), Medium (≤ 512 MiB), Large (≤ 1 GiB), Huge (> 1 GiB). |
| Thread‑local pools | Lock‑free fast path via per‑thread buckets. |
| Native virtual memory | Direct mmap / NtAllocateVirtualMemory, with huge‑page support. |
| MemoryTracker | Source‑location leak tracing, no third‑party dependencies. |
| SafeMemoryLeakReporter | Automatically dumps leaks on process exit using only fwrite. |
| Atomic counters | Real‑time byte/op counts for quick sanity checks. |
| Header‑only public API | Just #include and go. |
| C++17 compliant | Supports Windows / Linux (x64).
📂 File Overview | Code Structure
| File | Purpose | Role |
| ------------------------------- | ----------------------------------------------- | ----------------------------------------------- |
| memory_allocators.hpp | SystemAllocator & helpers – thin OS wrappers. | OS‑layer wrappers (Linux mmap/Windows NT*). |
| memory_tracker.hpp | Leak map & track_* helpers. | Leak mapping & tracking functions. |
| safe_memory_leak_reporter.hpp | atexit dump helper. | Automatically reports leaks at process exit. |
| memory_pool.hpp / .cpp | Core MemoryPool, four managers, TLS cache. | Core memory pool & managers. |
| memory_pool.cpp | Implementation details. | Implementation specifics. |
| (optional) pool_allocator.hpp | Plug‑n‑play STL‑style allocator. | STL‑compatible allocator. | |
🔧 Build Example | 构建示例
Linux 都已经2025年了,用个C++20标准有那么难吗?赶紧跟进一下吧! 推荐:-std=c++20,当然仍兼容 C++17。
g++ -std=c++20 -O2 -pthread demo.cpp -o demo
2025年,别再用2017了!
Windows (MSVC)
cl /std:c++20 /O2 demo.cpp`
C++20 or report your weird bugs yourself
EN – It’s 2025 — using C++17 as default? Come on, upgrade to C++20! If you insist on C++17 and hit odd bugs, feel free to file an issue — but you’ll have to debug it yourself. 中文 – 都 2025 年了,默认用 C++17?别犹豫,赶紧改成 C++20!要是你还在 C++17 报奇怪问题,欢迎反馈,但得自己解决哦~
🧪 Tests & Manual Tracking | 手动追踪与测试样例
除了SafeMemoryLeakReporter::Automatic自动模式,本项目还支持显式启停的纯手动追踪接口,方便做单元测试或 A/B Benchmark。下面给出一套覆盖率较高的示例用例,直接复制即可运行。
EN – The snippet toggles tracking on/off, runs five stress tests, prints a manual leak report, then disables tracking and checks again. 中文 – 该示例开启追踪 → 跑完五个压力 / 泄漏用例 → 手动打印一次 → 关闭追踪并确认无残留。
#include "global_allocator_api.hpp"
#include "stl_allocator.hpp"
#include "safe_memory_leak_reporter.hpp"
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
#include <random>
#include <thread>
#include <chrono>
/*
* Detailed Explanation of Two Key Lines for C++ I/O Optimization
* 1. Disable C/C++ Stream Synchronization
* std::ios::sync_with_stdio(false);
*
* • Benefits:
* - Eliminates mutex-like synchronization between C++ streams (std::cin/std::cout)
* and C stdio (printf/scanf), reducing OS-level function call overhead.
* - Can yield up to 2× speedup in pure C++ I/O scenarios, especially when reading
* or writing large volumes of data.
*
* • Drawbacks:
* - After disabling, mixing printf/scanf with std::cout/std::cin leads to unpredictable
* ordering or missing output, complicating debugging.
* - On some platforms or compilers, default I/O may already be optimized, so gains
* may be negligible in light I/O workloads.
*
* 2. Untie std::cin from std::cout
* std::cin.tie(nullptr);
*
* • Benefits:
* - Prevents std::cin from flushing std::cout before every input operation,
* saving a few microseconds per flush in read‑heavy loops.
* - In tight loops of alternating read/write, cumulative savings can reach
* tens or hundreds of microseconds.
*
* • Drawbacks:
* - Prompts or previous outputs may remain buffered and not appear before input,
* requiring explicit std::cout.flush() or use of std::endl at key points.
* - If your logic relies on automatic flushing for user prompts, you must now
* manage flushes manually to maintain correct UX.
*
* Tips for Safe Use:
* - If mixing C and C++ I/O, do not disable sync, or restrict these optimizations
* to pure C++ sections.
* - In multithreaded contexts, protect shared streams with an external mutex;
* these calls do not alter the standard’s per‑operation thread safety guarantees.
*
*
* C++ I/O 优化两行关键代码详解
* 1. 禁用 C 与 C++ 流的同步
* std::ios::sync_with_stdio(false);
*
* • 好处:
* - 消除 C++ 流(std::cin/std::cout)与 C stdio(printf/scanf)之间的“互斥”同步,
* 减少系统调用开销。
* - 在纯 C++ I/O 场景下,可获得最高约 2 倍的性能提升,尤其是处理大文件或海量数据时。
*
* • 坏处:
* - 解除同步后,若混用 printf/scanf 和 std::cout/std::cin,输出顺序可能错乱或丢失,
* 调试难度增加。
* - 某些平台/编译器默认已优化 I/O,轻量级 I/O 场景下提升有限。
*
* 2. 解除 std::cin 与 std::cout 的绑定
* std::cin.tie(nullptr);
*
* • 好处:
* - 防止 std::cin 在每次输入前自动刷新 std::cout,避免每次 flush 带来的几微秒开销。
* - 在交替读写的循环中,可累计节省几十到几百微秒。
*
* • 坏处:
* - 提示信息或前一轮输出可能停留在缓冲区中,需手动调用 std::cout.flush() 或使用 std::endl。
* - 如果依赖自动刷新来确保提示先行显示,需自行管理刷新时机,否则影响用户体验。
*
* 安全使用建议:
* - 混合使用 C/C++ I/O 时,应避免禁用同步,或仅在纯 C++ 区块内应用上述优化。
* - 多线程环境下,共享流对象仍需使用外部互斥锁保护;这些调用不改变 C++ 标准
* 对单次流操作的线程安全保证。
*/
void cpp_io_optimization()
{
std::cout.sync_with_stdio( false );
std::cin.tie( nullptr );
}
// 测试nothrow分配场景 / Test nothrow allocation scenario
void test_nothrow()
{
// 尝试分配8GB / Attempt to allocate 8GB
void* first_allocation_pointer = ALLOCATE_NOTHROW( 1024ULL * 1024ULL * 1024ULL * 8ULL );
if ( !first_allocation_pointer )
{
std::cout << "Nothrow allocation failed as expected" << std::endl;
}
else
{
os_memory::api::my_deallocate( first_allocation_pointer );
}
// 测试普通分配(会抛出异常) / Test regular allocation (throws on failure)
try
{
// 尝试分配8GB / Attempt to allocate 8GB
void* second_allocation_pointer = ALLOCATE( 1024ULL * 1024ULL * 1024ULL * 8ULL );
os_memory::api::my_deallocate( second_allocation_pointer );
}
catch ( const std::bad_alloc& exception_reference )
{
std::cout << "Caught bad_alloc: " << exception_reference.what() << std::endl;
}
}
// 测试内存泄漏场景 / Test memory leak scenario
void test_memory_leak()
{
// 分配带调试信息的内存 / Allocate memory with debug info
int* first_int_pointer = static_cast<int*>( ALLOCATE( 1024 ) );
double* first_aligned_double_pointer = static_cast<double*>( ALLOCATE_ALIGNED( 256, 64 ) );
// 故意泄漏一个内存块 / Intentionally leak one block
// void* intentional_leak = ALLOC(512);
// 释放部分内存 / Deallocate some blocks
os_memory::api::my_deallocate( first_int_pointer );
os_memory::api::my_deallocate( first_aligned_double_pointer );
}
/// @brief 分配器碎片化场景 / Fragmentation stress test
void test_fragmentation()
{
std::mt19937_64 random_engine( static_cast<unsigned long long>( std::chrono::steady_clock::now().time_since_epoch().count() ) );
std::uniform_int_distribution<size_t> small_size_distribution( 16, 256 );
std::uniform_int_distribution<size_t> medium_size_distribution( 257, 4096 );
std::uniform_int_distribution<size_t> large_size_distribution( 4097, 16384 );
std::vector<void*> allocation_pointer_list;
allocation_pointer_list.reserve( 2000 );
// 交替分配小/中/大块并随机对齐 / Alternate allocations of small/medium/large blocks with random alignment
std::vector<size_t> alignment_options = { 8, 16, 32, 64, 128, 256 };
for ( int iteration_index = 0; iteration_index < 1200; ++iteration_index )
{
size_t allocation_size;
if ( iteration_index % 3 == 0 )
allocation_size = small_size_distribution( random_engine );
else if ( iteration_index % 3 == 1 )
allocation_size = medium_size_distribution( random_engine );
else
allocation_size = large_size_distribution( random_engine );
size_t allocation_alignment = alignment_options[ random_engine() % alignment_options.size() ];
void* allocation_pointer = ALLOCATE_ALIGNED_NOTHROW( allocation_size, allocation_alignment );
if ( allocation_pointer )
allocation_pointer_list.push_back( allocation_pointer );
}
// 随机释放一半以制造空洞 / Randomly free half to create holes
std::shuffle( allocation_pointer_list.begin(), allocation_pointer_list.end(), random_engine );
for ( size_t release_index = 0; release_index < allocation_pointer_list.size() / 2; ++release_index )
{
DEALLOCATE( allocation_pointer_list[ release_index ] );
allocation_pointer_list[ release_index ] = nullptr;
}
// 再次分配填充碎片 / Reallocate to fill fragmentation
for ( int refill_index = 0; refill_index < 600; ++refill_index )
{
size_t allocation_size = static_cast<size_t>( ( refill_index * 37 ) % 1024 ) + 1;
void* allocation_pointer = ALLOCATE( allocation_siz
Related Skills
node-connect
339.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
339.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.9kCommit, push, and open a PR
