rapidyaml  0.13.0
parse and emit YAML, and do it fast
PerTreeMemoryExample Struct Reference

an example for a per-tree memory allocator More...

Public Member Functions

ryml::Callbacks callbacks () const
 
void * allocate (size_t len)
 
void free (void *mem, size_t len)
 
 ~PerTreeMemoryExample ()
 
void check_and_reset ()
 

Public Attributes

std::vector< char > memory_pool = std::vector<char>(10u * 1024u)
 
size_t num_allocs = 0
 
size_t alloc_size = 0
 
size_t num_deallocs = 0
 
size_t dealloc_size = 0
 

Detailed Description

an example for a per-tree memory allocator

Definition at line 6018 of file quickstart.cpp.

Constructor & Destructor Documentation

◆ ~PerTreeMemoryExample()

PerTreeMemoryExample::~PerTreeMemoryExample ( )
inline

Definition at line 6060 of file quickstart.cpp.

6061  {
6062  check_and_reset();
6063  }

Member Function Documentation

◆ callbacks()

ryml::Callbacks PerTreeMemoryExample::callbacks ( ) const
inline

Definition at line 6024 of file quickstart.cpp.

6025  {
6026  // Above we used static functions to bridge to our methods.
6027  // To show a different approach, we employ lambdas here.
6028  // Note that there can be no captures in the lambdas
6029  // because these are C-style function pointers.
6030  ryml::Callbacks cb;
6031  cb.m_user_data = (void*) this;
6032  cb.m_allocate = [](size_t len, void *, void *data){ return ((PerTreeMemoryExample*) data)->allocate(len); };
6033  cb.m_free = [](void *mem, size_t len, void *data){ return ((PerTreeMemoryExample*) data)->free(mem, len); };
6034  return cb;
6035  }
an example for a per-tree memory allocator
void free(void *mem, size_t len)
void * allocate(size_t len)
A c-style callbacks class to customize behavior on errors or allocation.
Definition: common.hpp:541
void * m_user_data
data to be forwarded in every call to a callback
Definition: common.hpp:542
pfn_allocate m_allocate
a pointer to an allocate handler function
Definition: common.hpp:543
pfn_free m_free
a pointer to a free handler function
Definition: common.hpp:544

◆ allocate()

void* PerTreeMemoryExample::allocate ( size_t  len)
inline

Definition at line 6037 of file quickstart.cpp.

6038  {
6039  void *ptr = &memory_pool[alloc_size];
6040  alloc_size += len;
6041  ++num_allocs;
6042  if(C4_UNLIKELY(alloc_size > memory_pool.size()))
6043  {
6044  std::cerr << "out of memory! requested=" << alloc_size << " vs " << memory_pool.size() << " available" << std::endl;
6045  std::abort();
6046  }
6047  return ptr;
6048  }
std::vector< char > memory_pool

◆ free()

void PerTreeMemoryExample::free ( void *  mem,
size_t  len 
)
inline

Definition at line 6050 of file quickstart.cpp.

6051  {
6052  CHECK((char*)mem >= &memory_pool.front() && (char*)mem < &memory_pool.back());
6053  CHECK((char*)mem+len >= &memory_pool.front() && (char*)mem+len <= &memory_pool.back());
6054  dealloc_size += len;
6055  ++num_deallocs;
6056  // no need to free here
6057  }
#define CHECK(predicate)
a quick'n'dirty assertion to verify a predicate
Definition: quickstart.cpp:309

◆ check_and_reset()

void PerTreeMemoryExample::check_and_reset ( )
inline

Definition at line 6064 of file quickstart.cpp.

6065  {
6066  std::cout << "size: alloc=" << alloc_size << " dealloc=" << dealloc_size << std::endl;
6067  std::cout << "count: #allocs=" << num_allocs << " #deallocs=" << num_deallocs << std::endl;
6069  CHECK(alloc_size >= dealloc_size); // failure here means a double free
6070  CHECK(alloc_size == dealloc_size); // failure here means a leak
6071  num_allocs = 0;
6072  num_deallocs = 0;
6073  alloc_size = 0;
6074  dealloc_size = 0;
6075  }

Member Data Documentation

◆ memory_pool

std::vector<char> PerTreeMemoryExample::memory_pool = std::vector<char>(10u * 1024u)

Definition at line 6020 of file quickstart.cpp.

◆ num_allocs

size_t PerTreeMemoryExample::num_allocs = 0

Definition at line 6021 of file quickstart.cpp.

◆ alloc_size

size_t PerTreeMemoryExample::alloc_size = 0

Definition at line 6021 of file quickstart.cpp.

◆ num_deallocs

size_t PerTreeMemoryExample::num_deallocs = 0

Definition at line 6022 of file quickstart.cpp.

◆ dealloc_size

size_t PerTreeMemoryExample::dealloc_size = 0

Definition at line 6022 of file quickstart.cpp.


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