rapidyaml  0.16.0
parseandemitYAML,anddoitfast
Allocators

Functions

void sample_global_allocator ()
 set a global allocator for ryml More...
 
void sample_per_tree_allocator ()
 set per-tree allocators More...
 

Detailed Description

Function Documentation

void sample_global_allocator ( )

set a global allocator for ryml

demonstrates how to set the global allocator for ryml

Definition at line 6919 of file quickstart.cpp.

Referenced by main().

6920 {
6922 
6923  // save the existing callbacks for restoring
6924  ryml::Callbacks defaults = ryml::get_callbacks();
6925 
6926  // set to our callbacks
6928 
6929  // verify that the allocator is in effect
6930  ryml::Callbacks const& current = ryml::get_callbacks();
6931  CHECK(current.m_allocate == &mem.s_allocate);
6932  CHECK(current.m_free == &mem.s_free);
6933 
6934  // so far nothing was allocated
6935  CHECK(mem.alloc_size == 0);
6936 
6937  // parse one tree and check
6938  (void)ryml::parse_in_arena(R"({foo: bar})");
6939  mem.check_and_reset();
6940 
6941  // parse another tree and check
6942  (void)ryml::parse_in_arena(R"([a, b, c, d, {foo: bar, money: pennys}])");
6943  mem.check_and_reset();
6944 
6945  // verify that by reserving we save allocations
6946  {
6947  ryml::EventHandlerTree evt_handler;
6948  ryml::Parser parser(&evt_handler); // reuse a parser
6949  ryml::Tree tree; // reuse a tree
6950 
6951  tree.reserve(10); // reserve the number of nodes
6952  tree.reserve_arena(100); // reserve the arena size
6953  parser.reserve_stack(10); // reserve the parser depth.
6954 
6955  // since the parser stack uses Small Storage Optimization,
6956  // allocations will only happen with capacities higher than 16.
6957  CHECK(mem.num_allocs == 2); // tree, tree_arena and NOT the parser
6958 
6959  parser.reserve_stack(20); // reserve the parser depth.
6960  CHECK(mem.num_allocs == 3); // tree, tree_arena and now the parser as well
6961 
6962  // verify that no other allocations occur when parsing
6963  size_t size_before = mem.alloc_size;
6964  parse_in_arena(&parser, "", R"([a, b, c, d, {foo: bar, money: pennys}])", &tree);
6965  CHECK(mem.alloc_size == size_before);
6966  CHECK(mem.num_allocs == 3);
6967  }
6968  mem.check_and_reset();
6969 
6970  // restore defaults.
6971  ryml::set_callbacks(defaults);
6972 }
The event handler to create a ryml Tree.
void set_callbacks(Callbacks const &c)
set the global callbacks for the library; after a call to this function, these callbacks will be used...
Definition: common.cpp:89
A c-style callbacks class to customize behavior on errors or allocation.
Definition: common.hpp:373
void parse_in_arena(Parser *parser, csubstr filename, csubstr yaml, Tree *tree, id_type node_id)
(1) parse YAML into an existing tree node. The filename will be used in any error messages arising du...
Definition: parse.cpp:209
void reserve_arena(size_t arena_cap=RYML_DEFAULT_TREE_ARENA_CAPACITY)
ensure the tree's internal string arena is at least the given capacity
Definition: tree.hpp:1409
Parser parser
Definition: parse.cpp:117
#define CHECK(predicate)
a testing assertion, used only in this quickstart
Definition: quickstart.cpp:310
This is the main driver of parsing logic: it scans the YAML or JSON source for tokens, and emits the appropriate sequence of parsing events to its event handler.
Definition: fwd.hpp:17
Callbacks const & get_callbacks()
get the global callbacks
Definition: common.cpp:94
ryml::Callbacks callbacks()
static void * s_allocate(size_t len, void *, void *this_)
void reserve(id_type node_capacity=RYML_DEFAULT_TREE_CAPACITY)
Definition: tree.cpp:292
static void s_free(void *mem, size_t len, void *this_)
void sample_per_tree_allocator ( )

set per-tree allocators

Definition at line 7042 of file quickstart.cpp.

Referenced by main().

7043 {
7047 
7048  // the trees will use the memory in the resources above,
7049  // with each tree using a separate resource
7050  {
7051  // Watchout: ensure that the lifetime of the callbacks target
7052  // exceeds the lifetime of the tree.
7053  ryml::EventHandlerTree evt_handler(mrp.callbacks());
7054  ryml::Parser parser(&evt_handler);
7055  ryml::Tree tree1(mr1.callbacks());
7056  ryml::Tree tree2(mr2.callbacks());
7057 
7058  ryml::csubstr yml1 = "{a: b}";
7059  ryml::csubstr yml2 = "{c: d, e: f, g: [h, i, 0, 1, 2, 3]}";
7060 
7061  parse_in_arena(&parser, "file1.yml", yml1, &tree1);
7062  parse_in_arena(&parser, "file2.yml", yml2, &tree2);
7063  }
7064 
7065  CHECK(mrp.num_allocs == 0); // YAML depth not large enough to warrant a parser allocation
7066  CHECK(mr1.alloc_size <= mr2.alloc_size); // because yml2 has more nodes
7067 }
The event handler to create a ryml Tree.
an example for a per-tree memory allocator
void parse_in_arena(Parser *parser, csubstr filename, csubstr yaml, Tree *tree, id_type node_id)
(1) parse YAML into an existing tree node. The filename will be used in any error messages arising du...
Definition: parse.cpp:209
Parser parser
Definition: parse.cpp:117
ryml::Callbacks callbacks() const
#define CHECK(predicate)
a testing assertion, used only in this quickstart
Definition: quickstart.cpp:310
This is the main driver of parsing logic: it scans the YAML or JSON source for tokens, and emits the appropriate sequence of parsing events to its event handler.
Definition: fwd.hpp:17