lib/sizeof.h: memberof(): Add macro

This macro is useful to get the size of a member of a structure
without having a variable of that type.

Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2024-02-18 18:32:31 +01:00
parent 8d1f0bcf99
commit 544709fad3
+5 -4
View File
@@ -15,10 +15,11 @@
#include "must_be.h"
#define WIDTHOF(x) (sizeof(x) * CHAR_BIT)
#define SIZEOF_ARRAY(a) (sizeof(a) + must_be_array(a))
#define NITEMS(a) (SIZEOF_ARRAY((a)) / sizeof((a)[0]))
#define STRLEN(s) (NITEMS(s) - 1)
#define memberof(T, member) ((T){}.member)
#define WIDTHOF(x) (sizeof(x) * CHAR_BIT)
#define SIZEOF_ARRAY(a) (sizeof(a) + must_be_array(a))
#define NITEMS(a) (SIZEOF_ARRAY((a)) / sizeof((a)[0]))
#define STRLEN(s) (NITEMS(s) - 1)
#endif // include guard