rapidyaml 0.15.2
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 6835 of file quickstart.cpp.

6836{
6838
6839 // save the existing callbacks for restoring
6841
6842 // set to our callbacks
6844
6845 // verify that the allocator is in effect
6846 ryml::Callbacks const& current = ryml::get_callbacks();
6847 CHECK(current.m_allocate == &mem.s_allocate);
6848 CHECK(current.m_free == &mem.s_free);
6849
6850 // so far nothing was allocated
6851 CHECK(mem.alloc_size == 0);
6852
6853 // parse one tree and check
6854 (void)ryml::parse_in_arena(R"({foo: bar})");
6855 mem.check_and_reset();
6856
6857 // parse another tree and check
6858 (void)ryml::parse_in_arena(R"([a, b, c, d, {foo: bar, money: pennys}])");
6859 mem.check_and_reset();
6860
6861 // verify that by reserving we save allocations
6862 {
6863 ryml::EventHandlerTree evt_handler;
6864 ryml::Parser parser(&evt_handler); // reuse a parser
6865 ryml::Tree tree; // reuse a tree
6866
6867 tree.reserve(10); // reserve the number of nodes
6868 tree.reserve_arena(100); // reserve the arena size
6869 parser.reserve_stack(10); // reserve the parser depth.
6870
6871 // since the parser stack uses Small Storage Optimization,
6872 // allocations will only happen with capacities higher than 16.
6873 CHECK(mem.num_allocs == 2); // tree, tree_arena and NOT the parser
6874
6875 parser.reserve_stack(20); // reserve the parser depth.
6876 CHECK(mem.num_allocs == 3); // tree, tree_arena and now the parser as well
6877
6878 // verify that no other allocations occur when parsing
6879 size_t size_before = mem.alloc_size;
6880 parse_in_arena(&parser, "", R"([a, b, c, d, {foo: bar, money: pennys}])", &tree);
6881 CHECK(mem.alloc_size == size_before);
6882 CHECK(mem.num_allocs == 3);
6883 }
6884 mem.check_and_reset();
6885
6886 // restore defaults.
6887 ryml::set_callbacks(defaults);
6888}
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 6958 of file quickstart.cpp.

6959{
6963
6964 // the trees will use the memory in the resources above,
6965 // with each tree using a separate resource
6966 {
6967 // Watchout: ensure that the lifetime of the callbacks target
6968 // exceeds the lifetime of the tree.
6969 ryml::EventHandlerTree evt_handler(mrp.callbacks());
6970 ryml::Parser parser(&evt_handler);
6971 ryml::Tree tree1(mr1.callbacks());
6972 ryml::Tree tree2(mr2.callbacks());
6973
6974 ryml::csubstr yml1 = "{a: b}";
6975 ryml::csubstr yml2 = "{c: d, e: f, g: [h, i, 0, 1, 2, 3]}";
6976
6977 parse_in_arena(&parser, "file1.yml", yml1, &tree1);
6978 parse_in_arena(&parser, "file2.yml", yml2, &tree2);
6979 }
6980
6981 CHECK(mrp.num_allocs == 0); // YAML depth not large enough to warrant a parser allocation
6982 CHECK(mr1.alloc_size <= mr2.alloc_size); // because yml2 has more nodes
6983}
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().