summaryrefslogtreecommitdiffstats
path: root/trie.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2020-09-18 16:39:40 -0400
committerTavian Barnes <tavianator@tavianator.com>2020-10-13 13:42:11 -0400
commitc565e665781f39cf31c64d85c76224c2fffa9f7d (patch)
treeed8d167bd38d2d23f39875ffe25793de3416948a /trie.c
parentfd0b968b5afe28d9adcfbc9aef17bc83d6eaf84b (diff)
downloadbfs-c565e665781f39cf31c64d85c76224c2fffa9f7d.tar.xz
util: New BFS_FLEX_SIZEOF() macro for more precise flexible array allocations
See http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_282.htm for all the fun behind this.
Diffstat (limited to 'trie.c')
-rw-r--r--trie.c5
1 files changed, 3 insertions, 2 deletions
diff --git a/trie.c b/trie.c
index b9adc8c..6489b0c 100644
--- a/trie.c
+++ b/trie.c
@@ -77,6 +77,7 @@
*/
#include "trie.h"
+#include "util.h"
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
@@ -319,7 +320,7 @@ struct trie_leaf *trie_find_prefix(const struct trie *trie, const char *key) {
/** Create a new leaf, holding a copy of the given key. */
static struct trie_leaf *new_trie_leaf(const void *key, size_t length) {
- struct trie_leaf *leaf = malloc(sizeof(*leaf) + length);
+ struct trie_leaf *leaf = malloc(BFS_FLEX_SIZEOF(struct trie_leaf, key, length));
if (leaf) {
leaf->value = NULL;
leaf->length = length;
@@ -335,7 +336,7 @@ static size_t trie_node_size(unsigned int size) {
// Node size must be a power of two
assert((size & (size - 1)) == 0);
- return sizeof(struct trie_node) + size*sizeof(uintptr_t);
+ return BFS_FLEX_SIZEOF(struct trie_node, children, size);
}
/** Find the offset of the first nibble that differs between two keys. */