Add container_of macro

Ref: c3599515c8
This commit is contained in:
Saúl Ibarra Corretgé 2023-12-22 21:51:53 +01:00
parent c1a3b64382
commit 0a640f5040
3 changed files with 6 additions and 3 deletions

View file

@ -66,6 +66,10 @@
#define endof(x) ((x) + countof(x)) #define endof(x) ((x) + countof(x))
#endif #endif
#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; typedef int BOOL;

3
list.h
View file

@ -36,8 +36,7 @@ struct list_head {
#define LIST_HEAD_INIT(el) { &(el), &(el) } #define LIST_HEAD_INIT(el) { &(el), &(el) }
/* return the pointer of type 'type *' containing 'el' as field 'member' */ /* return the pointer of type 'type *' containing 'el' as field 'member' */
#define list_entry(el, type, member) \ #define list_entry(el, type, member) container_of(el, type, member)
((type *)((uint8_t *)(el) - offsetof(type, member)))
static inline void init_list_head(struct list_head *head) static inline void init_list_head(struct list_head *head)
{ {

View file

@ -4025,7 +4025,7 @@ void JS_FreeCString(JSContext *ctx, const char *ptr)
if (!ptr) if (!ptr)
return; return;
/* purposely removing constness */ /* 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)); JS_FreeValue(ctx, JS_MKPTR(JS_TAG_STRING, p));
} }