From 501bdb51e428e9b8c1941a73f30b908884b346c4 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 24 Oct 2023 15:32:45 +0200 Subject: [PATCH] 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. --- glib/glib-private.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/glib/glib-private.h b/glib/glib-private.h index dae496bfd..e96a73fb6 100644 --- a/glib/glib-private.h +++ b/glib/glib-private.h @@ -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: *