rapidyaml  0.7.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 4863 of file quickstart.cpp.

Constructor & Destructor Documentation

◆ ~PerTreeMemoryExample()

sample::PerTreeMemoryExample::~PerTreeMemoryExample ( )
inline

Definition at line 4905 of file quickstart.cpp.

4906  {
4907  check_and_reset();
4908  }

References check_and_reset().

Member Function Documentation

◆ callbacks()

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

Definition at line 4869 of file quickstart.cpp.

4870  {
4871  // Above we used static functions to bridge to our methods.
4872  // To show a different approach, we employ lambdas here.
4873  // Note that there can be no captures in the lambdas
4874  // because these are C-style function pointers.
4875  ryml::Callbacks cb;
4876  cb.m_user_data = (void*) this;
4877  cb.m_allocate = [](size_t len, void *, void *data){ return ((PerTreeMemoryExample*) data)->allocate(len); };
4878  cb.m_free = [](void *mem, size_t len, void *data){ return ((PerTreeMemoryExample*) data)->free(mem, len); };
4879  return cb;
4880  }
a c-style callbacks class.
Definition: common.hpp:375
void * m_user_data
Definition: common.hpp:376
pfn_allocate m_allocate
Definition: common.hpp:377
pfn_free m_free
Definition: common.hpp:378
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 4882 of file quickstart.cpp.

4883  {
4884  void *ptr = &memory_pool[alloc_size];
4885  alloc_size += len;
4886  ++num_allocs;
4887  if(C4_UNLIKELY(alloc_size > memory_pool.size()))
4888  {
4889  std::cerr << "out of memory! requested=" << alloc_size << " vs " << memory_pool.size() << " available" << std::endl;
4890  std::abort();
4891  }
4892  return ptr;
4893  }
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 4895 of file quickstart.cpp.

4896  {
4897  CHECK((char*)mem >= &memory_pool.front() && (char*)mem < &memory_pool.back());
4898  CHECK((char*)mem+len >= &memory_pool.front() && (char*)mem+len <= &memory_pool.back());
4899  dealloc_size += len;
4900  ++num_deallocs;
4901  // no need to free here
4902  }
#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 4909 of file quickstart.cpp.

4910  {
4911  std::cout << "size: alloc=" << alloc_size << " dealloc=" << dealloc_size << std::endl;
4912  std::cout << "count: #allocs=" << num_allocs << " #deallocs=" << num_deallocs << std::endl;
4914  CHECK(alloc_size >= dealloc_size); // failure here means a double free
4915  CHECK(alloc_size == dealloc_size); // failure here means a leak
4916  num_allocs = 0;
4917  num_deallocs = 0;
4918  alloc_size = 0;
4919  dealloc_size = 0;
4920  }

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 4865 of file quickstart.cpp.

Referenced by allocate(), and free().

◆ num_allocs

size_t sample::PerTreeMemoryExample::num_allocs = 0

Definition at line 4866 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 4866 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 4867 of file quickstart.cpp.

Referenced by check_and_reset(), and free().

◆ dealloc_size

size_t sample::PerTreeMemoryExample::dealloc_size = 0

Definition at line 4867 of file quickstart.cpp.

Referenced by check_and_reset(), and free().


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