toys

toys.git
git clone git://git.lenczewski.org/toys.git
Log | Files | Refs | README | LICENSE

commit a5594b59cb12558282cba3beb74eba97fe168969
parent d7c4a85236076592c53b4b623e36b49475e775d5
Author: MikoĊ‚aj Lenczewski <mblenczewski@gmail.com>
Date:   Sat,  4 Oct 2025 23:14:03 +0100

Added standalone assert implementation

Diffstat:
Aassert.h | 10++++++++++
Mbuild.sh | 3++-
Mlist.c | 22++++++++++++++++++++++
Mlist.h | 24------------------------
Mtrie.c | 20+++++++++++++++++++-
Mtrie.h | 36+++++++++---------------------------
6 files changed, 62 insertions(+), 53 deletions(-)

diff --git a/assert.h b/assert.h @@ -0,0 +1,10 @@ +#ifndef ASSERT_H +#define ASSERT_H + +#ifdef NDEBUG +# define ASSERT(cond) +#else +# define ASSERT(cond) do { if (!(cond)) __builtin_trap(); } while (0); +#endif + +#endif /* ASSERT_H */ diff --git a/build.sh b/build.sh @@ -1,6 +1,7 @@ #!/bin/sh -FLAGS="-Wall -Wextra -Wpedantic -Wno-format-pedantic -std=c99 -Og -g" +WARNINGS="-Wall -Wextra -Wno-format-pedantic -Wno-unused-variable" +FLAGS="$WARNINGS -std=c99 -O0 -g3" set -ex diff --git a/list.c b/list.c @@ -1,6 +1,28 @@ #define HEADER_IMPL #include "list.h" +#include <stdio.h> + +static inline void +list_print(struct list_node const *list, int reversed) +{ + fprintf(stderr, "list: %p, head: %p, tail: %p\n", + (void *) list, (void *) LIST_HEAD(list), (void *) LIST_TAIL(list)); + + struct list_node *it; + if (reversed) { + LIST_ITER(list, it) { + fprintf(stderr, "\t{ %p, prev: %p, next: %p }\n", + (void *) it, (void *) it->prev, (void *) it->next); + } + } else { + LIST_RITER(list, it) { + fprintf(stderr, "\t{ %p, prev: %p, next: %p }\n", + (void *) it, (void *) it->prev, (void *) it->next); + } + } +} + struct mystruct { int a; struct list_node list_node; diff --git a/list.h b/list.h @@ -3,7 +3,6 @@ #include <stddef.h> #include <stdint.h> -#include <stdio.h> #include <stdlib.h> /* we define an "intrusive" list, meaning that out data struct contains a @@ -101,26 +100,6 @@ list_pop_tail(struct list_node *list) &(it)->member != (list); \ (it) = LIST_NODE_ENTRY((it)->member.prev, __typeof__ (*(it)), member)) -inline void -list_print(struct list_node const *list, int reversed) -{ - fprintf(stderr, "list: %p, head: %p, tail: %p\n", - (void *) list, (void *) LIST_HEAD(list), (void *) LIST_TAIL(list)); - - struct list_node *it; - if (reversed) { - LIST_ITER(list, it) { - fprintf(stderr, "\t{ %p, prev: %p, next: %p }\n", - (void *) it, (void *) it->prev, (void *) it->next); - } - } else { - LIST_RITER(list, it) { - fprintf(stderr, "\t{ %p, prev: %p, next: %p }\n", - (void *) it, (void *) it->prev, (void *) it->next); - } - } -} - #endif /* LIST_H */ #ifdef HEADER_IMPL @@ -144,7 +123,4 @@ list_pop_head(struct list_node *list); extern inline struct list_node * list_pop_tail(struct list_node *list); -extern inline void -list_print(struct list_node const *list, int reversed); - #endif /* HEADER_IMPL */ diff --git a/trie.c b/trie.c @@ -1,11 +1,29 @@ #define HEADER_IMPL #include "trie.h" +#include <assert.h> +#include <stdio.h> + +static inline void +trie_display(struct trie_node const *trie, size_t indent) +{ + for (size_t i = 0; i < indent; i++) + printf(" "); + + printf("{ %p, prefix: \"%.*s\", }\n", + trie, (int) (trie->prefix.end - trie->prefix.begin), trie->prefix.begin); + + struct trie_node *it; + LIST_ENTRY_ITER(&trie->children, it, list_node) { + trie_display(it, indent + 1); + } +} + int main(void) { char buf[8192]; - struct arena arena = { .ptr = buf, .cap = sizeof buf, .len = 0, }; + struct arena_allocator arena = { .ptr = buf, .cap = sizeof buf, .len = 0, }; printf("trie_init()\n"); struct trie_node *root = trie_init(&arena); diff --git a/trie.h b/trie.h @@ -1,10 +1,10 @@ #ifndef TRIE_H #define TRIE_H -#include <assert.h> #include <stddef.h> -#include "arena.h" +#include "assert.h" +#include "allocators.h" #include "list.h" /* a trie is a data structure that holds a trie of string prefixes. we can walk @@ -40,7 +40,7 @@ struct trie_node { #endif /* TRIE_ADAPTIVE_RADIX */ inline struct trie_node * -trie_init(struct arena *arena) +trie_init(struct arena_allocator *arena) { struct trie_node *res = ARENA_ALLOC_SIZED(arena, struct trie_node); if (!res) @@ -57,7 +57,7 @@ trie_init(struct arena *arena) inline char const * _trie_split_prefix(struct trie_node const *trie, char const *key, size_t len) { - assert(trie->prefix.begin && trie->prefix.end); + ASSERT(trie->prefix.begin && trie->prefix.end); char const *cur = trie->prefix.begin, *key_end = key + len; while (cur < trie->prefix.end && key < key_end) { @@ -87,10 +87,10 @@ _trie_split_prefix(struct trie_node const *trie, char const *key, size_t len) * append child node to parent node with prefix: key */ inline struct trie_node * -trie_insert(struct arena *arena, struct trie_node *trie, char const *key, size_t len) +trie_insert(struct arena_allocator *arena, struct trie_node *trie, char const *key, size_t len) { - assert(key); - assert(len); + ASSERT(key); + ASSERT(len); struct trie_node *res = NULL; @@ -234,38 +234,20 @@ trie_search(struct trie_node const *trie, char const *key, size_t len) return NULL; } -inline void -trie_display(struct trie_node const *trie, size_t indent) -{ - for (size_t i = 0; i < indent; i++) - printf(" "); - - printf("{ %p, prefix: \"%.*s\", }\n", - trie, (int) (trie->prefix.end - trie->prefix.begin), trie->prefix.begin); - - struct trie_node *it; - LIST_ENTRY_ITER(&trie->children, it, list_node) { - trie_display(it, indent + 1); - } -} - #endif /* TRIE_H */ #ifdef HEADER_IMPL extern inline struct trie_node * -trie_init(struct arena *arena); +trie_init(struct arena_allocator *arena); extern inline char const * _trie_split_prefix(struct trie_node const *trie, char const *key, size_t len); extern inline struct trie_node * -trie_insert(struct arena *arena, struct trie_node *trie, char const *key, size_t len); +trie_insert(struct arena_allocator *arena, struct trie_node *trie, char const *key, size_t len); extern inline struct trie_node const * trie_search(struct trie_node const *trie, char const *key, size_t len); -extern inline void -trie_display(struct trie_node const *trie, size_t indent); - #endif /* HEADER_IMPL */