glib: add internal G_CONTAINER_OF() macro

"container_of()" is a popular macro that works together with
G_STRUCT_OFFSET()/offsetof().

For internal use only.
This commit is contained in:
Thomas Haller 2023-10-24 15:32:45 +02:00
parent 3c09257ea1
commit 501bdb51e4

View File

@ -63,6 +63,18 @@ void __lsan_ignore_object (const void *p) __attribute__ ((weak));
#endif
/**
* G_CONTAINER_OF:
* @ptr: a pointer to a member @field of type @type.
* @type: the type of the container in which @field is embedded.
* @field: the name of the field in @type.
*
* Casts away constness of @ptr.
*
* Returns: a pointer to the container, so that "&(@container)->field == (@ptr)" holds.
*/
#define G_CONTAINER_OF(ptr, type, field) ((type *) G_STRUCT_MEMBER_P (ptr, -G_STRUCT_OFFSET (type, field)))
/*
* g_leak_sanitizer_is_supported:
*