2016-08-06 20:41:00 +01:00
|
|
|
/* 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"
|
|
|
|
|
2018-01-26 13:51:48 +00:00
|
|
|
namespace Dynarmic::Common {
|
2016-08-06 20:41:00 +01:00
|
|
|
|
|
|
|
Pool::Pool(size_t object_size, size_t initial_pool_size) : object_size(object_size), slab_size(initial_pool_size) {
|
2016-09-07 13:20:42 +01:00
|
|
|
AllocateNewSlab();
|
2016-08-06 20:41:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Pool::~Pool() {
|
|
|
|
std::free(current_slab);
|
|
|
|
|
|
|
|
for (char* slab : slabs) {
|
|
|
|
std::free(slab);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void* Pool::Alloc() {
|
|
|
|
if (remaining == 0) {
|
|
|
|
slabs.emplace_back(current_slab);
|
2016-09-07 13:20:42 +01:00
|
|
|
AllocateNewSlab();
|
2016-08-06 20:41:00 +01:00
|
|
|
}
|
|
|
|
|
2016-09-07 13:20:42 +01:00
|
|
|
void* ret = static_cast<void*>(current_ptr);
|
2016-08-06 20:41:00 +01:00
|
|
|
current_ptr += object_size;
|
|
|
|
remaining--;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-09-07 13:20:42 +01:00
|
|
|
void Pool::AllocateNewSlab() {
|
|
|
|
current_slab = static_cast<char*>(std::malloc(object_size * slab_size));
|
|
|
|
current_ptr = current_slab;
|
|
|
|
remaining = slab_size;
|
|
|
|
}
|
|
|
|
|
2018-01-26 13:51:48 +00:00
|
|
|
} // namespace Dynarmic::Common
|