toys

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

commit df9b3ae7e4b1a93812ef46d86cf6d23e825a34d5
parent babca69b3485ac8d3434490773c07419849e62b0
Author: MikoĊ‚aj Lenczewski <mblenczewski@gmail.com>
Date:   Thu, 23 Oct 2025 15:41:00 +0100

Tweak utils.h

Diffstat:
Mlist.h | 9+--------
Mutils.h | 12++++++++++++
2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/list.h b/list.h @@ -5,13 +5,7 @@ #include <stdint.h> #include <stdlib.h> -/* we define an "intrusive" list, meaning that out data struct contains a - * list_node member. this allows a type-generic list implementation, but - * necessitates need a way to get from a pointer to this list_node, to a - * pointer to the parent struct - */ -#define TO_PARENT(ptr, T, member) \ - ((T *) ((uintptr_t) (ptr) - offsetof(T, member))) +#include "utils.h" /* we choose to implement a doubly-linked list for simplicity and flexible * use in situations that require either singly-linked or doubly-linked lists @@ -33,7 +27,6 @@ struct list_node { #define LIST_INIT(list) ((struct list_node) { .prev = &(list), .next = &(list), }) #define LIST_EMTPY(list) (LIST_HEAD(list) == (list) && LIST_TAIL(list) == (list)) - inline void list_node_link(struct list_node *node, struct list_node *prev, struct list_node *next) { diff --git a/utils.h b/utils.h @@ -1,6 +1,10 @@ #ifndef UTILS_H #define UTILS_H +#include <stdalign.h> +#include <stddef.h> +#include <stdint.h> + #define KiB(v) (1024ull * (v)) #define MiB(v) (1024 * KiB(v)) #define GiB(v) (1024 * MiB(v)) @@ -8,6 +12,14 @@ #define ARRLEN(arr) (sizeof (arr) / sizeof (arr)[0]) +/* we define an "intrusive" list, meaning that out data struct contains a + * list_node member. this allows a type-generic list implementation, but + * necessitates need a way to get from a pointer to this list_node, to a + * pointer to the parent struct + */ +#define TO_PARENT(ptr, T, member) \ + ((T *) ((uintptr_t) (ptr) - offsetof(T, member))) + /* helper macros to allow us to use bitwise tricks to quickly and efficiently * calculate aligned addresses and sizes. */