memory_pool: Deduplicate slab allocation code (#28)
This commit is contained in:
parent
d646c3119d
commit
b41de890fb
2 changed files with 15 additions and 7 deletions
|
@ -12,9 +12,7 @@ namespace Dynarmic {
|
||||||
namespace Common {
|
namespace Common {
|
||||||
|
|
||||||
Pool::Pool(size_t object_size, size_t initial_pool_size) : object_size(object_size), slab_size(initial_pool_size) {
|
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);
|
AllocateNewSlab();
|
||||||
current_ptr = current_slab;
|
|
||||||
remaining = slab_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Pool::~Pool() {
|
Pool::~Pool() {
|
||||||
|
@ -28,17 +26,22 @@ Pool::~Pool() {
|
||||||
void* Pool::Alloc() {
|
void* Pool::Alloc() {
|
||||||
if (remaining == 0) {
|
if (remaining == 0) {
|
||||||
slabs.emplace_back(current_slab);
|
slabs.emplace_back(current_slab);
|
||||||
current_slab = (char*)std::malloc(object_size * slab_size);
|
AllocateNewSlab();
|
||||||
current_ptr = current_slab;
|
|
||||||
remaining = slab_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void* ret = (void*)current_ptr;
|
void* ret = static_cast<void*>(current_ptr);
|
||||||
current_ptr += object_size;
|
current_ptr += object_size;
|
||||||
remaining--;
|
remaining--;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Pool::AllocateNewSlab() {
|
||||||
|
current_slab = static_cast<char*>(std::malloc(object_size * slab_size));
|
||||||
|
current_ptr = current_slab;
|
||||||
|
remaining = slab_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace Common
|
} // namespace Common
|
||||||
} // namespace Dynarmic
|
} // namespace Dynarmic
|
||||||
|
|
|
@ -28,6 +28,11 @@ public:
|
||||||
void* Alloc();
|
void* Alloc();
|
||||||
|
|
||||||
private:
|
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 object_size;
|
||||||
size_t slab_size;
|
size_t slab_size;
|
||||||
char* current_slab;
|
char* current_slab;
|
||||||
|
|
Loading…
Reference in a new issue