zpp
Zephyr C++20 Framework
zpp::mem_slab_base< T_MemSlab > Class Template Reference

Allocator that uses k_mem_slab for memory. More...

#include <mem_slab.hpp>

Public Types

using native_type = struct k_mem_slab
 
using native_pointer = native_type *
 
using native_const_pointer = native_type const *
 

Public Member Functions

void * allocate () noexcept
 allocate a memory block, waiting forever More...
 
void * try_allocate () noexcept
 try allocate a memory block, not waiting More...
 
template<class T_Rep , class T_Period >
void * try_allocate_for (const std::chrono::duration< T_Rep, T_Period > &timeout) noexcept
 try allocate a memory block waiting with a timeout More...
 
void deallocate (void *vp) noexcept
 deallocate memory More...
 
constexpr auto block_size () const noexcept
 the size of the memory blocks More...
 
constexpr auto total_block_count () const noexcept
 get maximm number of blocks that can be allocated More...
 
constexpr auto used_block_count () noexcept
 get current number of used blocks More...
 
constexpr auto free_block_count () noexcept
 get current number of free blocks More...
 
auto native_handle () noexcept -> native_pointer
 get the native zephyr mem slab handle. More...
 
auto native_handle () const noexcept -> native_const_pointer
 get the native zephyr mem slab handle. More...
 
 mem_slab_base (const mem_slab_base &)=delete
 
 mem_slab_base (mem_slab_base &&)=delete
 
mem_slab_baseoperator= (const mem_slab_base &)=delete
 
mem_slab_baseoperator= (mem_slab_base &&)=delete
 

Protected Member Functions

constexpr mem_slab_base () noexcept
 

Detailed Description

template<class T_MemSlab>
class zpp::mem_slab_base< T_MemSlab >

Allocator that uses k_mem_slab for memory.

Parameters
T_MemSlabthe CRTP type

Definition at line 27 of file mem_slab.hpp.

Member Typedef Documentation

◆ native_const_pointer

template<class T_MemSlab >
using zpp::mem_slab_base< T_MemSlab >::native_const_pointer = native_type const *

Definition at line 31 of file mem_slab.hpp.

◆ native_pointer

template<class T_MemSlab >
using zpp::mem_slab_base< T_MemSlab >::native_pointer = native_type*

Definition at line 30 of file mem_slab.hpp.

◆ native_type

template<class T_MemSlab >
using zpp::mem_slab_base< T_MemSlab >::native_type = struct k_mem_slab

Definition at line 29 of file mem_slab.hpp.

Constructor & Destructor Documentation

◆ mem_slab_base() [1/3]

template<class T_MemSlab >
constexpr zpp::mem_slab_base< T_MemSlab >::mem_slab_base ( )
inlineconstexprprotectednoexcept

Definition at line 33 of file mem_slab.hpp.

33 {}

◆ mem_slab_base() [2/3]

template<class T_MemSlab >
zpp::mem_slab_base< T_MemSlab >::mem_slab_base ( const mem_slab_base< T_MemSlab > &  )
delete

◆ mem_slab_base() [3/3]

template<class T_MemSlab >
zpp::mem_slab_base< T_MemSlab >::mem_slab_base ( mem_slab_base< T_MemSlab > &&  )
delete

Member Function Documentation

◆ allocate()

template<class T_MemSlab >
void* zpp::mem_slab_base< T_MemSlab >::allocate ( )
inlinenoexcept

allocate a memory block, waiting forever

Returns
pointer to memory or nullptr on error

Definition at line 41 of file mem_slab.hpp.

42  {
43  void* vp{nullptr};
44 
45  auto rc = k_mem_slab_alloc(native_handle(), &vp, K_FOREVER);
46 
47  if (rc == 0) {
48  return vp;
49  } else {
50  return nullptr;
51  }
52  }
auto native_handle() noexcept -> native_pointer
get the native zephyr mem slab handle.
Definition: mem_slab.hpp:155

References zpp::mem_slab_base< T_MemSlab >::native_handle().

◆ block_size()

template<class T_MemSlab >
constexpr auto zpp::mem_slab_base< T_MemSlab >::block_size ( ) const
inlineconstexprnoexcept

the size of the memory blocks

Returns
the size of the memory blocks in bytes

Definition at line 114 of file mem_slab.hpp.

115  {
116  return native_handle()->block_size;
117  }

References zpp::mem_slab_base< T_MemSlab >::native_handle().

