39 size_t aligned_size = (size + alignment - 1) & ~(alignment - 1);
40 return _aligned_malloc(aligned_size, alignment);
43 size_t aligned_size = (size + alignment - 1) & ~(alignment - 1);
44 return std::aligned_alloc(alignment, aligned_size);
117 if (
this != &
other) {
133 if (
this != &
other) {
151 return (
static_cast<double>(
total_allocations.load()) /
static_cast<double>(total)) * 100.0;
207 other.total_blocks_ = 0;
211 if (
this != &
other) {
221 other.total_blocks_ = 0;
231 std::lock_guard<std::mutex> lock(
mutex_);
247 return common::ok(block);
256 if (ptr ==
nullptr) {
260 std::lock_guard<std::mutex> lock(
mutex_);
280 template<
typename T,
typename... Args>
287 if (result.is_err()) {
291 void* ptr = result.value();
292 T* obj =
new (ptr) T(std::forward<Args>(args)...);
294 return common::ok(obj);
305 if (obj ==
nullptr) {
318 std::lock_guard<std::mutex> lock(
mutex_);
358 if (chunk ==
nullptr) {
359 throw std::bad_alloc();
366 char* ptr =
static_cast<char*
>(chunk);
378 if (new_blocks == 0) {
385 if (chunk ==
nullptr) {
391 char* ptr =
static_cast<char*
>(chunk);
392 for (
size_t i = 0; i < new_blocks; ++i) {
405 char* chunk_end = chunk_start + chunk_blocks *
block_size_;
407 if (ptr >= chunk_start && ptr < chunk_end) {
409 size_t offset =
static_cast<char*
>(ptr) - chunk_start;
421 while (current_usage > peak) {
442 return std::make_unique<memory_pool>();
451 return std::make_unique<memory_pool>(config);
461 {.initial_blocks = 512, .max_blocks = 2048, .block_size = 32, .alignment = 8},
463 {.initial_blocks = 256, .max_blocks = 1024, .block_size = 128, .alignment = 16},
465 {.initial_blocks = 64, .max_blocks = 256, .block_size = 512, .alignment = 32},
467 {.initial_blocks = 256, .max_blocks = 1024, .block_size = 64, .alignment = 8, .use_thread_local_cache =
true}
Thread-safe fixed-size block memory allocator.
void reset_statistics()
Reset statistics.
common::VoidResult deallocate_object(T *obj)
Destroy and deallocate an object.
std::vector< void * > free_blocks_
size_t block_size() const
Get block size.
memory_pool & operator=(memory_pool &&other) noexcept
const memory_pool_statistics & get_statistics() const
Get pool statistics.
memory_pool(const memory_pool &)=delete
bool is_owned_block(void *ptr) const
size_t available_blocks() const
Get number of available blocks.
memory_pool(memory_pool &&other) noexcept
memory_pool(const memory_pool_config &config)
Construct with configuration.
std::vector< void * > memory_chunks_
size_t total_blocks() const
Get total number of blocks.
memory_pool()
Default constructor with default configuration.
common::Result< void * > allocate()
Allocate a memory block.
memory_pool_config config_
common::VoidResult deallocate(void *ptr)
Deallocate a memory block.
memory_pool_statistics stats_
common::Result< T * > allocate_object(Args &&... args)
Allocate and construct an object.
memory_pool & operator=(const memory_pool &)=delete
Internal implementation details - not part of public API.
void * aligned_alloc_impl(size_t alignment, size_t size)
Platform-specific aligned memory allocation.
void aligned_free_impl(void *ptr)
Platform-specific aligned memory deallocation.
std::unique_ptr< memory_pool > make_memory_pool()
Create a memory pool with default configuration.
std::vector< memory_pool_config > create_default_pool_configs()
Create default pool configurations for different use cases.
Result pattern type definitions for monitoring system.
Extended error information with context.
Configuration for memory pool.
size_t block_size
Size of each block in bytes.
bool use_thread_local_cache
Use thread-local caching.
size_t alignment
Memory alignment (must be power of 2)
size_t max_blocks
Maximum number of blocks (0 = unlimited)
size_t initial_blocks
Initial number of blocks.
bool validate() const
Validate configuration.
Statistics for memory pool operations.
double get_allocation_success_rate() const
Get allocation success rate.
std::atomic< size_t > total_allocations
std::atomic< size_t > total_deallocations
memory_pool_statistics(memory_pool_statistics &&other) noexcept
std::atomic< size_t > peak_usage
memory_pool_statistics()=default
std::atomic< size_t > allocation_failures
memory_pool_statistics & operator=(memory_pool_statistics &&other) noexcept
memory_pool_statistics & operator=(const memory_pool_statistics &other)
memory_pool_statistics(const memory_pool_statistics &other)
void reset()
Reset all statistics.