Skip to content

Commit

Permalink
core: adds more control to simulation/project reallocation
Browse files Browse the repository at this point in the history
  • Loading branch information
quesnel committed Jun 15, 2024
1 parent 31a46e4 commit 0752aad
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 46 deletions.
21 changes: 11 additions & 10 deletions app/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ void show_version() noexcept
VERSION_TWEAK);
}

void run_simulation(irt::real begin,
irt::real duration,
int /* models */,
int /* messages */,
void run_simulation(irt::real begin,
irt::real duration,
int /* models */,
int /* messages */,
const char* file_name) noexcept
{
fmt::print("Run simulation from `{}' to `{}' for file {}\n",
Expand All @@ -259,12 +259,13 @@ void run_simulation(irt::real begin,

irt::attempt_all(
[&]() noexcept -> irt::status {
irt::project pj;
irt::modeling_initializer init;
irt::modeling mod;
irt::simulation sim;
irt::external_source srcs;
irt::cache_rw cache;
irt::project pj;
irt::modeling_initializer init;
irt::modeling mod;
irt::simulation_memory_requirement smr{ 1024 * 1024 * 8 };
irt::simulation sim{ smr };
irt::external_source srcs;
irt::cache_rw cache;

irt_check(pj.init(init));
irt_check(mod.init(init));
Expand Down
29 changes: 29 additions & 0 deletions lib/include/irritator/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,23 @@ class memory_resource
do_deallocate(pointer, bytes, alignment);
}

bool can_alloc(
std::size_t bytes,
std::size_t alignment = alignof(std::max_align_t)) const noexcept
{
return do_can_alloc(bytes, alignment);
}

protected:
virtual void* do_allocate(std::size_t bytes,
std::size_t alignment) noexcept = 0;

virtual void do_deallocate(void* pointer,
std::size_t bytes,
std::size_t alignment) noexcept = 0;

virtual bool do_can_alloc(std::size_t bytes,
std::size_t alignment) const noexcept = 0;
};

//! A dynamic heap `memory_resource` using `std::aligned_alloc` or
Expand All @@ -107,6 +117,12 @@ class malloc_memory_resource final : public memory_resource
void do_deallocate(void* pointer,
std::size_t bytes,
std::size_t alignment) noexcept override;

bool do_can_alloc(std::size_t /*bytes*/,
std::size_t /*alignment*/) const noexcept override
{
return true;
}
};

//! A stack `memory_resource` using an `std::array` to store aligned memory.
Expand Down Expand Up @@ -192,6 +208,12 @@ class static_memory_resource final : public memory_resource

debug::ensure(first <= pos and pos <= last);
}

bool do_can_alloc(std::size_t bytes,
std::size_t /*alignment*/) const noexcept override
{
return bytes < Bytes;
}
};

inline malloc_memory_resource* get_malloc_memory_resource() noexcept
Expand Down Expand Up @@ -286,6 +308,13 @@ class mr_allocator
m_mr->deallocate(pointer, bytes, alignment);
}

bool can_alloc_bytes(
size_type bytes,
size_type alignment = alignof(std::max_align_t)) noexcept
{
return m_mr->can_alloc(bytes, alignment);
}

template<typename T>
bool can_alloc(std::size_t element_count) const noexcept
{
Expand Down
Loading

0 comments on commit 0752aad

Please sign in to comment.