Skip to content

Commit c359951

Browse files
author
Fabrice Bellard
committed
added container_of macro
1 parent 1692f2a commit c359951

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

cutils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
#define countof(x) (sizeof(x) / sizeof((x)[0]))
5050
#endif
5151

52+
/* return the pointer of type 'type *' containing 'ptr' as field 'member' */
53+
#define container_of(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member)))
54+
5255
typedef int BOOL;
5356

5457
#ifndef FALSE

list.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ struct list_head {
3636
#define LIST_HEAD_INIT(el) { &(el), &(el) }
3737

3838
/* return the pointer of type 'type *' containing 'el' as field 'member' */
39-
#define list_entry(el, type, member) \
40-
((type *)((uint8_t *)(el) - offsetof(type, member)))
39+
#define list_entry(el, type, member) container_of(el, type, member)
4140

4241
static inline void init_list_head(struct list_head *head)
4342
{

quickjs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4064,7 +4064,7 @@ void JS_FreeCString(JSContext *ctx, const char *ptr)
40644064
if (!ptr)
40654065
return;
40664066
/* purposely removing constness */
4067-
p = (JSString *)(void *)(ptr - offsetof(JSString, u));
4067+
p = container_of(ptr, JSString, u);
40684068
JS_FreeValue(ctx, JS_MKPTR(JS_TAG_STRING, p));
40694069
}
40704070

0 commit comments

Comments
 (0)