Skip to content

Commit

Permalink
core: fix allocation/memory-resource link
Browse files Browse the repository at this point in the history
  • Loading branch information
quesnel committed Jun 17, 2024
1 parent 0752aad commit f9f3c46
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 94 deletions.
32 changes: 21 additions & 11 deletions lib/include/irritator/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ class fixed_linear_memory_resource
size_t /*alignment*/) noexcept
{}

/** Release memory provides in contructor or in release memory. */
void destroy() noexcept;

//! @brief Reset the use of the chunk of memory.
void reset() noexcept;

Expand Down Expand Up @@ -618,15 +621,22 @@ class freelist_memory_resource

void deallocate(void* ptr, size_t /*bytes*/, size_t /*alignment*/) noexcept;

//! @brief Reset the use of the chunk of memory.
/** Release memory provides in contructor or in release memory. */
void destroy() noexcept;

/** Reset the use of the chunk of memory. If the chunk is undefined do
* nothing.
* @attention Release of all memory of container/memory_resource using old
* chunk. */
void reset() noexcept;

//! @brief Assign a chunk of memory.
//!
//! @attention Use this function only when no chunk of memory are allocated
//! (ie the default constructor was called).
//! @param data The new buffer. Must be not null.
//! @param size The size of the buffer. Must be not null.
/** Assign a new chunk of memory to the memory resource. If the new chunk is
* undefined do nothing.
* @attention Release of all memory of container/memory_resource using old
* chunk.
* @param data The new buffer. Must be not null.
* @param size The size of the buffer. Must be not null.
*/
void reset(std::byte* data, std::size_t size) noexcept;

//! Get the pointer to the allocated memory provided in `constructor` or in
Expand Down Expand Up @@ -1518,8 +1528,8 @@ class ring_buffer
using memory_resource_t = typename A::memory_resource_t;

static_assert((std::is_nothrow_constructible_v<T> ||
std::is_nothrow_move_constructible_v<
T>)&&std::is_nothrow_destructible_v<T>);
std::is_nothrow_move_constructible_v<T>) &&
std::is_nothrow_destructible_v<T>);

private:
T* buffer = nullptr;
Expand Down Expand Up @@ -1824,8 +1834,8 @@ class small_ring_buffer
public:
static_assert(length >= 1);
static_assert((std::is_nothrow_constructible_v<T> ||
std::is_nothrow_move_constructible_v<
T>)&&std::is_nothrow_destructible_v<T>);
std::is_nothrow_move_constructible_v<T>) &&
std::is_nothrow_destructible_v<T>);

using value_type = T;
using size_type = small_storage_size_t<length>;
Expand Down
58 changes: 26 additions & 32 deletions lib/include/irritator/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1297,52 +1297,38 @@ class simulation

template<typename T>
concept has_lambda_function = requires(T t, simulation& sim) {
{
t.lambda(sim)
} -> std::same_as<status>;
{ t.lambda(sim) } -> std::same_as<status>;
};

template<typename T>
concept has_transition_function =
requires(T t, simulation& sim, time s, time e, time r) {
{
t.transition(sim, s, e, r)
} -> std::same_as<status>;
{ t.transition(sim, s, e, r) } -> std::same_as<status>;
};

template<typename T>
concept has_observation_function = requires(T t, time s, time e) {
{
t.observation(s, e)
} -> std::same_as<observation_message>;
{ t.observation(s, e) } -> std::same_as<observation_message>;
};

template<typename T>
concept has_initialize_function = requires(T t, simulation& sim) {
{
t.initialize(sim)
} -> std::same_as<status>;
{ t.initialize(sim) } -> std::same_as<status>;
};

template<typename T>
concept has_finalize_function = requires(T t, simulation& sim) {
{
t.finalize(sim)
} -> std::same_as<status>;
{ t.finalize(sim) } -> std::same_as<status>;
};

template<typename T>
concept has_input_port = requires(T t) {
{
t.x
};
{ t.x };
};

template<typename T>
concept has_output_port = requires(T t) {
{
t.y
};
{ t.y };
};

