From c565e665781f39cf31c64d85c76224c2fffa9f7d Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 18 Sep 2020 16:39:40 -0400 Subject: util: New BFS_FLEX_SIZEOF() macro for more precise flexible array allocations See http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_282.htm for all the fun behind this. --- util.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'util.h') diff --git a/util.h b/util.h index a9bdecf..15e983f 100644 --- a/util.h +++ b/util.h @@ -26,6 +26,7 @@ #include #include #include +#include #include // Some portability concerns @@ -91,6 +92,28 @@ # define BFS_FORMATTER(fmt, args) #endif +// Lower bound on BFS_FLEX_SIZEOF() +#define BFS_FLEX_LB(type, member, length) (offsetof(type, member) + sizeof(((type *)NULL)->member[0]) * (length)) + +// Maximum macro for BFS_FLEX_SIZE() +#define BFS_FLEX_MAX(a, b) ((a) > (b) ? (a) : (b)) + +/** + * Computes the size of a struct containing a flexible array member of the given + * length. + * + * @param type + * The type of the struct containing the flexible array. + * @param member + * The name of the flexible array member. + * @param length + * The length of the flexible array. + */ +#define BFS_FLEX_SIZEOF(type, member, length) \ + (sizeof(type) <= BFS_FLEX_LB(type, member, 0) \ + ? BFS_FLEX_LB(type, member, length) \ + : BFS_FLEX_MAX(sizeof(type), BFS_FLEX_LB(type, member, length))) + /** * readdir() wrapper that makes error handling cleaner. */ -- cgit v1.2.3