std-aux: add _NM_ENSURE_POINTER() macro

This commit is contained in:
Thomas Haller 2021-07-30 10:38:40 +02:00
parent 387d5ded93
commit 4484363df0
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -258,6 +258,17 @@ typedef uint64_t _nm_bitwise nm_be64_t;
#define _NM_ENSURE_TYPE_CONST(type, value) ((const type) (value))
#endif
/* returns void, but does a compile time check that the argument is a pointer
* (that is, can be converted to (const void *)). It does not actually evaluate
* (value). That means, it's also safe to call _NM_ENSURE_POINTER(array[0]) if
* array might be NULL. It's also safe to call on a macro argument that is
* supposed to be evaluate at most once (this macro will not "execute" the
* argument). */
#define _NM_ENSURE_POINTER(value) \
do { \
_nm_unused const void *const _unused_for_type_check = 0 ? (value) : NULL; \
} while (0)
#if _NM_CC_SUPPORT_GENERIC && (!defined(__clang__) || __clang_major__ > 3)
#define NM_STRUCT_OFFSET_ENSURE_TYPE(type, container, field) \
(_Generic((&(((container *) NULL)->field))[0], type : nm_offsetof(container, field)))