constexpr observation_message qss_observation(real X,
Expand Down Expand Up @@ -5410,6 +5396,7 @@ inline void simulation::realloc(
const simulation_memory_requirement& init) noexcept
{
debug::ensure(init.valid());
destroy();

shared.reset(m_alloc.allocate_bytes(init.simulation_b), init.simulation_b);
nodes_alloc.reset(m_alloc.allocate_bytes(init.connections_b),
Expand Down Expand Up @@ -5463,17 +5450,24 @@ inline void simulation::destroy() noexcept
hsms.destroy();
srcs.destroy();

shared.reset();
nodes_alloc.reset();
dated_messages_alloc.reset();
external_source_alloc.reset();

m_alloc.deallocate_bytes(shared.head(), shared.capacity());
m_alloc.deallocate_bytes(nodes_alloc.head(), nodes_alloc.capacity());
m_alloc.deallocate_bytes(dated_messages_alloc.head(),
dated_messages_alloc.capacity());
m_alloc.deallocate_bytes(external_source_alloc.head(),
external_source_alloc.capacity());
if (nodes_alloc.head())
m_alloc.deallocate_bytes(nodes_alloc.head(), nodes_alloc.capacity());

if (dated_messages_alloc.head())
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();
}

inline simulation::~simulation() noexcept { destroy(); }
Expand Down
8 changes: 4 additions & 4 deletions lib/include/irritator/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ struct e_errno {
};

struct e_memory {
long long unsigned int capacity; //!< Current capacity in bytes.
long long unsigned int request; //!< Requested capacity in bytes.
long long unsigned int capacity{}; //!< Current capacity in bytes.
long long unsigned int request{}; //!< Requested capacity in bytes.
};

struct e_allocator {
long long unsigned int needed;
long long unsigned int capacity;
size_t needed{};
size_t capacity{};
};

struct e_json {
Expand Down
23 changes: 16 additions & 7 deletions lib/include/irritator/modeling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1444,11 +1444,11 @@ class project
const char* filename) noexcept;

struct required_data {
unsigned tree_node_nb{};
unsigned model_nb{};
unsigned hsm_nb{};
unsigned tree_node_nb{ 1u };
unsigned model_nb{ 0u };
unsigned hsm_nb{ 0u };

constexpr static friend required_data operator+(
constexpr friend required_data operator+(
const required_data lhs,
const required_data rhs) noexcept
{
Expand All @@ -1464,12 +1464,21 @@ class project
hsm_nb += other.hsm_nb;
return *this;
}

/** Apply boundaries for all values. */
constexpr void fix() noexcept
{
tree_node_nb = std::clamp(tree_node_nb, 1u, UINT32_MAX >> 16);
model_nb = std::clamp(model_nb, 16u, UINT32_MAX >> 2);
hsm_nb = std::clamp(hsm_nb, 0u, UINT32_MAX >> 2);
}
};

/** Compute the number of @c tree_node required to load the component @c
* into the @c project and the number of models and connections to fill the
* simulation structures. */
required_data compute_treenode_number(const modeling& mod,
* into the @c project and the number of @c irt::model and @c
* irt::hierarchical_state_machine to fill the @C irt::simulation
* structures. */
required_data compute_memory_required(const modeling& mod,
const component& c) const noexcept;

/// Assign a new @c component head. The previously allocated tree_node
Expand Down
59 changes: 42 additions & 17 deletions lib/src/memory-resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ void* malloc_memory_resource_allocate_win32(std::size_t bytes,
debug::ensure(is_alignment(alignment));
debug::ensure((bytes % alignment) == 0);

using fn = void* (*)(std::size_t, std::size_t) noexcept;
fn call = reinterpret_cast<fn>(::_aligned_malloc);
using fn = void* (*)(std::size_t, std::size_t) noexcept;
fn call = reinterpret_cast<fn>(::_aligned_malloc);

return call(bytes, alignment);
}
Expand Down Expand Up @@ -60,8 +60,8 @@ void* malloc_memory_resource_allocate_posix(std::size_t bytes,
debug::ensure(is_alignment(alignment));
debug::ensure((bytes % alignment) == 0);

using fn = void* (*)(std::size_t, std::size_t) noexcept;
fn call = reinterpret_cast<fn>(std::aligned_alloc);
using fn = void* (*)(std::size_t, std::size_t) noexcept;
fn call = reinterpret_cast<fn>(std::aligned_alloc);

return call(alignment, bytes);
}
Expand Down Expand Up @@ -154,6 +154,13 @@ void* fixed_linear_memory_resource::allocate(size_t bytes,
return reinterpret_cast<void*>(next_address);
}

void fixed_linear_memory_resource::destroy() noexcept
{
m_start = nullptr;
m_total_size = 0;
m_offset = 0;
}

void fixed_linear_memory_resource::reset() noexcept { m_offset = { 0 }; }

void fixed_linear_memory_resource::reset(std::byte* data,
Expand Down Expand Up @@ -337,33 +344,51 @@ void freelist_memory_resource::deallocate(void* ptr,
merge(prev, freeNode);
}

void freelist_memory_resource::destroy() noexcept
{
m_start_ptr = nullptr;
m_total_size = 0;
m_used = 0;
m_peak = 0;
m_find_policy = find_policy::find_first;
}

void freelist_memory_resource::reset() noexcept
{
m_used = 0u;
m_peak = 0u;

node* first = reinterpret_cast<node*>(m_start_ptr);
first->data.block_size = m_total_size;
first->next = nullptr;
if (not m_start_ptr) {
destroy();
} else {
node* first = reinterpret_cast<node*>(m_start_ptr);
first->data.block_size = m_total_size;
first->next = nullptr;

m_freeList.head = nullptr;
m_freeList.insert(nullptr, first);
m_freeList.head = nullptr;
m_freeList.insert(nullptr, first);
}
}

void freelist_memory_resource::reset(std::byte* data, std::size_t size) noexcept
{
debug::ensure(data != nullptr);
debug::ensure(size != 0u);
debug::ensure(m_start_ptr == nullptr);
debug::ensure(m_total_size == 0u);

m_start_ptr = data;
m_total_size = size;
m_used = 0;
m_peak = 0;
m_find_policy = find_policy::find_first;
if (data == nullptr or size == 0)
return;

reset();
destroy();

m_start_ptr = data;
m_total_size = size;

node* first = reinterpret_cast<node*>(m_start_ptr);
first->data.block_size = m_total_size;
first->next = nullptr;

m_freeList.head = nullptr;
m_freeList.insert(nullptr, first);
}

void freelist_memory_resource::merge(node* previous, node* free_node) noexcept
Expand Down
Loading

0 comments on commit f9f3c46

Please sign in to comment.