From b41de890fb01fc8e1e3c8127efd42e62c23a3a0f Mon Sep 17 00:00:00 2001 From: Mat M Date: Wed, 7 Sep 2016 08:20:42 -0400 Subject: [PATCH] memory_pool: Deduplicate slab allocation code (#28) --- src/common/memory_pool.cpp | 17 ++++++++++------- src/common/memory_pool.h | 5 +++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/common/memory_pool.cpp b/src/common/memory_pool.cpp index 32de1236..71c102a2 100644 --- a/src/common/memory_pool.cpp +++ b/src/common/memory_pool.cpp @@ -12,9 +12,7 @@ namespace Dynarmic { namespace Common { Pool::Pool(size_t object_size, size_t initial_pool_size) : object_size(object_size), slab_size(initial_pool_size) { - current_slab = (char*)std::malloc(object_size * slab_size); - current_ptr = current_slab; - remaining = slab_size; + AllocateNewSlab(); } Pool::~Pool() { @@ -28,17 +26,22 @@ Pool::~Pool() { void* Pool::Alloc() { if (remaining == 0) { slabs.emplace_back(current_slab); - current_slab = (char*)std::malloc(object_size * slab_size); - current_ptr = current_slab; - remaining = slab_size; + AllocateNewSlab(); } - void* ret = (void*)current_ptr; + void* ret = static_cast(current_ptr); current_ptr += object_size; remaining--; return ret; } +void Pool::AllocateNewSlab() { + current_slab = static_cast(std::malloc(object_size * slab_size)); + current_ptr = current_slab; + remaining = slab_size; +} + + } // namespace Common } // namespace Dynarmic diff --git a/src/common/memory_pool.h b/src/common/memory_pool.h index fc64958b..1099e62f 100644 --- a/src/common/memory_pool.h +++ b/src/common/memory_pool.h @@ -28,6 +28,11 @@ public: void* Alloc(); private: + // Allocates a completely new memory slab. + // Used when an entirely new slab is needed + // due the current one running out of usable space. + void AllocateNewSlab(); + size_t object_size; size_t slab_size; char* current_slab;