From 8b0bdb090e2882aa431e89f4bc7aa4736e9e2838 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 25 Jun 2014 11:58:57 +0930 Subject: [PATCH] structeq: new module. World's most trivial module, but I want it for pettycoin. Signed-off-by: Rusty Russell --- Makefile-ccan | 1 + ccan/structeq/LICENSE | 1 + ccan/structeq/_info | 57 +++++++++++++++++++++ ccan/structeq/structeq.h | 17 ++++++ ccan/structeq/test/compile_fail-different.c | 22 ++++++++ ccan/structeq/test/run.c | 27 ++++++++++ 6 files changed, 125 insertions(+) create mode 120000 ccan/structeq/LICENSE create mode 100644 ccan/structeq/_info create mode 100644 ccan/structeq/structeq.h create mode 100644 ccan/structeq/test/compile_fail-different.c create mode 100644 ccan/structeq/test/run.c diff --git a/Makefile-ccan b/Makefile-ccan index 6410301d..40d03891 100644 --- a/Makefile-ccan +++ b/Makefile-ccan @@ -23,6 +23,7 @@ MODS_NO_SRC := alignof \ minmax \ objset \ short_types \ + structeq \ tcon \ tlist \ typesafe_cb \ diff --git a/ccan/structeq/LICENSE b/ccan/structeq/LICENSE new file mode 120000 index 00000000..b7951dab --- /dev/null +++ b/ccan/structeq/LICENSE @@ -0,0 +1 @@ +../../licenses/CC0 \ No newline at end of file diff --git a/ccan/structeq/_info b/ccan/structeq/_info new file mode 100644 index 00000000..d66e2960 --- /dev/null +++ b/ccan/structeq/_info @@ -0,0 +1,57 @@ +#include "config.h" +#include +#include + +/** + * structeq - bitwise comparison of structs. + * + * This is a replacement for memcmp, which checks the argument types are the + * same. + * + * License: CC0 (Public domain) + * Author: Rusty Russell + * + * Example: + * #include + * #include + * #include + * + * struct mydata { + * int start, end; + * }; + * + * int main(void) + * { + * struct mydata a, b; + * + * // No padding in struct, otherwise this doesn't work! + * BUILD_ASSERT(sizeof(a) == sizeof(a.start) + sizeof(a.end)); + * + * a.start = 100; + * a.end = 101; + * + * b.start = 100; + * b.end = 101; + * + * // They are equal. + * assert(structeq(&a, &b)); + * + * b.end++; + * // Now they are not. + * assert(!structeq(&a, &b)); + * + * return 0; + * } + */ +int main(int argc, char *argv[]) +{ + /* Expect exactly one argument */ + if (argc != 2) + return 1; + + if (strcmp(argv[1], "depends") == 0) { + return 0; + } + + return 1; +} diff --git a/ccan/structeq/structeq.h b/ccan/structeq/structeq.h new file mode 100644 index 00000000..3af20c53 --- /dev/null +++ b/ccan/structeq/structeq.h @@ -0,0 +1,17 @@ +/* CC0 (Public domain) - see LICENSE file for details */ +#ifndef CCAN_STRUCTEQ_H +#define CCAN_STRUCTEQ_H +#include + +/** + * structeq - are two structures bitwise equal (including padding!) + * @a: a pointer to a structure + * @b: a pointer to a structure of the same type. + * + * If you *know* a structure has no padding, you can memcmp them. At + * least this way, the compiler will issue a warning if the structs are + * different types! + */ +#define structeq(a, b) \ + (memcmp((a), (b), sizeof(*(a)) + 0 * sizeof((a) == (b))) == 0) +#endif /* CCAN_STRUCTEQ_H */ diff --git a/ccan/structeq/test/compile_fail-different.c b/ccan/structeq/test/compile_fail-different.c new file mode 100644 index 00000000..9a08503f --- /dev/null +++ b/ccan/structeq/test/compile_fail-different.c @@ -0,0 +1,22 @@ +#include + +struct mydata1 { + int start, end; +}; + +struct mydata2 { + int start, end; +}; + +int main(void) +{ + struct mydata1 a = { 0, 100 }; +#ifdef FAIL + struct mydata2 +#else + struct mydata1 +#endif + b = { 0, 100 }; + + return structeq(&a, &b); +} diff --git a/ccan/structeq/test/run.c b/ccan/structeq/test/run.c new file mode 100644 index 00000000..9ecb4b7d --- /dev/null +++ b/ccan/structeq/test/run.c @@ -0,0 +1,27 @@ +#include +#include + +struct mydata { + int start, end; +}; + +int main(void) +{ + struct mydata a, b; + + /* This is how many tests you plan to run */ + plan_tests(3); + + a.start = 0; + a.end = 100; + ok1(structeq(&a, &a)); + + b = a; + ok1(structeq(&a, &b)); + + b.end++; + ok1(!structeq(&a, &b)); + + /* This exits depending on whether all tests passed */ + return exit_status(); +} -- 2.39.2