◆ deallocate()

template<class T_MemSlab >
void zpp::mem_slab_base< T_MemSlab >::deallocate ( void *  vp)
inlinenoexcept

deallocate memory

Parameters
vpthe pointer to free

Definition at line 102 of file mem_slab.hpp.

103  {
104  if (vp != nullptr) {
105  k_mem_slab_free(native_handle(), &vp);
106  }
107  }

References zpp::mem_slab_base< T_MemSlab >::native_handle().

◆ free_block_count()

template<class T_MemSlab >
constexpr auto zpp::mem_slab_base< T_MemSlab >::free_block_count ( )
inlineconstexprnoexcept

get current number of free blocks

Returns
the current number of free blocks

Definition at line 144 of file mem_slab.hpp.

145  {
146  return k_mem_slab_num_free_get(native_handle());
147  }

References zpp::mem_slab_base< T_MemSlab >::native_handle().

◆ native_handle() [1/2]

template<class T_MemSlab >
auto zpp::mem_slab_base< T_MemSlab >::native_handle ( ) const -> native_const_pointer
inlinenoexcept

get the native zephyr mem slab handle.

Returns
A pointer to the zephyr k_mem_slab.

Definition at line 165 of file mem_slab.hpp.

166  {
167  return static_cast<const T_MemSlab*>(this)->native_handle();
168  }

References zpp::mem_slab_base< T_MemSlab >::native_handle().

◆ native_handle() [2/2]

◆ operator=() [1/2]

template<class T_MemSlab >
mem_slab_base& zpp::mem_slab_base< T_MemSlab >::operator= ( const mem_slab_base< T_MemSlab > &  )
delete

◆ operator=() [2/2]

template<class T_MemSlab >
mem_slab_base& zpp::mem_slab_base< T_MemSlab >::operator= ( mem_slab_base< T_MemSlab > &&  )
delete

◆ total_block_count()

template<class T_MemSlab >
constexpr auto zpp::mem_slab_base< T_MemSlab >::total_block_count ( ) const
inlineconstexprnoexcept

get maximm number of blocks that can be allocated

Returns
the maximum number of blocks that can be allocated

Definition at line 124 of file mem_slab.hpp.

125  {
126  return native_handle()->num_blocks;
127  }

References zpp::mem_slab_base< T_MemSlab >::native_handle().

◆ try_allocate()

template<class T_MemSlab >
void* zpp::mem_slab_base< T_MemSlab >::try_allocate ( )
inlinenoexcept

try allocate a memory block, not waiting

Returns
pointer to memory or nullptr on error

Definition at line 60 of file mem_slab.hpp.

61  {
62  void* vp{nullptr};
63 
64  auto rc = k_mem_slab_alloc(native_handle(), &vp, K_NO_WAIT);
65 
66  if (rc == 0) {
67  return vp;
68  } else {
69  return nullptr;
70  }
71  }

References zpp::mem_slab_base< T_MemSlab >::native_handle().

◆ try_allocate_for()

template<class T_MemSlab >
template<class T_Rep , class T_Period >
void* zpp::mem_slab_base< T_MemSlab >::try_allocate_for ( const std::chrono::duration< T_Rep, T_Period > &  timeout)
inlinenoexcept

try allocate a memory block waiting with a timeout

Parameters
timeoutthe time to try
Returns
pointer to memory or nullptr on error

Definition at line 82 of file mem_slab.hpp.

83  {
84  using namespace std::chrono;
85 
86  void* vp{nullptr};
87 
88  auto rc = k_mem_slab_alloc(native_handle(), &vp, to_timeout(timeout));
89 
90  if (rc == 0) {
91  return vp;
92  } else {
93  return nullptr;
94  }
95  }
constexpr k_timeout_t to_timeout(const std::chrono::duration< T_Rep, T_Period > &d) noexcept
convert a duration to tick
Definition: clock.hpp:88

References zpp::mem_slab_base< T_MemSlab >::native_handle(), and zpp::to_timeout().

◆ used_block_count()

template<class T_MemSlab >
constexpr auto zpp::mem_slab_base< T_MemSlab >::used_block_count ( )
inlineconstexprnoexcept

get current number of used blocks

Returns
the current number of used blocks

Definition at line 134 of file mem_slab.hpp.

135  {
136  return k_mem_slab_num_used_get(native_handle());
137  }

References zpp::mem_slab_base< T_MemSlab >::native_handle().


The documentation for this class was generated from the following file: