Common: Add a memory pool implementation, remove use of boost::pool

This commit is contained in:
MerryMage 2016-08-06 20:41:00 +01:00
parent 411e804b0d
commit 4d127c19dd
6 changed files with 85 additions and 5 deletions

View file

@ -0,0 +1,44 @@
/* This file is part of the dynarmic project.
* Copyright (c) 2016 MerryMage
* This software may be used and distributed according to the terms of the GNU
* General Public License version 2 or any later version.
*/
#include <cstdlib>
#include "common/memory_pool.h"
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;
}
Pool::~Pool() {
std::free(current_slab);
for (char* slab : slabs) {
std::free(slab);
}
}
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;
}
void* ret = (void*)current_ptr;
current_ptr += object_size;
remaining--;
return ret;
}
} // namespace Common
} // namespace Dynarmic

36
src/common/memory_pool.h Normal file
View file

@ -0,0 +1,36 @@
/* This file is part of the dynarmic project.
* Copyright (c) 2016 MerryMage
* This software may be used and distributed according to the terms of the GNU
* General Public License version 2 or any later version.
*/
#pragma once
#include <vector>
#include "common/common_types.h"
namespace Dynarmic {
namespace Common {
class Pool {
public:
Pool(size_t object_size, size_t initial_pool_size);
~Pool();
Pool(Pool&) = delete;
Pool(Pool&&) = delete;
void* Alloc();
private:
size_t object_size;
size_t slab_size;
char* current_slab;
char* current_ptr;
size_t remaining;
std::vector<char*> slabs;
};
} // namespace Common
} // namespace Dynarmic