util+treewide: container_of() cleanup

Replace mesa's slightly different container_of() with one more aligned
to the linux kernel's version which takes a type as the 2nd param.  This
avoids warnings like:

  freedreno_context.c:396:44: warning: variable 'batch' is uninitialized when used within its own initialization [-Wuninitialized]

At the same time, we can add additional build-time type-checking asserts

Signed-off-by: Rob Clark <robdclark@chromium.org>
Acked-by: Erik Faye-Lund <erik.faye-lund@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7941>
This commit is contained in:
Rob Clark
2020-12-05 11:56:45 -08:00
committed by Marge Bot
parent 6fe84c5dda
commit 790144e65a
18 changed files with 59 additions and 57 deletions

View File

@@ -87,6 +87,27 @@
# define STATIC_ASSERT(COND) do { } while (0)
#endif
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*/
#ifndef __GNUC__
/* a grown-up compiler is required for the extra type checking: */
# define container_of(ptr, type, member) \
(type*)((uint8_t *)ptr - offsetof(type, member))
#else
# define __same_type(a, b) \
__builtin_types_compatible_p(__typeof__(a), __typeof__(b))
# define container_of(ptr, type, member) ({ \
uint8_t *__mptr = (uint8_t *)(ptr); \
STATIC_ASSERT(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void) || \
!"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); \
})
#endif
/**
* Unreachable macro. Useful for suppressing "control reaches end of non-void