7 #ifndef ZPP_INCLUDE_ZPP_MEM_SLAB_HPP
8 #define ZPP_INCLUDE_ZPP_MEM_SLAB_HPP
10 #include <zephyr/kernel.h>
11 #include <zephyr/sys/__assert.h>
26 template<
class T_MemSlab>
80 template<
class T_Rep,
class T_Period>
84 using namespace std::chrono;
180 template<u
int32_t T_BlockSize, u
int32_t T_BlockCount, u
int32_t T_Align=sizeof(
void*)>
183 static_assert(T_BlockCount > 0);
185 static_assert(T_Align >=
sizeof(
void*));
187 static_assert(T_BlockSize >= T_Align);
188 static_assert((T_BlockSize % T_Align) == 0);
199 k_mem_slab_init(&m_mem_slab, m_mem_buffer.data(), T_BlockSize, T_BlockCount);
223 alignas(T_Align) std::array<uint8_t, T_BlockSize * T_BlockCount> m_mem_buffer;
245 __ASSERT_NO_MSG(m_mem_slab_ptr !=
nullptr);
254 template<
class T_MemSlab>
256 : m_mem_slab_ptr(m.native_handle())
258 __ASSERT_NO_MSG(m_mem_slab_ptr !=
nullptr);
272 __ASSERT_NO_MSG(m_mem_slab_ptr !=
nullptr);
284 template<
class T_MemSlab>
288 __ASSERT_NO_MSG(m_mem_slab_ptr !=
nullptr);
299 return m_mem_slab_ptr;
309 return m_mem_slab_ptr;
Allocator that uses k_mem_slab for memory.
constexpr auto free_block_count() noexcept
get current number of free blocks
native_type const * native_const_pointer
mem_slab_base & operator=(mem_slab_base &&)=delete
auto native_handle() const noexcept -> native_const_pointer
get the native zephyr mem slab handle.
constexpr auto total_block_count() const noexcept
get maximm number of blocks that can be allocated
mem_slab_base(const mem_slab_base &)=delete
void * try_allocate_for(const std::chrono::duration< T_Rep, T_Period > &timeout) noexcept
try allocate a memory block waiting with a timeout
constexpr auto block_size() const noexcept
the size of the memory blocks
constexpr auto used_block_count() noexcept
get current number of used blocks
struct k_mem_slab native_type
mem_slab_base(mem_slab_base &&)=delete
mem_slab_base & operator=(const mem_slab_base &)=delete
native_type * native_pointer
void * try_allocate() noexcept
try allocate a memory block, not waiting
void * allocate() noexcept
allocate a memory block, waiting forever
void deallocate(void *vp) noexcept
deallocate memory
constexpr mem_slab_base() noexcept
auto native_handle() noexcept -> native_pointer
get the native zephyr mem slab handle.
A mem_slab class referencing another mem slab object.
constexpr mem_slab_ref & operator=(native_pointer m) noexcept
Assign a new native k_mem_slab* object.
constexpr mem_slab_ref(T_MemSlab &m) noexcept
Construct a reference to another mem_slab object.
constexpr auto native_handle() const noexcept -> native_const_pointer
get the native zephyr mem slab handle.
constexpr mem_slab_ref & operator=(T_MemSlab &m) noexcept
Assign a new native mem slab object.
constexpr mem_slab_ref(native_pointer m) noexcept
Construct a reference to a native k_mem_slab*.
constexpr auto native_handle() noexcept -> native_pointer
get the native zephyr mem slab handle.
constexpr auto native_handle() noexcept -> native_pointer
get the native zephyr mem slab handle.
mem_slab & operator=(mem_slab &&)=delete
mem_slab & operator=(const mem_slab &)=delete
mem_slab(const mem_slab &)=delete
mem_slab() noexcept
Default constructor.
constexpr auto native_handle() const noexcept -> native_const_pointer
get the native zephyr mem slab handle.
mem_slab(mem_slab &&)=delete
constexpr k_timeout_t to_timeout(const std::chrono::duration< T_Rep, T_Period > &d) noexcept
convert a duration to tick
consteval bool is_power_of_two(uint32_t value) noexcept
check if a value is a power of two
consteval bool is_multiple_of(uint32_t value, uint32_t base)
Check if a value is a multiple of another value.