From 0a640f50409986e8a83316587475f603e7bab200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Fri, 22 Dec 2023 21:51:53 +0100 Subject: [PATCH] Add container_of macro Ref: https://github.com/bellard/quickjs/commit/c3599515c87faea1bab5a0c95a93f45090ea2561 --- cutils.h | 4 ++++ list.h | 3 +-- quickjs.c | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/cutils.h b/cutils.h index 23b3da4..2fa6d8d 100644 --- a/cutils.h +++ b/cutils.h @@ -66,6 +66,10 @@ #define endof(x) ((x) + countof(x)) #endif #endif +#ifndef container_of +/* return the pointer of type 'type *' containing 'ptr' as field 'member' */ +#define container_of(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member))) +#endif typedef int BOOL; diff --git a/list.h b/list.h index c23193f..8098311 100644 --- a/list.h +++ b/list.h @@ -36,8 +36,7 @@ struct list_head { #define LIST_HEAD_INIT(el) { &(el), &(el) } /* return the pointer of type 'type *' containing 'el' as field 'member' */ -#define list_entry(el, type, member) \ - ((type *)((uint8_t *)(el) - offsetof(type, member))) +#define list_entry(el, type, member) container_of(el, type, member) static inline void init_list_head(struct list_head *head) { diff --git a/quickjs.c b/quickjs.c index 8725414..cc7bc1a 100644 --- a/quickjs.c +++ b/quickjs.c @@ -4025,7 +4025,7 @@ void JS_FreeCString(JSContext *ctx, const char *ptr) if (!ptr) return; /* purposely removing constness */ - p = (JSString *)(void *)(ptr - offsetof(JSString, u)); + p = container_of(ptr, JSString, u); JS_FreeValue(ctx, JS_MKPTR(JS_TAG_STRING, p)); }