summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-09-10 20:11:31 -0400
committerDavid Robillard <d@drobilla.net>2021-09-10 20:11:31 -0400
commitf562429e1b03d1947492dd35db1dcfd5a396fbab (patch)
tree8c68a1a73e5d19493093fe5d27580a69b7bb9c88
parente48bf96abd8e872979b74ba95a02992c7bfac4ca (diff)
downloadzix-f562429e1b03d1947492dd35db1dcfd5a396fbab.tar.gz
zix-f562429e1b03d1947492dd35db1dcfd5a396fbab.tar.bz2
zix-f562429e1b03d1947492dd35db1dcfd5a396fbab.zip
Avoid implicit padding in BTree nodes on 64-bit
Might as well use 32-bit integers if the space is there anyway.
-rw-r--r--src/btree.c23
1 files changed, 15 insertions, 8 deletions
diff --git a/src/btree.c b/src/btree.c
index 3553a9a..e4d2f41 100644
--- a/src/btree.c
+++ b/src/btree.c
@@ -24,11 +24,18 @@
// #define ZIX_BTREE_DEBUG 1
// #define ZIX_BTREE_SORTED_CHECK 1
+// Define ZixShort as an integer type half the size of a pointer
+#if UINTPTR_MAX >= UINT32_MAX
+typedef uint32_t ZixShort;
+#else
+typedef uint16_t ZixShort;
+#endif
+
#ifndef ZIX_BTREE_PAGE_SIZE
# define ZIX_BTREE_PAGE_SIZE 4096u
#endif
-#define ZIX_BTREE_NODE_SPACE (ZIX_BTREE_PAGE_SIZE - 2u * sizeof(uint16_t))
+#define ZIX_BTREE_NODE_SPACE (ZIX_BTREE_PAGE_SIZE - 2u * sizeof(ZixShort))
#define ZIX_BTREE_LEAF_VALS ((ZIX_BTREE_NODE_SPACE / sizeof(void*)) - 1u)
#define ZIX_BTREE_INODE_VALS (ZIX_BTREE_LEAF_VALS / 2u)
@@ -42,9 +49,9 @@ struct ZixBTreeImpl {
};
struct ZixBTreeNodeImpl {
- uint16_t is_leaf;
- uint16_t n_vals;
- // On 64-bit we rely on some padding here to get page-sized nodes
+ ZixShort is_leaf;
+ ZixShort n_vals;
+
union {
struct {
void* vals[ZIX_BTREE_LEAF_VALS];
@@ -169,18 +176,18 @@ zix_btree_free_rec(ZixBTree* const t, ZixBTreeNode* const n)
if (n) {
if (n->is_leaf) {
if (t->destroy) {
- for (uint16_t i = 0; i < n->n_vals; ++i) {
+ for (ZixShort i = 0u; i < n->n_vals; ++i) {
t->destroy(n->data.leaf.vals[i]);
}
}
} else {
if (t->destroy) {
- for (uint16_t i = 0; i < n->n_vals; ++i) {
+ for (ZixShort i = 0u; i < n->n_vals; ++i) {
t->destroy(n->data.inode.vals[i]);
}
}
- for (uint16_t i = 0; i < n->n_vals + 1; ++i) {
+ for (ZixShort i = 0u; i < n->n_vals + 1u; ++i) {
zix_btree_free_rec(t, zix_btree_child(n, i));
}
}
@@ -686,7 +693,7 @@ zix_btree_remove(ZixBTree* const t,
} else if (n == t->root && n->n_vals == 1) {
// Root has two children, both minimal, delete it
assert(i == 0 || i == 1);
- const uint16_t counts[2] = {zix_btree_child(n, 0)->n_vals,
+ const ZixShort counts[2] = {zix_btree_child(n, 0)->n_vals,
zix_btree_child(n, 1)->n_vals};
n = zix_btree_merge(t, n, 0);