liblist-c

C type-agnostic linked-list library
git clone https://git.sinitax.com/sinitax/liblist-c
Log | Files | Refs | LICENSE | sfeed.txt

commit e25f7bbf238b47675c922428abdb6c638e1a01e0
parent 87a08258d9a6d83899b8aa71444e7cb8024eb685
Author: Louis Burda <quent.burda@gmail.com>
Date:   Sun, 13 Feb 2022 11:56:54 +0100

More collision resistant and generalized macros

Diffstat:
Minclude/list.h | 10++++------
Msrc/test.c | 2+-
2 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/include/list.h b/include/list.h @@ -2,15 +2,13 @@ #include <stdlib.h> -#define LINK_OFFSET(type) ((size_t) &((type *)0)->link) -#define UPCAST(ptr, type) ({ \ - const typeof( ((type *)0)->link ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - LINK_OFFSET(type) ); }) +#define LINK_OFFSET(type, member) ((size_t) &((type *)0)->member) +#define LINK_UPCAST(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - LINK_OFFSET(type, member) ); }) #define LINK_EMPTY ((struct link) { 0 }) -#define LINK(p) (&(p)->link) - #define LIST_INNER(list, link) \ (((link) != &(list)->head) && ((link) != &(list)->tail)) diff --git a/src/test.c b/src/test.c @@ -28,7 +28,7 @@ main(int argc, const char **argv) } for (LIST_ITER(&list, iter)) { - item = UPCAST(iter, struct arg); + item = LINK_UPCAST(iter, struct arg, link); printf("%s\n", item->str); } }