For implementing a B+Tree I have a bunch of constants which are all calculated based on the page size.
At the end I can calculate a (reasonable) max depth of the tree and use this in my code to allocate slices which will always be big enough to hold the entire path to leaf node.
pub const MAX_DEPTH = max_depth_block: {
// max file size on ext4 is 16 TiB
const max_file_size = 16 * 1024 * 1024 * 1024 * 1024;
// this will never be reached as the file also contains the tree
const max_points = max_file_size / POINT_SIZE;
const max_leaf_page = max_points / LEAF_CAPACITY;
// log rounds down -> +1
const max_depth = std.math.log(comptime_int, MAX_LINKS, max_leaf_page) + 1;
break :max_depth_block max_depth;
};
Wrote my first data structure using comptime to replace an ArrayList where the maximum size is known at compile time. As I use it as a stack, it only implements append and pop as that is all I need.