rapidyaml  0.16.0
parseandemitYAML,anddoitfast
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 6981 of file quickstart.cpp.

Constructor & Destructor Documentation

PerTreeMemoryExample::~PerTreeMemoryExample ( )
inline

Definition at line 7023 of file quickstart.cpp.

7024  {
7025  check_and_reset();
7026  }

Member Function Documentation

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

Definition at line 6987 of file quickstart.cpp.

Referenced by sample_per_tree_allocator().

6988  {
6989  // Above we used static functions to bridge to our methods.
6990  // To show a different approach, we employ lambdas here.
6991  // Note that there can be no captures in the lambdas
6992  // because these are C-style function pointers.
6993  ryml::Callbacks cb;
6994  cb.m_user_data = (void*) this;
6995  cb.m_allocate = [](size_t len, void *, void *data){ return ((PerTreeMemoryExample*) data)->allocate(len); };
6996  cb.m_free = [](void *mem, size_t len, void *data){ return ((PerTreeMemoryExample*) data)->free(mem, len); };
6997  return cb;
6998  }
void * allocate(size_t len)
an example for a per-tree memory allocator
A c-style callbacks class to customize behavior on errors or allocation.
Definition: common.hpp:373
void * m_user_data
data to be forwarded in every call to a callback
Definition: common.hpp:375
pfn_free m_free
a pointer to a free handler function
Definition: common.hpp:377
pfn_allocate m_allocate
a pointer to an allocate handler function
Definition: common.hpp:376
void free(void *mem, size_t len)
void* PerTreeMemoryExample::allocate ( size_t  len)
inline

Definition at line 7000 of file quickstart.cpp.

Referenced by callbacks().

7001  {
7002  void *ptr = &memory_pool[alloc_size];
7003  alloc_size += len;
7004  ++num_allocs;
7005  if C4_UNLIKELY(alloc_size > memory_pool.size())
7006  {
7007  std::cerr << "out of memory! requested=" << alloc_size << " vs " << memory_pool.size() << " available" << std::endl; // LCOV_EXCL_LINE
7008  std::abort(); // LCOV_EXCL_LINE
7009  }
7010  return ptr;
7011  }
std::vector< char > memory_pool
void PerTreeMemoryExample::free ( void *  mem,
size_t  len 
)
inline

Definition at line 7013 of file quickstart.cpp.

Referenced by callbacks().

7014  {
7015  CHECK((char*)mem >= &memory_pool.front() && (char*)mem < &memory_pool.back());
7016  CHECK((char*)mem+len >= &memory_pool.front() && (char*)mem+len <= &memory_pool.back());
7017  dealloc_size += len;
7018  ++num_deallocs;
7019  // no need to free here
7020  }
std::vector< char > memory_pool
#define CHECK(predicate)
a testing assertion, used only in this quickstart
Definition: quickstart.cpp:310
void PerTreeMemoryExample::check_and_reset ( )
inline

Definition at line 7027 of file quickstart.cpp.

Referenced by ~PerTreeMemoryExample().

7028  {
7029  std::cout << "size: alloc=" << alloc_size << " dealloc=" << dealloc_size << std::endl;
7030  std::cout << "count: #allocs=" << num_allocs << " #deallocs=" << num_deallocs << std::endl;
7032  CHECK(alloc_size >= dealloc_size); // failure here means a double free
7033  CHECK(alloc_size == dealloc_size); // failure here means a leak
7034  num_allocs = 0;
7035  num_deallocs = 0;
7036  alloc_size = 0;
7037  dealloc_size = 0;
7038  }
#define CHECK(predicate)
a testing assertion, used only in this quickstart
Definition: quickstart.cpp:310

Member Data Documentation

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

Definition at line 6983 of file quickstart.cpp.

size_t PerTreeMemoryExample::num_allocs = 0

Definition at line 6984 of file quickstart.cpp.

Referenced by allocate(), and sample_per_tree_allocator().

size_t PerTreeMemoryExample::alloc_size = 0

Definition at line 6984 of file quickstart.cpp.

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

size_t PerTreeMemoryExample::num_deallocs = 0

Definition at line 6985 of file quickstart.cpp.

Referenced by free().

size_t PerTreeMemoryExample::dealloc_size = 0

Definition at line 6985 of file quickstart.cpp.

Referenced by check_and_reset(), and free().


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