rapidyaml  0.9.0
parse and emit YAML, and do it fast
sample::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 4891 of file quickstart.cpp.

Constructor & Destructor Documentation

◆ ~PerTreeMemoryExample()

sample::PerTreeMemoryExample::~PerTreeMemoryExample ( )
inline

Definition at line 4933 of file quickstart.cpp.

4934  {
4935  check_and_reset();
4936  }

References check_and_reset().

Member Function Documentation

◆ callbacks()

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

Definition at line 4897 of file quickstart.cpp.

4898  {
4899  // Above we used static functions to bridge to our methods.
4900  // To show a different approach, we employ lambdas here.
4901  // Note that there can be no captures in the lambdas
4902  // because these are C-style function pointers.
4903  ryml::Callbacks cb;
4904  cb.m_user_data = (void*) this;
4905  cb.m_allocate = [](size_t len, void *, void *data){ return ((PerTreeMemoryExample*) data)->allocate(len); };
4906  cb.m_free = [](void *mem, size_t len, void *data){ return ((PerTreeMemoryExample*) data)->free(mem, len); };
4907  return cb;
4908  }
a c-style callbacks class.
Definition: common.hpp:376
void * m_user_data
Definition: common.hpp:377
pfn_allocate m_allocate
Definition: common.hpp:378
pfn_free m_free
Definition: common.hpp:379
void free(void *mem, size_t len)
void * allocate(size_t len)

References allocate(), free(), c4::yml::Callbacks::m_allocate, c4::yml::Callbacks::m_free, and c4::yml::Callbacks::m_user_data.

Referenced by sample::sample_per_tree_allocator().

◆ allocate()

void* sample::PerTreeMemoryExample::allocate ( size_t  len)
inline

Definition at line 4910 of file quickstart.cpp.

4911  {
4912  void *ptr = &memory_pool[alloc_size];
4913  alloc_size += len;
4914  ++num_allocs;
4915  if(C4_UNLIKELY(alloc_size > memory_pool.size()))
4916  {
4917  std::cerr << "out of memory! requested=" << alloc_size << " vs " << memory_pool.size() << " available" << std::endl;
4918  std::abort();
4919  }
4920  return ptr;
4921  }
std::vector< char > memory_pool

References alloc_size, memory_pool, and num_allocs.

Referenced by callbacks().

◆ free()

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

Definition at line 4923 of file quickstart.cpp.

4924  {
4925  CHECK((char*)mem >= &memory_pool.front() && (char*)mem < &memory_pool.back());
4926  CHECK((char*)mem+len >= &memory_pool.front() && (char*)mem+len <= &memory_pool.back());
4927  dealloc_size += len;
4928  ++num_deallocs;
4929  // no need to free here
4930  }
#define CHECK(predicate)
Definition: quickstart.cpp:229

References CHECK, dealloc_size, memory_pool, and num_deallocs.

Referenced by callbacks().

◆ check_and_reset()

void sample::PerTreeMemoryExample::check_and_reset ( )
inline

Definition at line 4937 of file quickstart.cpp.

4938  {
4939  std::cout << "size: alloc=" << alloc_size << " dealloc=" << dealloc_size << std::endl;
4940  std::cout << "count: #allocs=" << num_allocs << " #deallocs=" << num_deallocs << std::endl;
4942  CHECK(alloc_size >= dealloc_size); // failure here means a double free
4943  CHECK(alloc_size == dealloc_size); // failure here means a leak
4944  num_allocs = 0;
4945  num_deallocs = 0;
4946  alloc_size = 0;
4947  dealloc_size = 0;
4948  }

References alloc_size, CHECK, dealloc_size, num_allocs, and num_deallocs.

Referenced by ~PerTreeMemoryExample().

Member Data Documentation

◆ memory_pool

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

Definition at line 4893 of file quickstart.cpp.

Referenced by allocate(), and free().

◆ num_allocs

size_t sample::PerTreeMemoryExample::num_allocs = 0

Definition at line 4894 of file quickstart.cpp.

Referenced by allocate(), check_and_reset(), and sample::sample_per_tree_allocator().

◆ alloc_size

size_t sample::PerTreeMemoryExample::alloc_size = 0

Definition at line 4894 of file quickstart.cpp.

Referenced by allocate(), check_and_reset(), and sample::sample_per_tree_allocator().

◆ num_deallocs

size_t sample::PerTreeMemoryExample::num_deallocs = 0

Definition at line 4895 of file quickstart.cpp.

Referenced by check_and_reset(), and free().

◆ dealloc_size

size_t sample::PerTreeMemoryExample::dealloc_size = 0

Definition at line 4895 of file quickstart.cpp.

Referenced by check_and_reset(), and free().


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