Function sample_per_tree_allocator


#include <samples/quickstart.cpp>

void sample_per_tree_allocator()


set per-tree allocators

Mentioned in


Lines 3659-3683 in samples/quickstart.cpp. Line 75 in samples/quickstart.cpp.

void sample_per_tree_allocator()
    PerTreeMemoryExample mrp;
    PerTreeMemoryExample mr1;
    PerTreeMemoryExample mr2;

    // the trees will use the memory in the resources above,
    // with each tree using a separate resource
        // Watchout: ensure that the lifetime of the memory resource
        // exceeds the lifetime of the tree.
        ryml::Parser parser = {ryml::Allocator(&mrp)};
        ryml::Tree   tree1  = {ryml::Allocator(&mr1)};
        ryml::Tree   tree2  = {ryml::Allocator(&mr2)};

        ryml::csubstr yml1 = "{a: b}";
        ryml::csubstr yml2 = "{c: d, e: f, g: [h, i, 0, 1, 2, 3]}";

        parser.parse("file1.yml", yml1, &tree1);
        parser.parse("file2.yml", yml2, &tree2);

    CHECK(mrp.num_allocs == 0); // YAML depth not large enough to warrant a parser allocation
    CHECK(mr1.alloc_size <= mr2.alloc_size); // because yml2 has more nodes

Add Discussion as Guest

Log in