Commit 3d065e53 authored by Gauthier Quesnel's avatar Gauthier Quesnel Committed by Gauthier Quesnel
Browse files

core: use new global allocator

Adding two global inline function pointers to allocate/deallocate
memory. This patch allows the use of external tools instead of default
malloc/free functions.
parent 62e8f86f
Pipeline #29296 passed with stage
in 1 minute and 26 seconds
......@@ -330,6 +330,12 @@ public:
return cb(obj, std::forward<Args>(args)...);
}
constexpr void reset() noexcept
{
obj = nullptr;
cb = nullptr;
}
private:
void* obj = nullptr;
R (*cb)(void*, Args...) = nullptr;
......@@ -349,6 +355,31 @@ function_ref(R (*)(Args...)) -> function_ref<R(Args...)>;
template<typename R, typename... Args>
function_ref(R (*)(Args...) noexcept) -> function_ref<R(Args...) noexcept>;
/*****************************************************************************
*
* Allocator
*
****************************************************************************/
using global_alloc_function_type = function_ref<void*(sz size)>;
using global_free_function_type = function_ref<void(void *ptr)>;
inline
void* malloc_wrapper(sz size)
{
return std::malloc(size);
}
inline
void free_wrapper(void *ptr)
{
if (ptr)
std::free(ptr);
}
static inline global_alloc_function_type g_alloc_fn = malloc_wrapper;
static inline global_free_function_type g_free_fn = free_wrapper;
/*****************************************************************************
*
* Definition of Time
......@@ -660,7 +691,7 @@ public:
~block_allocator() noexcept
{
if (blocks)
std::free(blocks);
g_free_fn(blocks);
}
status init(std::size_t new_capacity) noexcept
......@@ -670,9 +701,10 @@ public:
if (new_capacity != capacity) {
if (blocks)
std::free(blocks);
g_free_fn(blocks);
blocks =
static_cast<block*>(std::malloc(new_capacity * sizeof(block)));
static_cast<block*>(g_alloc_fn(new_capacity * sizeof(block)));
if (blocks == nullptr)
return status::block_allocator_not_enough_memory;
}
......@@ -1696,7 +1728,7 @@ public:
clear();
if (m_items)
std::free(m_items);
g_free_fn(m_items);
}
/**
......@@ -1711,7 +1743,7 @@ public:
if (capacity > get_max_size<Identifier>())
return status::data_array_init_capacity_error;
m_items = static_cast<item*>(std::malloc(capacity * sizeof(item)));
m_items = static_cast<item*>(g_alloc_fn(capacity * sizeof(item)));
if (!m_items)
return status::data_array_not_enough_memory;
......@@ -2075,7 +2107,7 @@ public:
~heap() noexcept
{
if (nodes)
std::free(nodes);
g_free_fn(nodes);
}
status init(size_t new_capacity) noexcept
......@@ -2085,9 +2117,10 @@ public:
if (new_capacity != capacity) {
if (nodes)
std::free(nodes);
g_free_fn(nodes);
nodes =
static_cast<node*>(std::malloc(new_capacity * sizeof(node)));
static_cast<node*>(g_alloc_fn(new_capacity * sizeof(node)));
if (nodes == nullptr)
return status::head_allocator_not_enough_memory;
}
......
......@@ -95,6 +95,44 @@ run_simulation(irt::simulation& sim, const double duration)
return irt::status::success;
}
struct global_alloc
{
size_t allocation_size = 0;
int allocation_number = 0;
void* operator()(size_t size)
{
allocation_size += size;
allocation_number++;
return std::malloc(size);
}
};
struct global_free
{
int free_number = 0;
void operator()(void *ptr)
{
free_number++;
if (ptr)
std::free(ptr);
}
};
static
void* null_alloc(size_t /*sz*/)
{
return nullptr;
}
static
void null_free(void *)
{
}
int
main()
{
......@@ -3102,4 +3140,34 @@ main()
expect(run_simulation(sim, 30.) == irt::status::success);
}
};
"memory"_test = [] {
global_alloc g_a;
global_free g_b;
{
irt::g_alloc_fn = g_a;
irt::g_free_fn = g_b;
irt::simulation sim;
expect(sim.init(30u, 30u) == irt::status::success);
}
expect(g_a.allocation_size > 0);
expect(g_a.allocation_number == g_b.free_number);
irt::g_alloc_fn.reset();
irt::g_free_fn.reset();
};
"null_memory"_test = [] {
irt::is_fatal_breakpoint = false;
irt::g_alloc_fn = null_alloc;
irt::g_free_fn = null_free;
irt::simulation sim;
expect(sim.init(30u, 30u) != irt::status::success);
irt::is_fatal_breakpoint = true;
};
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment