Skip to content

Commit

Permalink
fixed usage of default c++ new/delete memory resource
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 committed Jan 31, 2025
1 parent 6ed86a4 commit 9a978c4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
6 changes: 5 additions & 1 deletion libcyphal_demo/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ namespace
constexpr std::size_t HeapSize = 16ULL * 1024ULL;
alignas(O1HEAP_ALIGNMENT) std::array<cetl::byte, HeapSize> s_heap_arena{}; // NOLINT

constexpr std::size_t BlockHeapSize = 128ULL * 1024ULL;
alignas(O1HEAP_ALIGNMENT) std::array<cetl::byte, BlockHeapSize> s_block_heap_arena{}; // NOLINT

} // namespace

Application::Application(const char* const root_path)
: o1_heap_mr_{s_heap_arena}
, media_block_mr_{*cetl::pmr::new_delete_resource()}
, o1_block_heap_mr_{s_block_heap_arena}
, media_block_mr_{o1_block_heap_mr_}
, storage_{root_path}
, registry_{o1_heap_mr_}
, regs_{o1_heap_mr_, registry_, media_block_mr_}
Expand Down
2 changes: 1 addition & 1 deletion libcyphal_demo/src/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <cetl/pf17/cetlpf.hpp>
#include <libcyphal/application/registry/register.hpp>
#include <libcyphal/application/registry/registry_impl.hpp>
#include <libcyphal/platform/storage.hpp>

#include <algorithm>
#include <array>
Expand Down Expand Up @@ -247,6 +246,7 @@ class Application final

platform::Linux::EpollSingleThreadedExecutor executor_;
platform::O1HeapMemoryResource o1_heap_mr_;
platform::O1HeapMemoryResource o1_block_heap_mr_;
platform::BlockMemoryResource media_block_mr_;
platform::storage::KeyValue storage_;
libcyphal::application::registry::Registry registry_;
Expand Down
1 change: 1 addition & 0 deletions libcyphal_demo/src/no_cpp_heap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cetl/cetl.hpp>

#include <cstddef>
#include <cstdlib>
#include <iostream>

#if (__cplusplus >= CETL_CPP_STANDARD_17)
Expand Down
25 changes: 13 additions & 12 deletions libcyphal_demo/src/platform/block_memory_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class BlockMemoryResource final : public cetl::pmr::memory_resource
}; // Diagnostics

explicit BlockMemoryResource(cetl::pmr::memory_resource& memory)
: pool_ptr_{nullptr, {&memory, 0U}}
: memory_{memory}
, pool_ptr_{nullptr, {&memory, 0U}}
{
}

Expand All @@ -63,8 +64,7 @@ class BlockMemoryResource final : public cetl::pmr::memory_resource
CETL_DEBUG_ASSERT(pool_size >= alignment, "");
CETL_DEBUG_ASSERT(alignment && !(alignment & (alignment - 1)), "Should be a power of 2");

auto* const mr = pool_ptr_.get_deleter().resource();
pool_ptr_ = PoolPtr{mr->allocate(pool_size), {mr, pool_size}};
pool_ptr_ = PoolPtr{memory_.allocate(pool_size), {&memory_, pool_size}};
if (!pool_ptr_)
{
CETL_DEBUG_ASSERT(false, "Failed to allocate memory pool");
Expand Down Expand Up @@ -176,15 +176,16 @@ class BlockMemoryResource final : public cetl::pmr::memory_resource
private:
using PoolPtr = std::unique_ptr<void, cetl::pmr::MemoryResourceDeleter<cetl::pmr::memory_resource>>;

PoolPtr pool_ptr_;
std::size_t alignment_{0U};
void** head_{nullptr};
std::size_t block_count_{0U};
std::size_t block_size_{0U};
std::size_t used_blocks_{0U};
std::size_t used_blocks_peak_{0U};
std::size_t request_count_{0U};
std::size_t oom_count_{0U};
cetl::pmr::memory_resource& memory_;
PoolPtr pool_ptr_;
std::size_t alignment_{0U};
void** head_{nullptr};
std::size_t block_count_{0U};
std::size_t block_size_{0U};
std::size_t used_blocks_{0U};
std::size_t used_blocks_peak_{0U};
std::size_t request_count_{0U};
std::size_t oom_count_{0U};

// See `do_allocate` special case for zero bytes.
// Note that we still need at least one byte - b/c `std::array<..., 0>::data()` returns `nullptr`.
Expand Down
1 change: 1 addition & 0 deletions libcyphal_demo/src/platform/o1_heap_memory_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>

namespace platform
Expand Down

0 comments on commit 9a978c4

Please sign in to comment.