/src/ocudu/lib/support/byte_buffer_chain.cpp
Line | Count | Source |
1 | | // SPDX-FileCopyrightText: Copyright (C) 2021-2026 Software Radio Systems Limited |
2 | | // SPDX-License-Identifier: BSD-3-Clause-Open-MPI |
3 | | |
4 | | #include "ocudu/adt/byte_buffer_chain.h" |
5 | | #include "ocudu/adt/detail/byte_buffer_segment_pool.h" |
6 | | #include "ocudu/ocudulog/ocudulog.h" |
7 | | #include "ocudu/support/memory_pool/memory_pool_utils.h" |
8 | | |
9 | | using namespace ocudu; |
10 | | |
11 | | byte_buffer_chain::byte_buffer_chain(void* mem) : |
12 | 0 | mem_block(mem), |
13 | | // Number of slices that fit in the memory block. |
14 | 0 | max_slices(detail::get_default_byte_buffer_segment_pool().memory_block_size() / sizeof(buffer_storage_type)), |
15 | | // Initialize slice array in the allocated memory block. |
16 | 0 | slices_ptr(static_cast<byte_buffer_slice*>(align_next(mem_block.get(), alignof(buffer_storage_type)))) |
17 | 0 | { |
18 | 0 | ocudu_assert(mem, "Invalid memory block"); |
19 | 0 | } |
20 | | |
21 | | expected<byte_buffer_chain> byte_buffer_chain::create() |
22 | 0 | { |
23 | | // Allocate memory block from pool for the array of slices. |
24 | 0 | auto* mem_block = detail::get_default_byte_buffer_segment_pool().allocate_node(); |
25 | 0 | if (mem_block == nullptr) { |
26 | 0 | ocudulog::fetch_basic_logger("ALL").warning("POOL: Failed to allocate memory block for byte_buffer_chain"); |
27 | 0 | return make_unexpected(default_error_t{}); |
28 | 0 | } |
29 | | |
30 | 0 | return byte_buffer_chain{mem_block}; |
31 | 0 | } |
32 | | |
33 | | void byte_buffer_chain::block_deleter::operator()(void* p) |
34 | 0 | { |
35 | 0 | if (p != nullptr) { |
36 | 0 | detail::byte_buffer_segment_pool::get_instance().deallocate_node(p); |
37 | 0 | } |
38 | 0 | } |