From cc3db07ecc15f3e1e556387d82318697956e8e0c Mon Sep 17 00:00:00 2001 From: David Gibson Date: Tue, 20 Oct 2015 01:46:41 +1100 Subject: [PATCH] tcon: Add tcon_sizeof() helper Add a new tcon_sizeof() helper macro which returns the size of a type for which a canary has been constructed with TCON or TCON_WRAP. Signed-off-by: David Gibson --- ccan/tcon/tcon.h | 7 ++++++ ccan/tcon/test/compile_ok-sizeof.c | 35 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 ccan/tcon/test/compile_ok-sizeof.c diff --git a/ccan/tcon/tcon.h b/ccan/tcon/tcon.h index a256f103..62bacf53 100644 --- a/ccan/tcon/tcon.h +++ b/ccan/tcon/tcon.h @@ -159,6 +159,13 @@ #define tcon_type(x, canary) void * #endif +/** + * tcon_sizeof - the size of type within a container + * @x: the structure containing the TCON. + * @canary: which canary to check against. + */ +#define tcon_sizeof(x, canary) sizeof((x)->_tcon[0].canary) + /** * tcon_ptr_type - pointer to the type within a container (or void *) * @x: the structure containing the TCON. diff --git a/ccan/tcon/test/compile_ok-sizeof.c b/ccan/tcon/test/compile_ok-sizeof.c new file mode 100644 index 00000000..64ccc136 --- /dev/null +++ b/ccan/tcon/test/compile_ok-sizeof.c @@ -0,0 +1,35 @@ +#include +#include +#include + +struct container { + void *p; +}; + +struct int_container { + struct container raw; + TCON(int tc); +}; + +struct charp_and_int_container { + struct container raw; + TCON(int tc1; char *tc2); +}; + +int main(int argc, char *argv[]) +{ + struct int_container icon; + struct charp_and_int_container cicon; + TCON_WRAP(struct container, int tc) iconw; + TCON_WRAP(struct container, int tc1; char *tc2) ciconw; + + BUILD_ASSERT(tcon_sizeof(&icon, tc) == sizeof(int)); + BUILD_ASSERT(tcon_sizeof(&cicon, tc1) == sizeof(int)); + BUILD_ASSERT(tcon_sizeof(&cicon, tc2) == sizeof(char *)); + + BUILD_ASSERT(tcon_sizeof(&iconw, tc) == sizeof(int)); + BUILD_ASSERT(tcon_sizeof(&ciconw, tc1) == sizeof(int)); + BUILD_ASSERT(tcon_sizeof(&ciconw, tc2) == sizeof(char *)); + + return 0; +} -- 2.39.2