From f9a38756ca0f4ec6369fb6aa9d83503ec1f95781 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Wed, 8 Nov 2023 01:31:52 -0600 Subject: [PATCH] Ripped out inlined (in-RAM) btree union This did not turn out to be useful, mainly because type-agnostic inlining requires unnecessary encoding/decoding and risks a higher RAM allocation than is really needed. It's better to just reserve a bit in the weight field and allow higher-level operations to use operation-specific unions. code stack before: 31580 2072 after: 31160 (-1.3%) 2072 (+0.0%) --- lfs.c | 306 ++++-------------- lfs.h | 24 +- tests/test_btree.toml | 712 ++++++++++++++++++++++-------------------- 3 files changed, 446 insertions(+), 596 deletions(-) diff --git a/lfs.c b/lfs.c index 2732cb2c..8d4c250b 100644 --- a/lfs.c +++ b/lfs.c @@ -2038,6 +2038,16 @@ static inline bool lfsr_rbyd_isfetched(const lfsr_rbyd_t *rbyd) { return !(rbyd->eoff == 0 && rbyd->trunk > 0); } +static inline int lfsr_rbyd_cmp( + const lfsr_rbyd_t *a, + const lfsr_rbyd_t *b) { + if (a->block != b->block) { + return a->block - b->block; + } else { + return a->trunk - b->trunk; + } +} + static inline void lfsr_rbyd_unerase(lfsr_rbyd_t *rbyd) { rbyd->eoff = -1; } @@ -3657,51 +3667,14 @@ static int lfsr_rbyd_namelookup(lfs_t *lfs, const lfsr_rbyd_t *rbyd, // convenience operations -// use sign bit to indicate if btree is inlined -#define LFSR_BTREE_INLINED 0x80000000 - -// TODO need null btrees? -#define LFSR_BTREE_NULL ((lfsr_btree_t){.u.weight=0x80000000}) - -static inline bool lfsr_btree_isinlined(const lfsr_btree_t *btree) { - return btree->u.weight & LFSR_BTREE_INLINED; -} - -static inline lfsr_bid_t lfsr_btree_weight(const lfsr_btree_t *btree) { - return btree->u.weight & ~LFSR_BTREE_INLINED; -} - -static inline bool lfsr_btree_isnull(const lfsr_btree_t *btree) { - return (lfsr_bid_t)btree->u.weight == (LFSR_BTREE_INLINED | 0); -} - static inline int lfsr_btree_cmp( const lfsr_btree_t *a, const lfsr_btree_t *b) { - if (a->u.weight != b->u.weight) { - return a->u.weight - b->u.weight; - } else if (lfsr_btree_isinlined(a)) { - if (a->u.inlined.tag != b->u.inlined.tag) { - return a->u.inlined.tag - b->u.inlined.tag; - } else if (a->u.inlined.size != b->u.inlined.size) { - return a->u.inlined.size - b->u.inlined.size; - } else { - return memcmp(a->u.inlined.buf, b->u.inlined.buf, - a->u.inlined.size); - } - } else { - if (a->u.rbyd.block != b->u.rbyd.block) { - return a->u.rbyd.block - b->u.rbyd.block; - } else { - return a->u.rbyd.trunk - b->u.rbyd.trunk; - } - } + return lfsr_rbyd_cmp(a, b); } static inline void lfsr_btree_unerase(lfsr_btree_t *btree) { - if (!lfsr_btree_isinlined(btree)) { - lfsr_rbyd_unerase(&btree->u.rbyd); - } + lfsr_rbyd_unerase(btree); } @@ -3768,12 +3741,9 @@ static int lfsr_data_readbranch(lfs_t *lfs, lfsr_data_t *data, #define LFSR_DATA_FROMBTREE(_btree, _buffer) \ lfsr_data_frombtree(_btree, _buffer) -static lfsr_data_t lfsr_data_frombtree(const lfsr_rbyd_t *btree, +static lfsr_data_t lfsr_data_frombtree(const lfsr_btree_t *btree, uint8_t buffer[static LFSR_BTREE_DSIZE]) { - // upper layers must take care of encoding inlined btrees - LFS_ASSERT(!lfsr_btree_isinlined((const lfsr_btree_t*)btree)); lfs_ssize_t d = 0; - lfs_ssize_t d_ = lfs_toleb128(btree->weight, &buffer[d], 5); LFS_ASSERT(d_ >= 0); d += d_; @@ -3784,24 +3754,8 @@ static lfsr_data_t lfsr_data_frombtree(const lfsr_rbyd_t *btree, return LFSR_DATA_BUF(buffer, d); } -static int lfsr_data_readbtreeinlined(lfs_t *lfs, lfsr_data_t *data, - lfsr_tag_t tag, lfsr_bid_t weight, - lfsr_btree_t *btree) { - LFS_ASSERT(lfsr_data_size(data) <= LFSR_BTREE_INLINESIZE); - // mark as inlined - btree->u.inlined.weight = weight | LFSR_BTREE_INLINED; - btree->u.inlined.tag = tag; - lfs_ssize_t size = lfsr_data_read(lfs, data, - btree->u.inlined.buf, LFSR_BTREE_INLINESIZE); - if (size < 0) { - return size; - } - btree->u.inlined.size = size; - return 0; -} - static int lfsr_data_readbtree(lfs_t *lfs, lfsr_data_t *data, - lfsr_rbyd_t *btree) { + lfsr_btree_t *btree) { lfsr_bid_t weight; int err = lfsr_data_readleb128(lfs, data, (int32_t*)&weight); if (err) { @@ -3819,49 +3773,26 @@ static int lfsr_data_readbtree(lfs_t *lfs, lfsr_data_t *data, // B-tree operations +static int lfsr_btree_alloc(lfs_t *lfs, lfsr_btree_t *btree) { + return lfsr_rbyd_alloc(lfs, btree); +} + static int lfsr_btree_lookupnext_(lfs_t *lfs, const lfsr_btree_t *btree, lfsr_bid_t bid, lfsr_bid_t *bid_, lfsr_rbyd_t *rbyd_, lfsr_srid_t *rid_, lfsr_tag_t *tag_, lfsr_bid_t *weight_, lfsr_data_t *data_) { - // in range? - if (bid >= lfsr_btree_weight(btree)) { - return LFS_ERR_NOENT; - } - - // inlined? - if (lfsr_btree_isinlined(btree)) { - // TODO how many of these should be conditional? - if (bid_) { - *bid_ = lfsr_btree_weight(btree)-1; - } - if (tag_) { - *tag_ = btree->u.inlined.tag; - } - if (weight_) { - *weight_ = lfsr_btree_weight(btree); - } - if (data_) { - *data_ = LFSR_DATA_BUF(btree->u.inlined.buf, btree->u.inlined.size); - } - return 0; - } - // descend down the btree looking for our bid - lfsr_rbyd_t branch = btree->u.rbyd; + lfsr_rbyd_t branch = *btree; lfsr_srid_t rid = bid; while (true) { // each branch is a pair of optional name + on-disk structure lfsr_srid_t rid__; lfsr_tag_t tag__; - // TODO do we really need to fetch weight__ if we get it in our - // btree struct? - // TODO maybe only when validating? lfsr_rid_t weight__; lfsr_data_t data__; int err = lfsr_rbyd_lookupnext(lfs, &branch, rid, 0, &rid__, &tag__, &weight__, &data__); if (err) { - LFS_ASSERT(err != LFS_ERR_NOENT); return err; } @@ -3942,22 +3873,17 @@ static int lfsr_btree_lookup(lfs_t *lfs, const lfsr_btree_t *btree, static int lfsr_btree_parent(lfs_t *lfs, const lfsr_btree_t *btree, lfsr_bid_t bid, const lfsr_rbyd_t *child, lfsr_rbyd_t *rbyd_, lfsr_srid_t *rid_) { - // we only call this when we actually have parents - LFS_ASSERT(bid < lfsr_btree_weight(btree)); - LFS_ASSERT(!lfsr_btree_isinlined(btree)); - LFS_ASSERT(!(btree->u.rbyd.block == child->block - && btree->u.rbyd.trunk == child->trunk)); + // we should only call this when we actually have parents + LFS_ASSERT(bid < (lfsr_bid_t)btree->weight); + LFS_ASSERT(lfsr_rbyd_cmp(btree, child) != 0); // descend down the btree looking for our rid - lfsr_rbyd_t branch = btree->u.rbyd; + lfsr_rbyd_t branch = *btree; lfsr_srid_t rid = bid; while (true) { // each branch is a pair of optional name + on-disk structure lfsr_srid_t rid__; lfsr_tag_t tag__; - // TODO do we really need to fetch weight__ if we get it in our - // btree struct? - // TODO maybe only when validating? lfsr_rid_t weight__; lfsr_data_t data__; int err = lfsr_rbyd_lookupnext(lfs, &branch, rid, 0, @@ -4011,99 +3937,25 @@ static int lfsr_btree_parent(lfs_t *lfs, const lfsr_btree_t *btree, // core btree algorithm static int lfsr_btree_commit(lfs_t *lfs, lfsr_btree_t *btree, const lfsr_attr_t *attrs, lfs_size_t attr_count) { - // first find the effective bid and any changes to the number of tags + // TODO should we just use the first bid? + // first find the effective bid lfsr_bid_t bid = -1; - lfsr_srid_t tag_delta = 0; for (lfs_size_t i = 0; i < attr_count; i++) { // note unsigned min here chooses non-negative bids bid = lfs_min32(bid, attrs[i].rid); - - // non-grow tags with deltas change the number of tags - if (!lfsr_tag_isgrow(attrs[i].tag)) { - tag_delta += lfs_sclamp32(attrs[i].delta, -1, +1); - } - } - LFS_ASSERT(bid <= lfsr_btree_weight(btree)); - - // inlined btree? staying inlined? - if (lfsr_btree_isinlined(btree)) { - // staying inlined? - if (lfs_clamp32(lfsr_btree_weight(btree), 0, 1) + tag_delta <= 1) { - for (lfs_size_t i = 0; i < attr_count; i++) { - // update our inlined tag, make sure to strip wide/grow bits - if (lfsr_tag_suptype(lfsr_tag_key(attrs[i].tag)) - == LFSR_TAG_STRUCT) { - LFS_ASSERT(lfsr_data_size(&attrs[i].data) - <= LFSR_BTREE_INLINESIZE); - btree->u.inlined.tag = lfsr_tag_key(attrs[i].tag); - - lfsr_data_t data_ = attrs[i].data; - lfs_ssize_t d = lfsr_data_read(lfs, &data_, - btree->u.inlined.buf, LFSR_BTREE_INLINESIZE); - if (d < 0) { - return d; - } - btree->u.inlined.size = d; - } - - // update our btree weight - LFS_ASSERT((lfsr_sbid_t)lfsr_btree_weight(btree) - + attrs[i].delta >= 0); - btree->u.inlined.weight += attrs[i].delta; - } - return 0; - - // uninlining? - } else { - // allocate a root rbyd - lfsr_rbyd_t rbyd; - int err = lfsr_rbyd_alloc(lfs, &rbyd); - if (err) { - return err; - } - - // prepend inlined tag if we have one - if (lfsr_btree_weight(btree) > 0) { - err = lfsr_rbyd_appendattr(lfs, &rbyd, 0, - btree->u.inlined.tag, +lfsr_btree_weight(btree), - LFSR_DATA_BUF( - btree->u.inlined.buf, btree->u.inlined.size)); - if (err) { - return err; - } - } - - // commit our attrs - err = lfsr_rbyd_appendattrs(lfs, &rbyd, -1, -1, - attrs, attr_count); - if (err) { - return err; - } - - err = lfsr_rbyd_appendcksum(lfs, &rbyd); - if (err) { - return err; - } - - // save our new root - btree->u.rbyd = rbyd; - return 0; - } } + LFS_ASSERT(bid <= (lfsr_bid_t)btree->weight); // lookup in which leaf our bids resides // // for lfsr_btree_commit operations to work out, we need to // limit our bid to an rid in the tree, which is what this min // is doing - // - // note it's entirely possible for our btree to have a weight of - // zero here - lfsr_rbyd_t rbyd = btree->u.rbyd; - if (lfsr_btree_weight(btree) > 0) { + lfsr_rbyd_t rbyd = *btree; + if (btree->weight > 0) { lfsr_srid_t rid; int err = lfsr_btree_lookupnext_(lfs, btree, - lfs_min32(bid, lfsr_btree_weight(btree)-1), + lfs_min32(bid, btree->weight-1), &bid, &rbyd, &rid, NULL, NULL, NULL); if (err) { LFS_ASSERT(err != LFS_ERR_NOENT); @@ -4125,8 +3977,7 @@ static int lfsr_btree_commit(lfs_t *lfs, lfsr_btree_t *btree, lfsr_rbyd_t parent; lfsr_srid_t rid; // are we root? - if ((lfsr_bid_t)rbyd.weight == lfsr_btree_weight(btree) - || rbyd.weight == 0) { + if (rbyd.weight == btree->weight || rbyd.weight == 0) { // mark rid as -1 if we have no parent rid = -1; // mark btree as unerased in case of failure, our btree rbyd and @@ -4556,10 +4407,9 @@ static int lfsr_btree_commit(lfs_t *lfs, lfsr_btree_t *btree, // we must have a parent at this point, but is our parent the root // and is the root degenerate? LFS_ASSERT(rid != -1); - if ((lfsr_bid_t)(rbyd.weight+sibling.weight) - == lfsr_btree_weight(btree)) { + if (rbyd.weight+sibling.weight == btree->weight) { // collapse the root, decreasing the height of the tree - btree->u.rbyd = rbyd_; + *btree = rbyd_; return 0; } @@ -4583,14 +4433,14 @@ static int lfsr_btree_commit(lfs_t *lfs, lfsr_btree_t *btree, // done? if (rid == -1) { LFS_ASSERT(bid == 0); - btree->u.rbyd = rbyd_; + *btree = rbyd_; return 0; } // is our parent the root and is the root degenerate? - if ((lfsr_bid_t)rbyd.weight == lfsr_btree_weight(btree)) { + if (rbyd.weight == btree->weight) { // collapse the root, decreasing the height of the tree - btree->u.rbyd = rbyd_; + *btree = rbyd_; return 0; } @@ -4622,30 +4472,12 @@ static int lfsr_btree_namelookup(lfs_t *lfs, const lfsr_btree_t *btree, lfsr_bid_t *bid_, lfsr_tag_t *tag_, lfsr_bid_t *weight_, lfsr_data_t *data_) { // an empty tree? - if (lfsr_btree_weight(btree) == 0) { + if (btree->weight == 0) { return LFS_ERR_NOENT; } - // inlined? - if (lfsr_btree_isinlined(btree)) { - // TODO how many of these should be conditional? - if (bid_) { - *bid_ = lfsr_btree_weight(btree)-1; - } - if (tag_) { - *tag_ = btree->u.inlined.tag; - } - if (weight_) { - *weight_ = lfsr_btree_weight(btree); - } - if (data_) { - *data_ = LFSR_DATA_BUF(btree->u.inlined.buf, btree->u.inlined.size); - } - return 0; - } - // descend down the btree looking for our name - lfsr_rbyd_t branch = btree->u.rbyd; + lfsr_rbyd_t branch = *btree; lfsr_bid_t bid = 0; while (true) { // lookup our name in the rbyd via binary search @@ -4731,32 +4563,18 @@ static int lfsr_btraversal_read(lfs_t *lfs, const lfsr_btree_t *btree, lfsr_binfo_t *binfo) { while (true) { // in range? - if (traversal->bid >= lfsr_btree_weight(btree)) { + if (traversal->bid >= (lfsr_bid_t)btree->weight) { return LFS_ERR_NOENT; } - // inlined? - if (lfsr_btree_isinlined(btree)) { - // setup traversal to terminate next call - traversal->bid = lfsr_btree_weight(btree); - - binfo->bid = lfsr_btree_weight(btree)-1; - binfo->tag = btree->u.inlined.tag; - binfo->weight = lfsr_btree_weight(btree); - binfo->u.data = LFSR_DATA_BUF( - btree->u.inlined.buf, - btree->u.inlined.size); - return 0; - } - // restart from the root if (traversal->rid >= traversal->branch.weight) { traversal->bid += traversal->branch.weight; traversal->rid = traversal->bid; - traversal->branch = btree->u.rbyd; + traversal->branch = *btree; if (traversal->rid == 0) { - binfo->bid = lfsr_btree_weight(btree)-1; + binfo->bid = btree->weight-1; binfo->tag = LFSR_TAG_BRANCH; binfo->weight = traversal->branch.weight; binfo->u.rbyd = traversal->branch; @@ -6108,7 +5926,10 @@ static int lfsr_mdir_commit(lfs_t *lfs, lfsr_mdir_t *mdir, // new mtree? if (lfsr_mtree_ismptr(&mtree_)) { - mtree_.u.btree = LFSR_BTREE_NULL; + err = lfsr_btree_alloc(lfs, &mtree_.u.btree); + if (err) { + return err; + } uint8_t mdir_buf[LFSR_MPTR_DSIZE]; uint8_t msibling_buf[LFSR_MPTR_DSIZE]; @@ -6277,7 +6098,7 @@ static int lfsr_mdir_commit(lfs_t *lfs, lfsr_mdir_t *mdir, mtree_data = lfsr_data_frommptr(mtree_.u.mptr.blocks, mtree_buf); } else { mtree_tag = LFSR_TAG_WIDE(MTREE); - mtree_data = lfsr_data_frombtree(&mtree_.u.btree.u.rbyd, mtree_buf); + mtree_data = lfsr_data_frombtree(&mtree_.u.btree, mtree_buf); } err = lfsr_mdir_commit_(lfs, &mroot_, -1, 0, NULL, LFSR_ATTRS( @@ -7019,7 +6840,7 @@ static int lfsr_traversal_read(lfs_t *lfs, lfsr_traversal_t *traversal, // uninitialized in mountinited and 2. stack really matters since // we're at the bottom of lfs_alloc) if (binfo.tag == LFSR_TAG_BRANCH - && binfo.u.rbyd.block == lfs->mtree.u.btree.u.rbyd.block) { + && binfo.u.rbyd.block == lfs->mtree.u.btree.block) { continue; } @@ -7106,7 +6927,7 @@ static int lfsr_traversal_read(lfs_t *lfs, lfsr_traversal_t *traversal, // found a btree? } else if (err != LFS_ERR_NOENT && tag == LFSR_TAG_BTREE) { err = lfsr_data_readbtree(lfs, &data, - &traversal->btree.u.rbyd); + &traversal->btree); if (err) { return err; } @@ -7783,7 +7604,7 @@ static int lfsr_mountinited(lfs_t *lfs) { } else if (tinfo.tag == LFSR_TAG_BRANCH) { // found the root of the mtree? if (lfsr_mtree_isnull(&lfs->mtree)) { - lfs->mtree.u.btree.u.rbyd = tinfo.u.rbyd; + lfs->mtree.u.btree = tinfo.u.rbyd; } } else { @@ -8780,6 +8601,8 @@ int lfsr_dir_rewind(lfs_t *lfs, lfsr_dir_t *dir) { // sprout/shrub stuff #define LFSR_SHRUB_SPROUT 0x80000000 +#define LFSR_SHRUB_NULL ((lfsr_shrub_t){.u.data=LFSR_DATA_DISK(0, 0, 0)}) + static inline bool lfsr_shrub_isnull(const lfsr_shrub_t *shrub) { return (lfs_off_t)shrub->u.weight == (LFSR_SHRUB_SPROUT | 0); } @@ -8800,6 +8623,8 @@ static inline lfs_off_t lfsr_shrub_size(const lfsr_shrub_t *shrub) { // block/btree stuff #define LFSR_TREE_BPTR 0x80000000 +#define LFSR_TREE_NULL ((lfsr_tree_t){.u.size=(LFSR_TREE_BPTR | 0)}) + static inline bool lfsr_tree_isnull(const lfsr_tree_t *tree) { return (lfs_off_t)tree->u.size == (LFSR_TREE_BPTR | 0); } @@ -8900,9 +8725,9 @@ int lfsr_file_opencfg(lfs_t *lfs, lfsr_file_t *file, file->pos = 0; file->size = 0; // default inlined state - file->shrub.u.data = LFSR_DATA_DISK(0, 0, 0); + file->shrub = LFSR_SHRUB_NULL; // default btree state - file->tree.u.btree = LFSR_BTREE_NULL; + file->tree = LFSR_TREE_NULL; // lookup our parent lfsr_tag_t tag; @@ -9021,13 +8846,13 @@ int lfsr_file_opencfg(lfs_t *lfs, lfsr_file_t *file, } else if (err != LFS_ERR_NOENT && tag == LFSR_TAG_BTREE) { // TODO why does this not take a btree? err = lfsr_data_readbtree(lfs, &data, - &file->tree.u.btree.u.rbyd); + &file->tree.u.btree); if (err) { return err; } file->size = lfs_max32(file->size, - lfsr_btree_weight(&file->tree.u.btree)); + file->tree.u.btree.weight); } } } @@ -9751,7 +9576,7 @@ static int lfsr_tree_carve(lfs_t *lfs, lfsr_tree_t *tree, if (!lfsr_tree_hasbtree(tree)) { // TODO btree alloc? lfsr_btree_t btree_; - int err = lfsr_rbyd_alloc(lfs, &btree_.u.rbyd); + int err = lfsr_btree_alloc(lfs, &btree_); if (err) { return err; } @@ -10288,8 +10113,7 @@ static int lfsr_file_flushshrub(lfs_t *lfs, lfsr_file_t *file) { } // at this point both buffer and shrub should be flushed - // TODO LFSR_SHRUB_NULL? - file->shrub.u.data = LFSR_DATA_DISK(0, 0, 0); + file->shrub = LFSR_SHRUB_NULL; file->buffer_size = 0; return 0; } @@ -10463,7 +10287,7 @@ int lfsr_file_sync(lfs_t *lfs, lfsr_file_t *file) { : lfsr_tree_hasbtree(&file->tree) ? LFSR_ATTR(file->m.mdir.mid, BTREE, 0, FROMBTREE( - &file->tree.u.btree.u.rbyd, + &file->tree.u.btree, b_buf)) : LFSR_ATTR_NOOP))); if (err) { @@ -10548,9 +10372,8 @@ int lfsr_file_truncate(lfs_t *lfs, lfsr_file_t *file, lfs_off_t size) { file->buffer_size, size - lfs_min32(file->buffer_pos, size)); if (buffer_size >= size) { - // TODO LFSR_SHRUB_NULL/LFSR_TREE_NULL? - file->shrub.u.data = LFSR_DATA_DISK(0, 0, 0); - file->tree.u.btree = LFSR_BTREE_NULL; + file->shrub = LFSR_SHRUB_NULL; + file->tree = LFSR_TREE_NULL; // TODO, wait, could we just update file->size and leave it to // lfsr_file_sync to update the shrub? @@ -10617,9 +10440,8 @@ int lfsr_file_fruncate(lfs_t *lfs, lfsr_file_t *file, lfs_off_t size) { lfs_smax32(file->size - size - file->buffer_pos, 0), file->buffer_size); if (buffer_size >= size) { - // TODO LFSR_SHRUB_NULL/LFSR_TREE_NULL? - file->shrub.u.data = LFSR_DATA_DISK(0, 0, 0); - file->tree.u.btree = LFSR_BTREE_NULL; + file->shrub = LFSR_SHRUB_NULL; + file->tree = LFSR_TREE_NULL; // otherwise, we need to modify our sprout/shrub/bptr/btree } else { @@ -10657,7 +10479,7 @@ int lfsr_file_fruncate(lfs_t *lfs, lfsr_file_t *file, lfs_off_t size) { // revert btrees if they go to zero if ((lfs_soff_t)(file->size - size) >= (lfs_soff_t)lfsr_tree_size(&file->tree)) { - file->tree.u.btree = LFSR_BTREE_NULL; + file->tree = LFSR_TREE_NULL; } else { // TODO avoid transforming into trees all the time? int err = lfsr_tree_carve(lfs, &file->tree, diff --git a/lfs.h b/lfs.h index faee124b..3d3e5aaf 100644 --- a/lfs.h +++ b/lfs.h @@ -380,29 +380,7 @@ typedef struct lfsr_bptr { // TODO how do we track ecksum? } lfsr_bptr_t; -// The maximum size of inlined pointers in a btree, this depends on littlefs's -// on-disk pointer representations (there are several), but doesn't change at -// runtime. -// -// Pointers we store: -// - block addresses => 1 leb128 => 5 bytes (worst case) -// - mdir addresses => 2 leb128 => 10 bytes (worst case) -#define LFSR_BTREE_INLINESIZE 10 - -typedef struct lfsr_btree { - union { - // weight is common to both representations and its sign-bit indicates - // if the btree is inlined - lfsr_sbid_t weight; - struct { - lfsr_sbid_t weight; - lfsr_tag_t tag; - uint8_t size; - uint8_t buf[LFSR_BTREE_INLINESIZE]; - } inlined; - lfsr_rbyd_t rbyd; - } u; -} lfsr_btree_t; +typedef lfsr_rbyd_t lfsr_btree_t; typedef struct lfsr_mdir { lfsr_smid_t mid; diff --git a/tests/test_btree.toml b/tests/test_btree.toml index d94bd996..bd8e7b72 100644 --- a/tests/test_btree.toml +++ b/tests/test_btree.toml @@ -25,7 +25,7 @@ code = ''' static int lfsr_btree_push(lfs_t *lfs, lfsr_btree_t *btree, lfs_size_t bid, lfsr_tag_t tag, lfs_size_t weight, lfsr_data_t data) { - LFS_ASSERT(bid <= lfsr_btree_weight(btree)); + LFS_ASSERT(bid <= btree->weight); return lfsr_btree_commit(lfs, btree, LFSR_ATTRS( LFSR_ATTR(bid, TAG(tag), +weight, DATA(data)))); } @@ -33,8 +33,8 @@ code = ''' static int lfsr_btree_set(lfs_t *lfs, lfsr_btree_t *btree, lfs_size_t bid, lfsr_tag_t tag, lfs_size_t weight, lfsr_data_t data) { - LFS_ASSERT(bid < lfsr_btree_weight(btree)); - LFS_ASSERT(lfsr_btree_weight(btree) > 0); + LFS_ASSERT(bid < btree->weight); + LFS_ASSERT(btree->weight > 0); // lookup weight to compute deltas lfs_size_t weight_; @@ -53,8 +53,8 @@ code = ''' } static int lfsr_btree_pop(lfs_t *lfs, lfsr_btree_t *btree, lfs_size_t bid) { - LFS_ASSERT(bid < lfsr_btree_weight(btree)); - LFS_ASSERT(lfsr_btree_weight(btree) > 0); + LFS_ASSERT(bid < btree->weight); + LFS_ASSERT(btree->weight > 0); // lookup weight to compute deltas lfs_size_t weight_; @@ -72,8 +72,8 @@ code = ''' lfs_size_t bid, lfsr_data_t name, lfsr_tag_t tag1, lfs_size_t weight1, lfsr_data_t data1, lfsr_tag_t tag2, lfs_size_t weight2, lfsr_data_t data2) { - LFS_ASSERT(bid < lfsr_btree_weight(btree)); - LFS_ASSERT(lfsr_btree_weight(btree) > 0); + LFS_ASSERT(bid < btree->weight); + LFS_ASSERT(btree->weight > 0); // lookup weight to compute deltas lfs_size_t weight_; @@ -114,12 +114,13 @@ code = ''' lfs_alloc_ack(&lfs); // create an empty tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 0); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 0); // try looking up tags uint8_t buffer[4]; @@ -145,14 +146,15 @@ code = ''' lfs_alloc_ack(&lfs); // create a single-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("a", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 1); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 1); // try looking up tags uint8_t buffer[4]; @@ -184,16 +186,17 @@ code = ''' lfs_alloc_ack(&lfs); // create a two-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("a", 1)) => 0; lfsr_btree_push(&lfs, &btree, 1, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("b", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 2); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 2); // try looking up tags uint8_t buffer[4]; @@ -230,16 +233,17 @@ code = ''' lfs_alloc_ack(&lfs); // create a two-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("b", 1)) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("a", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 2); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 2); // try looking up tags uint8_t buffer[4]; @@ -277,7 +281,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a two-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("a", 1)) => 0; lfsr_btree_push(&lfs, &btree, 1, LFSR_TAG_DATA, 1, @@ -285,10 +290,10 @@ code = ''' lfsr_btree_push(&lfs, &btree, 2, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("c", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 3); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 3); // try looking up tags uint8_t buffer[4]; @@ -331,7 +336,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a two-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("c", 1)) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, @@ -339,10 +345,10 @@ code = ''' lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("a", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 3); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 3); // try looking up tags uint8_t buffer[4]; @@ -388,7 +394,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree with N elements - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; const char *alphas = "abcdefghijklmnopqrstuvwxyz"; lfs_size_t n = 0; for (lfs_size_t i = 0; i < N; i++) { @@ -402,10 +409,10 @@ code = ''' n += 1; } printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == n); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == n); // check that the elements are in the tree uint8_t buffer[4]; @@ -440,7 +447,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree with N elements - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; const char *alphas = "abcdefghijklmnopqrstuvwxyz"; lfs_size_t n = 0; for (lfs_size_t i = 0; i < N; i++) { @@ -454,10 +462,10 @@ code = ''' n += 1; } printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == n); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == n); // check that the elements are in the tree uint8_t buffer[4]; @@ -495,7 +503,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a btree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; // set up a simulation to compare against // @@ -537,10 +546,10 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == sim_size); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == sim_size); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -578,7 +587,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree with N elements - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; const char *alphas = "abcdefghijklmnopqrstuvwxyz"; lfs_size_t n = 0; for (lfs_size_t i = 0; i < N; i++) { @@ -592,10 +602,10 @@ code = ''' n += 1; } printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == n*W); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == n*W); // check that the elements are in the tree uint8_t buffer[4]; @@ -650,7 +660,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a btree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; // set up a simulation to compare against // @@ -713,15 +724,15 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); lfs_size_t total_weight = 0; for (lfs_size_t j = 0; j < sim_size; j++) { total_weight += sim_weights[j]; } - assert(lfsr_btree_weight(&btree) == total_weight); + assert(btree.weight == total_weight); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -788,17 +799,18 @@ code = ''' lfs_alloc_ack(&lfs); // create a single-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("a", 1)) => 0; // update the tree lfsr_btree_set(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("A", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 1); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 1); // try looking up tags uint8_t buffer[4]; @@ -829,7 +841,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a two-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("a", 1)) => 0; lfsr_btree_push(&lfs, &btree, 1, LFSR_TAG_DATA, 1, @@ -840,10 +853,10 @@ code = ''' lfsr_btree_set(&lfs, &btree, 1, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("B", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 2); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 2); // try looking up tags uint8_t buffer[4]; @@ -880,7 +893,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a two-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("a", 1)) => 0; lfsr_btree_push(&lfs, &btree, 1, LFSR_TAG_DATA, 1, @@ -895,10 +909,10 @@ code = ''' lfsr_btree_set(&lfs, &btree, 2, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("C", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 3); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 3); // try looking up tags uint8_t buffer[4]; @@ -942,7 +956,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree with N elements - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; const char *alphas = "abcdefghijklmnopqrstuvwxyz"; const char *uppers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for (lfs_size_t i = 0; i < N; i++) { @@ -951,9 +966,9 @@ code = ''' // ignore space issues if (err == LFS_ERR_NOSPC) { printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); return; } assert(err == 0); @@ -965,18 +980,18 @@ code = ''' // ignore space issues if (err == LFS_ERR_NOSPC) { printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); return; } assert(err == 0); } printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == N); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == N); // check that the elements are in the tree uint8_t buffer[4]; @@ -1016,16 +1031,17 @@ code = ''' lfs_alloc_ack(&lfs); // create a btree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; for (lfs_size_t i = 0; i < N; i++) { int err = lfsr_btree_push(&lfs, &btree, i, LFSR_TAG_DATA, 1, LFSR_DATA_BUF(&alphas[i % 26], 1)); // ignore space issues if (err == LFS_ERR_NOSPC) { printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); return; } assert(err == 0); @@ -1070,10 +1086,10 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == N); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == N); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -1110,7 +1126,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree with N elements - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; const char *alphas = "abcdefghijklmnopqrstuvwxyz"; const char *uppers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for (lfs_size_t i = 0; i < N; i++) { @@ -1119,9 +1136,9 @@ code = ''' // ignore space issues if (err == LFS_ERR_NOSPC) { printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); return; } assert(err == 0); @@ -1133,18 +1150,18 @@ code = ''' // ignore space issues if (err == LFS_ERR_NOSPC) { printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); return; } assert(err == 0); } printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == N*W); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == N*W); // check that the elements are in the tree uint8_t buffer[4]; @@ -1200,16 +1217,17 @@ code = ''' lfs_alloc_ack(&lfs); // create a btree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; for (lfs_size_t i = 0; i < N; i++) { int err = lfsr_btree_push(&lfs, &btree, i*W, LFSR_TAG_DATA, W, LFSR_DATA_BUF(&alphas[i % 26], 1)); // ignore space issues if (err == LFS_ERR_NOSPC) { printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); return; } assert(err == 0); @@ -1273,15 +1291,15 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); lfs_size_t total_weight = 0; for (lfs_size_t j = 0; j < N; j++) { total_weight += sim_weights[j]; } - assert(lfsr_btree_weight(&btree) == total_weight); + assert(btree.weight == total_weight); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -1349,16 +1367,17 @@ code = ''' lfs_alloc_ack(&lfs); // create a single-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("a", 1)) => 0; // pop! lfsr_btree_pop(&lfs, &btree, 0) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 0); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 0); // try looking up tags uint8_t buffer[4]; @@ -1372,10 +1391,10 @@ code = ''' lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("A", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 1); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 1); // try looking up tags lfsr_btree_get(&lfs, &btree, 0, @@ -1402,7 +1421,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a single-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("a", 1)) => 0; lfsr_btree_push(&lfs, &btree, 1, LFSR_TAG_DATA, 1, @@ -1410,10 +1430,10 @@ code = ''' // pop! lfsr_btree_pop(&lfs, &btree, 1) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 1); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 1); // try looking up tags uint8_t buffer[4]; @@ -1433,10 +1453,10 @@ code = ''' lfsr_btree_push(&lfs, &btree, 1, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("B", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 2); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 2); // try looking up tags lfsr_btree_get(&lfs, &btree, 0, @@ -1469,7 +1489,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a single-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("a", 1)) => 0; lfsr_btree_push(&lfs, &btree, 1, LFSR_TAG_DATA, 1, @@ -1477,10 +1498,10 @@ code = ''' // pop! lfsr_btree_pop(&lfs, &btree, 0) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 1); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 1); // try looking up tags uint8_t buffer[4]; @@ -1500,10 +1521,10 @@ code = ''' lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("A", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 2); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 2); // try looking up tags lfsr_btree_get(&lfs, &btree, 0, @@ -1536,7 +1557,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a single-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("a", 1)) => 0; lfsr_btree_push(&lfs, &btree, 1, LFSR_TAG_DATA, 1, @@ -1546,10 +1568,10 @@ code = ''' // pop! lfsr_btree_pop(&lfs, &btree, 2) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 2); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 2); // try looking up tags uint8_t buffer[4]; @@ -1575,10 +1597,10 @@ code = ''' lfsr_btree_push(&lfs, &btree, 2, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("C", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 3); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 3); // try looking up tags lfsr_btree_get(&lfs, &btree, 0, @@ -1620,7 +1642,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree with N elements - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; const char *alphas = "abcdefghijklmnopqrstuvwxyz"; for (lfs_size_t i = 0; i < N; i++) { int err = lfsr_btree_push(&lfs, &btree, i, LFSR_TAG_DATA, 1, @@ -1628,9 +1651,9 @@ code = ''' // ignore space issues if (err == LFS_ERR_NOSPC) { printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); return; } assert(err == 0); @@ -1641,19 +1664,19 @@ code = ''' // ignore space issues if (err == LFS_ERR_NOSPC) { printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); return; } assert(err == 0); } printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == REMAINING); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == REMAINING); // check that the elements are in the tree uint8_t buffer[4]; @@ -1711,7 +1734,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree with N elements - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; const char *alphas = "abcdefghijklmnopqrstuvwxyz"; for (lfs_size_t i = 0; i < N; i++) { int err = lfsr_btree_push(&lfs, &btree, i, LFSR_TAG_DATA, 1, @@ -1719,9 +1743,9 @@ code = ''' // ignore space issues if (err == LFS_ERR_NOSPC) { printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); return; } assert(err == 0); @@ -1732,18 +1756,18 @@ code = ''' // ignore space issues if (err == LFS_ERR_NOSPC) { printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); return; } assert(err == 0); } printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == REMAINING); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == REMAINING); // check that the elements are in the tree uint8_t buffer[4]; @@ -1804,16 +1828,17 @@ code = ''' lfs_alloc_ack(&lfs); // create a btree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; for (lfs_size_t i = 0; i < N; i++) { int err = lfsr_btree_push(&lfs, &btree, i, LFSR_TAG_DATA, 1, LFSR_DATA_BUF(&alphas[i % 26], 1)); // ignore space issues if (err == LFS_ERR_NOSPC) { printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); return; } assert(err == 0); @@ -1859,10 +1884,10 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == sim_size); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == sim_size); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -1901,7 +1926,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree with N elements - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; const char *alphas = "abcdefghijklmnopqrstuvwxyz"; for (lfs_size_t i = 0; i < N; i++) { int err = lfsr_btree_push(&lfs, &btree, i*W, LFSR_TAG_DATA, W, @@ -1909,9 +1935,9 @@ code = ''' // ignore space issues if (err == LFS_ERR_NOSPC) { printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); return; } assert(err == 0); @@ -1922,18 +1948,18 @@ code = ''' // ignore space issues if (err == LFS_ERR_NOSPC) { printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); return; } assert(err == 0); } printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == REMAINING*W); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == REMAINING*W); // check that the elements are in the tree uint8_t buffer[4]; @@ -2021,7 +2047,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a btree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; // set up a simulation to compare against // @@ -2049,9 +2076,9 @@ code = ''' // ignore space issues if (err == LFS_ERR_NOSPC) { printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); return; } assert(err == 0); @@ -2106,15 +2133,15 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); lfs_size_t total_weight = 0; for (lfs_size_t j = 0; j < sim_size; j++) { total_weight += sim_weights[j]; } - assert(lfsr_btree_weight(&btree) == total_weight); + assert(btree.weight == total_weight); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -2180,7 +2207,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree with N elements - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; const char *alphas = "abcdefghijklmnopqrstuvwxyz"; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF(&alphas[0 % 26], 1)) => 0; @@ -2197,10 +2225,10 @@ code = ''' n += 1; } printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == n); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == n); // check that the elements are in the tree uint8_t buffer[4]; @@ -2239,7 +2267,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a btree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("_", 1)) => 0; @@ -2286,10 +2315,10 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == sim_size); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == sim_size); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -2327,7 +2356,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree with N elements - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; const char *alphas = "abcdefghijklmnopqrstuvwxyz"; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, W, LFSR_DATA_BUF(&alphas[0 % 26], 1)) => 0; @@ -2344,10 +2374,10 @@ code = ''' n += 1; } printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == n*W); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == n*W); // check that the elements are in the tree uint8_t buffer[4]; @@ -2387,7 +2417,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a btree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, W, LFSR_DATA_BUF("_", 1)) => 0; @@ -2445,7 +2476,7 @@ code = ''' for (lfs_size_t j = 0; j < sim_size; j++) { total_weight += sim_weights[j]; } - assert(lfsr_btree_weight(&btree) == total_weight); + assert(btree.weight == total_weight); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -2484,15 +2515,15 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); lfs_size_t total_weight = 0; for (lfs_size_t j = 0; j < sim_size; j++) { total_weight += sim_weights[j]; } - assert(lfsr_btree_weight(&btree) == total_weight); + assert(btree.weight == total_weight); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -2560,7 +2591,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; // force it to split @@ -2575,21 +2607,21 @@ code = ''' LFSR_TAG_DATA, 1, LFSR_DATA_BUF(buf1, SIZE), LFSR_TAG_DATA, 1, LFSR_DATA_BUF(buf2, SIZE)) => 0; // force compaction - btree.u.rbyd.eoff = -1; + btree.eoff = -1; memset(buf2, 'b', SIZE); lfsr_btree_set(&lfs, &btree, 1, LFSR_TAG_DATA, 1, LFSR_DATA_BUF(buf2, SIZE)) => 0; - assert(lfsr_btree_weight(&btree) == 2); + assert(btree.weight == 2); // now remove one entry, since this brings the rbyd down to zero, // this should force one of the blocks to drop lfsr_btree_pop(&lfs, &btree, SIBLING) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 1); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 1); // check that our other entry is fine lfsr_tag_t tag_; @@ -2622,7 +2654,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; // force it to split @@ -2637,24 +2670,24 @@ code = ''' LFSR_TAG_DATA, 1, LFSR_DATA_BUF(buf1, SIZE), LFSR_TAG_DATA, 1, LFSR_DATA_BUF(buf2, SIZE)) => 0; // force compaction - btree.u.rbyd.eoff = -1; + btree.eoff = -1; memset(buf2, 'b', SIZE); lfsr_btree_set(&lfs, &btree, 1, LFSR_TAG_DATA, 1, LFSR_DATA_BUF(buf2, SIZE)) => 0; - assert(lfsr_btree_weight(&btree) == 2); + assert(btree.weight == 2); // now remove one entry, since this brings the rbyd down this zero, // this should force one of the blocks to drop // // do this while forcing a compaction - btree.u.rbyd.eoff = -1; + btree.eoff = -1; lfsr_btree_pop(&lfs, &btree, SIBLING) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 1); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 1); // check that our other entry is fine lfsr_tag_t tag_; @@ -2687,7 +2720,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; // force it to split @@ -2707,14 +2741,14 @@ code = ''' // // messy, isn't it? this is why we need an explicit test // - btree.u.rbyd.eoff = -1; + btree.eoff = -1; lfsr_btree_pop(&lfs, &btree, SIBLING) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 1); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 1); // check that our other entry is fine lfsr_tag_t tag_; @@ -2747,7 +2781,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; // force it to split @@ -2762,11 +2797,11 @@ code = ''' LFSR_TAG_DATA, 1, LFSR_DATA_BUF(buf1, SIZE), LFSR_TAG_DATA, 1, LFSR_DATA_BUF(buf2, SIZE)) => 0; // force compaction - btree.u.rbyd.eoff = -1; + btree.eoff = -1; memset(buf2, 'b', SIZE); lfsr_btree_set(&lfs, &btree, 1, LFSR_TAG_DATA, 1, LFSR_DATA_BUF(buf2, SIZE)) => 0; - assert(lfsr_btree_weight(&btree) == 2); + assert(btree.weight == 2); // now make both entries small so they should be merged if either compacts lfsr_btree_set(&lfs, &btree, 0, LFSR_TAG_DATA, 1, @@ -2776,14 +2811,14 @@ code = ''' // force compaction, while removing one entry, this drops the rbyd // down to zero while also triggering a merge - btree.u.rbyd.eoff = -1; + btree.eoff = -1; lfsr_btree_pop(&lfs, &btree, SIBLING) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 1); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 1); // check that our other entry is fine lfsr_tag_t tag_; @@ -2819,7 +2854,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a btree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; // set up a simulation to compare against // @@ -2893,10 +2929,10 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == sim_size); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == sim_size); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -2936,7 +2972,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a btree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; // set up a simulation to compare against // @@ -3034,15 +3071,15 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); lfs_size_t total_weight = 0; for (lfs_size_t j = 0; j < sim_size; j++) { total_weight += sim_weights[j]; } - assert(lfsr_btree_weight(&btree) == total_weight); + assert(btree.weight == total_weight); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -3107,12 +3144,13 @@ code = ''' lfs_alloc_ack(&lfs); // create a zero-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 0); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 0); // try to find tags lfsr_tag_t tag_; @@ -3140,14 +3178,15 @@ code = ''' lfs_alloc_ack(&lfs); // create a single-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("0", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 1); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 1); // try to find tags uint8_t buffer[4]; @@ -3189,7 +3228,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a two-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("0", 1)) => 0; lfsr_btree_split(&lfs, &btree, 0, @@ -3197,10 +3237,10 @@ code = ''' LFSR_TAG_DATA, 1, LFSR_DATA_BUF("0", 1), LFSR_TAG_DATA, 1, LFSR_DATA_BUF("1", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 2); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 2); // try to find tags uint8_t buffer[4]; @@ -3250,7 +3290,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a two-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("0", 1)) => 0; lfsr_btree_split(&lfs, &btree, 0, @@ -3262,10 +3303,10 @@ code = ''' LFSR_TAG_DATA, 1, LFSR_DATA_BUF("1", 1), LFSR_TAG_DATA, 1, LFSR_DATA_BUF("2", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 3); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 3); // try to find tags uint8_t buffer[4]; @@ -3323,7 +3364,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a two-entry tree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("0", 1)) => 0; lfsr_btree_split(&lfs, &btree, 0, @@ -3335,10 +3377,10 @@ code = ''' LFSR_TAG_DATA, 1, LFSR_DATA_BUF("0", 1), LFSR_TAG_DATA, 1, LFSR_DATA_BUF("1", 1)) => 0; printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == 3); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == 3); // try to find tags uint8_t buffer[4]; @@ -3397,7 +3439,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree with N elements - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; const char *alphas = "abcdefghijklmnopqrstuvwxyz"; const char *nums = "0123456789"; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, @@ -3419,10 +3462,10 @@ code = ''' n += 1; } printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == n); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == n); // try to find tags uint8_t buffer[4]; @@ -3465,7 +3508,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a btree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("_", 1)) => 0; @@ -3531,10 +3575,10 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == sim_size); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == sim_size); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -3575,7 +3619,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree with N elements - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; const char *alphas = "abcdefghijklmnopqrstuvwxyz"; const char *nums = "0123456789"; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, W, @@ -3597,10 +3642,10 @@ code = ''' n += 1; } printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == n*W); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == n*W); // try to find tags uint8_t buffer[4]; @@ -3644,7 +3689,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a btree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, W, LFSR_DATA_BUF("_", 1)) => 0; @@ -3737,15 +3783,15 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); lfs_size_t total_weight = 0; for (lfs_size_t j = 0; j < sim_size; j++) { total_weight += sim_weights[j]; } - assert(lfsr_btree_weight(&btree) == total_weight); + assert(btree.weight == total_weight); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -3795,7 +3841,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a btree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, 1, LFSR_DATA_BUF("_", 1)) => 0; @@ -3925,10 +3972,10 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == sim_size); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == sim_size); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -3970,7 +4017,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a btree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; lfsr_btree_push(&lfs, &btree, 0, LFSR_TAG_DATA, W, LFSR_DATA_BUF("_", 1)) => 0; @@ -4139,15 +4187,15 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); + btree.weight, + btree.block, + btree.trunk); lfs_size_t total_weight = 0; for (lfs_size_t j = 0; j < sim_size; j++) { total_weight += sim_weights[j]; } - assert(lfsr_btree_weight(&btree) == total_weight); + assert(btree.weight == total_weight); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -4195,7 +4243,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a tree with N elements - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; const char *alphas = "abcdefghijklmnopqrstuvwxyz"; lfs_size_t n = 0; for (lfs_size_t i = 0; i < N; i++) { @@ -4209,10 +4258,10 @@ code = ''' n += 1; } printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == n); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == n); // check that the elements are in the tree uint8_t buffer[4]; @@ -4238,7 +4287,7 @@ code = ''' lfsr_btraversal_t traversal = LFSR_BTRAVERSAL(); for (lfs_block_t i = 0;; i++) { // a bit hacky, but this catches infinite loops - assert(i < 2*N); + assert(i <= 2*N); lfsr_binfo_t binfo; int err = lfsr_btraversal_read(&lfs, &btree, &traversal, &binfo); @@ -4319,7 +4368,8 @@ code = ''' lfs_alloc_ack(&lfs); // create a btree - lfsr_btree_t btree = LFSR_BTREE_NULL; + lfsr_btree_t btree; + lfsr_btree_alloc(&lfs, &btree) => 0; // set up a simulation to compare against // @@ -4361,10 +4411,10 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == sim_size); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == sim_size); uint8_t buffer[4]; lfsr_tag_t tag_; @@ -4389,7 +4439,7 @@ code = ''' lfsr_btraversal_t traversal = LFSR_BTRAVERSAL(); for (lfs_block_t i = 0;; i++) { // a bit hacky, but this catches infinite loops - assert(i < 2*N); + assert(i <= 2*N); lfsr_binfo_t binfo; int err = lfsr_btraversal_read(&lfs, &btree, &traversal, &binfo); @@ -4450,10 +4500,10 @@ code = ''' } printf("]\n"); printf("btree: w%d 0x%x.%x\n", - btree.u.rbyd.weight, - btree.u.rbyd.block, - btree.u.rbyd.trunk); - assert(lfsr_btree_weight(&btree) == sim_size); + btree.weight, + btree.block, + btree.trunk); + assert(btree.weight == sim_size); for (lfs_size_t i = 0; i < sim_size; i++) { lfsr_btree_get(&lfs, &btree, i,