Skip to content

Commit

Permalink
core: simulation/external-source allocator refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
quesnel committed Jun 21, 2024
1 parent c861fb2 commit ee799af
Showing 1 changed file with 44 additions and 27 deletions.
71 changes: 44 additions & 27 deletions lib/include/irritator/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,10 +803,32 @@ class external_source
data_array<random_source, random_source_id, freelist_allocator>
random_sources;

/** Use the global `malloc_memory_resource` to prepare the
* `free_list_allocator` to allocate memory. Use the `realloc` function
* after this constructor to allocate memory. */
external_source() noexcept
: external_source(get_malloc_memory_resource())
{}

/** Use the specified memory resource `mem` to prepare the
* `free_list_allocator` to allocate memory. Use the `realloc` function
* after this constructor to allocate memory. */
external_source(memory_resource* mem) noexcept
: alloc(mem)
, constant_sources(&shared)
, binary_file_sources(&shared)
, text_file_sources(&shared)
, random_sources(&shared)
{}

/** Use the global `malloc_memory_resource` to prepare `free_list_allocator`
* and allocate memory according to the `init` values. */
external_source(const external_source_memory_requirement& init) noexcept
: external_source(get_malloc_memory_resource(), init)
{}

/** Use the specified memory resource `mem` to prepare `free_list_allocator`
* and allocate memory according to the `init` values. */
external_source(memory_resource* mem,
const external_source_memory_requirement& init) noexcept
: alloc(mem)
Expand Down Expand Up @@ -1180,7 +1202,6 @@ class simulation
freelist_memory_resource shared;
freelist_memory_resource nodes_alloc;
freelist_memory_resource dated_messages_alloc;
freelist_memory_resource external_source_alloc;

vector<output_message, freelist_allocator> emitting_output_ports;
vector<model_id, freelist_allocator> immediate_models;
Expand Down Expand Up @@ -1208,6 +1229,12 @@ class simulation
template<typename Dynamics>
model_id get_id(const Dynamics& dyn) const;

private:
/** Allocate new buffers for all memory resource and container. Do not call
* destroy before. This function is used in `constructors` or in `realloc()`
* function after the call to `destroy()`. */
void do_realloc(const simulation_memory_requirement& init) noexcept;

public:
//! Use the default malloc memory resource to allocate all memory need
//! by sub-containers.
Expand Down Expand Up @@ -5511,12 +5538,12 @@ inline simulation::simulation(
, messages(&shared)
, dated_messages(&shared)
, sched(&shared)
, srcs{ mem, init.srcs }
, srcs{ mem }
{
realloc(init);
do_realloc(init);
}

inline void simulation::realloc(
inline void simulation::do_realloc(
const simulation_memory_requirement& init) noexcept
{
debug::ensure(init.valid());
Expand All @@ -5527,8 +5554,6 @@ inline void simulation::realloc(
init.connections_b);
dated_messages_alloc.reset(m_alloc.allocate_bytes(init.dated_messages_b),
init.dated_messages_b);
external_source_alloc.reset(m_alloc.allocate_bytes(init.external_source_b),
init.external_source_b);

if (init.model_nb > 0) {
models.reserve(init.model_nb);
Expand All @@ -5547,17 +5572,14 @@ inline void simulation::realloc(
if (init.hsm_nb > 0)
hsms.reserve(init.hsm_nb);

if (init.srcs.constant_nb > 0)
srcs.constant_sources.reserve(init.srcs.constant_nb);

if (init.srcs.binary_file_nb > 0)
srcs.binary_file_sources.reserve(init.srcs.binary_file_nb);

if (init.srcs.text_file_nb > 0)
srcs.text_file_sources.reserve(init.srcs.text_file_nb);
srcs.realloc(init.srcs);
}

if (init.srcs.random_nb > 0)
srcs.random_sources.reserve(init.srcs.random_nb);
inline void simulation::realloc(
const simulation_memory_requirement& init) noexcept
{
destroy();
do_realloc(init);
}

inline void simulation::destroy() noexcept
Expand All @@ -5581,17 +5603,13 @@ inline void simulation::destroy() noexcept
m_alloc.deallocate_bytes(dated_messages_alloc.head(),
dated_messages_alloc.capacity());

if (external_source_alloc.head())
m_alloc.deallocate_bytes(external_source_alloc.head(),
external_source_alloc.capacity());

if (shared.head())
m_alloc.deallocate_bytes(shared.head(), shared.capacity());

nodes_alloc.destroy();
dated_messages_alloc.destroy();
external_source_alloc.destroy();
shared.destroy();
srcs.destroy();
}

inline simulation::~simulation() noexcept { destroy(); }
Expand Down Expand Up @@ -6503,14 +6521,13 @@ inline constexpr void simulation_memory_requirement::compute_buffer_size(
constexpr size_t alg = alignof(std::max_align_t);

global_b = bytes;
connections_b = make_divisible_to(bytes * *connections / 100, alg);
dated_messages_b = make_divisible_to(bytes * *dated_messages / 100, alg);
external_source_b = make_divisible_to(bytes * *external_source / 100, alg);
const auto margin = bytes - ((bytes * 10) / 100);
connections_b = make_divisible_to(margin * *connections / 100, alg);
dated_messages_b = make_divisible_to(margin * *dated_messages / 100, alg);
external_source_b = make_divisible_to(margin * *external_source / 100, alg);
simulation_b = make_divisible_to(
bytes - (connections_b + dated_messages_b + external_source_b), alg);
margin - (connections_b + dated_messages_b + external_source_b), alg);

debug::ensure(connections_b + dated_messages_b + external_source_b < bytes);
debug::ensure(simulation_b > 0);
debug::ensure(connections_b + dated_messages_b + external_source_b +
simulation_b <=
bytes);
Expand Down

0 comments on commit ee799af

Please sign in to comment.