commit b4ba392c6d266f2ab5acaff8162ea84516721235
parent d178483e04d505f77f01fe9da478ca5281e3e331
Author: Fabrice Bellard <fabrice@bellard.org>
Date: Fri, 22 Dec 2023 11:03:44 +0100
added container_of macro
Diffstat:
3 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/quickjs/cutils.h b/quickjs/cutils.h
@@ -49,6 +49,9 @@
#define countof(x) (sizeof(x) / sizeof((x)[0]))
#endif
+/* return the pointer of type 'type *' containing 'ptr' as field 'member' */
+#define container_of(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member)))
+
typedef int BOOL;
#ifndef FALSE
diff --git a/quickjs/list.h b/quickjs/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/quickjs.c b/quickjs/quickjs.c
@@ -4068,7 +4068,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));
}