rapidyaml 0.16.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
Allocators

Functions

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

Detailed Description

Function Documentation

◆ sample_global_allocator()

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.

6920{
6922
6923 // save the existing callbacks for restoring
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}
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
void reserve(id_type node_capacity=RYML_DEFAULT_TREE_CAPACITY)
Definition tree.cpp:292
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
Callbacks const & get_callbacks()
get the global callbacks
Definition common.cpp:94
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
ParseEngine< EventHandlerTree > Parser
This is the main ryml parser, where the parser events are handled to create a ryml tree (see Event Ha...
Definition fwd.hpp:19
#define CHECK(predicate)
a testing assertion, used only in this quickstart
static void s_free(void *mem, size_t len, void *this_)
ryml::Callbacks callbacks()
static void * s_allocate(size_t len, void *, void *this_)
A c-style callbacks class to customize behavior on errors or allocation.
Definition common.hpp:374
pfn_allocate m_allocate
a pointer to an allocate handler function
Definition common.hpp:376
pfn_free m_free
a pointer to a free handler function
Definition common.hpp:377
The event handler to create a ryml Tree.

Referenced by main().

◆ sample_per_tree_allocator()

void sample_per_tree_allocator ( )

set per-tree allocators

Definition at line 7042 of file quickstart.cpp.

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}
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2356
an example for a per-tree memory allocator
ryml::Callbacks callbacks() const

Referenced by main().