From 4d127c19dd288a96961d43a9d3b4cc33a82b3ea7 Mon Sep 17 00:00:00 2001 From: MerryMage Date: Sat, 6 Aug 2016 20:41:00 +0100 Subject: [PATCH] Common: Add a memory pool implementation, remove use of boost::pool --- CMakeLists.txt | 2 -- src/CMakeLists.txt | 2 ++ src/common/memory_pool.cpp | 44 ++++++++++++++++++++++++++++++++++ src/common/memory_pool.h | 36 ++++++++++++++++++++++++++++ src/frontend/ir/ir.h | 4 ++-- src/frontend/ir/ir_emitter.cpp | 2 +- 6 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 src/common/memory_pool.cpp create mode 100644 src/common/memory_pool.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d435469..9028a062 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,13 +10,11 @@ if (NOT MSVC) add_compile_options(--std=c++14) add_compile_options(-Wall -Werror -Wextra -pedantic -pedantic-errors -Wfatal-errors -Wno-unused-parameter -Wno-missing-braces) - add_compile_options(-DBOOST_SYSTEM_NO_DEPRECATED -DBOOST_POOL_NO_MT) if (ARCHITECTURE_x86_64) add_compile_options(-msse4.1) endif() else() add_compile_options(/W3 /MP /Zi /Zo /EHsc /WX) - add_compile_options(/DBOOST_SYSTEM_NO_DEPRECATED /DBOOST_POOL_NO_MT) endif() # This function should be passed a list of all files in a target. It will automatically generate diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a4f1a5b0..25993072 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,6 +6,7 @@ set(SRCS backend_x64/jitstate.cpp backend_x64/reg_alloc.cpp backend_x64/routines.cpp + common/memory_pool.cpp common/memory_util.cpp common/string_util.cpp common/x64/abi.cpp @@ -42,6 +43,7 @@ set(HEADERS common/bit_util.h common/code_block.h common/common_types.h + common/memory_pool.h common/memory_util.h common/mp.h common/scope_exit.h diff --git a/src/common/memory_pool.cpp b/src/common/memory_pool.cpp new file mode 100644 index 00000000..32de1236 --- /dev/null +++ b/src/common/memory_pool.cpp @@ -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 + +#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 diff --git a/src/common/memory_pool.h b/src/common/memory_pool.h new file mode 100644 index 00000000..4b08fe6d --- /dev/null +++ b/src/common/memory_pool.h @@ -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 + +#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 slabs; +}; + +} // namespace Common +} // namespace Dynarmic diff --git a/src/frontend/ir/ir.h b/src/frontend/ir/ir.h index 1097d8eb..56433ddb 100644 --- a/src/frontend/ir/ir.h +++ b/src/frontend/ir/ir.h @@ -10,13 +10,13 @@ #include #include -#include #include #include #include #include "common/assert.h" #include "common/common_types.h" +#include "common/memory_pool.h" #include "frontend/arm_types.h" #include "frontend/ir/opcodes.h" @@ -244,7 +244,7 @@ public: /// List of instructions in this block. boost::intrusive::list instructions; /// Memory pool for instruction list - std::unique_ptr> instruction_alloc_pool = std::make_unique>(sizeof(Inst)); + std::unique_ptr instruction_alloc_pool = std::make_unique(sizeof(Inst), 4096); /// Terminal instruction of this block. Terminal terminal = Term::Invalid{}; diff --git a/src/frontend/ir/ir_emitter.cpp b/src/frontend/ir/ir_emitter.cpp index 7a702acf..f472679b 100644 --- a/src/frontend/ir/ir_emitter.cpp +++ b/src/frontend/ir/ir_emitter.cpp @@ -352,7 +352,7 @@ void IREmitter::SetTerm(const IR::Terminal& terminal) { } IR::Value IREmitter::Inst(IR::Opcode op, std::initializer_list args) { - IR::Inst* inst = new(block.instruction_alloc_pool->malloc()) IR::Inst(op); + IR::Inst* inst = new(block.instruction_alloc_pool->Alloc()) IR::Inst(op); DEBUG_ASSERT(args.size() == inst->NumArgs()); std::for_each(args.begin(), args.end(), [&inst, op, index = size_t(0)](const auto& v) mutable {