diff options
Diffstat (limited to 'libmisc')
-rw-r--r-- | libmisc/include/libmisc/assert.h | 17 | ||||
-rw-r--r-- | libmisc/include/libmisc/linkedlist.h | 10 | ||||
-rw-r--r-- | libmisc/include/libmisc/macro.h | 6 | ||||
-rw-r--r-- | libmisc/include/libmisc/map.h | 2 |
4 files changed, 22 insertions, 13 deletions
diff --git a/libmisc/include/libmisc/assert.h b/libmisc/include/libmisc/assert.h index 8cf0735..ccdb288 100644 --- a/libmisc/include/libmisc/assert.h +++ b/libmisc/include/libmisc/assert.h @@ -1,6 +1,6 @@ /* libmisc/assert.h - More assertions * - * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com> * SPDX-License-Identifier: AGPL-3.0-or-later */ @@ -10,15 +10,20 @@ #ifdef NDEBUG # define __assert_msg(expr, expr_str, msg) ((void)0) #else -# define __assert_msg(expr, expr_str, msg) do { if (!(expr)) __assert_msg_fail(expr_str, __FILE__, __LINE__, __func__, msg); } while (0) +# define __assert_msg(expr, expr_str, msg) \ + do { \ + if (!(expr)) \ + __assert_msg_fail(expr_str, __FILE__, __LINE__, __func__, msg); \ + } while (0) [[noreturn]] void __assert_msg_fail(const char *expr, const char *file, unsigned int line, const char *func, const char *msg); #endif -#define assert_msg(expr, msg) __assert_msg(expr, #expr, msg) /* libmisc */ -#define assert(expr) __assert_msg(expr, #expr, 0) /* C89, POSIX-2001 */ -#define assert_notreached(msg) do { __assert_msg(0, "notreached", msg); __builtin_unreachable(); } while (0) /* libmisc */ -#define static_assert _Static_assert /* C11 */ +#define assert_msg(expr, msg) __assert_msg(expr, #expr, msg) /* libmisc */ +#define assert(expr) __assert_msg(expr, #expr, 0) /* C89, POSIX-2001 */ +#define assert_notreached(msg) do { __assert_msg(0, "notreached", msg); __builtin_unreachable(); } while (0) /* libmisc */ +#define static_assert _Static_assert /* C11 */ +#define static_assert_as_expr(...) (sizeof(struct {static_assert(__VA_ARGS__);})) /* libmisc */ #endif /* _LIBMISC_ASSERT_H_ */ diff --git a/libmisc/include/libmisc/linkedlist.h b/libmisc/include/libmisc/linkedlist.h index e8c65db..b6ff688 100644 --- a/libmisc/include/libmisc/linkedlist.h +++ b/libmisc/include/libmisc/linkedlist.h @@ -56,14 +56,14 @@ void _dlist_pop_from_front(struct _dlist_root *root); typeof(*_rootp->front) *_nodep = NODE; \ _slist_push_to_rear((struct _slist_root *)_rootp, \ (struct _slist_node *)_nodep); \ -} while(0) +} while (0) #define slist_pop_from_front(ROOT) { \ /* This temporary variables are to get the compiler to check \ * the type. */ \ typeof(*(ROOT)->_slist_root_typ[0]) *_rootp = ROOT; \ _slist_pop_from_front((struct _slist_root *)_rootp); \ -} while(0) +} while (0) /* doubly linked list (non-intrusive) *****************************************/ @@ -87,7 +87,7 @@ void _dlist_pop_from_front(struct _dlist_root *root); typeof(*_rootp->front) *_nodep = NODE; \ _dlist_push_to_rear((struct _dlist_root *)_rootp, \ (struct _dlist_node *)_nodep); \ -} while(0) +} while (0) #define dlist_remove(ROOT, NODE) { \ /* These temporary variables are to get the compiler to check \ @@ -96,13 +96,13 @@ void _dlist_pop_from_front(struct _dlist_root *root); typeof(*_rootp->front) *_nodep = NODE; \ _dlist_remove((struct _dlist_root *)_rootp, \ (struct _dlist_node *)_nodep); \ -} while(0) +} while (0) #define dlist_pop_from_front(ROOT) { \ /* This temporary variables are to get the compiler to check \ * the type. */ \ typeof(*(ROOT)->_dlist_root_typ[0]) *_rootp = ROOT; \ _dlist_pop_from_front((struct _dlist_root *)_rootp); \ -} while(0) +} while (0) #endif /* _LIBMISC_LINKEDLIST_H_ */ diff --git a/libmisc/include/libmisc/macro.h b/libmisc/include/libmisc/macro.h index 6cb15fb..a95ac82 100644 --- a/libmisc/include/libmisc/macro.h +++ b/libmisc/include/libmisc/macro.h @@ -7,6 +7,8 @@ #ifndef _LIBMISC_MACRO_H_ #define _LIBMISC_MACRO_H_ +#include <libmisc/assert.h> + /* for function definitions */ #define LM_UNUSED(argname) @@ -16,7 +18,9 @@ /* types */ -#define LM_ARRAY_LEN(ary) (sizeof(ary)/sizeof((ary)[0])) +/* If it's a pointer instead of an array, then typeof(&ptr[0]) == typeof(ptr) */ +#define _LM_IS_ARRAY(ary) (!__builtin_types_compatible_p(typeof(&(ary)[0]), typeof(ary))) +#define LM_ARRAY_LEN(ary) ( (sizeof(ary)/sizeof((ary)[0])) + static_assert_as_expr(_LM_IS_ARRAY(ary)) ) #define LM_CAST_FIELD_TO_STRUCT(STRUCT_TYP, FIELD_NAME, PTR_TO_FIELD) ({ \ /* The _fptr assignment is to get the compiler to do type checking. */ \ diff --git a/libmisc/include/libmisc/map.h b/libmisc/include/libmisc/map.h index 41ac069..6622595 100644 --- a/libmisc/include/libmisc/map.h +++ b/libmisc/include/libmisc/map.h @@ -52,7 +52,7 @@ struct _map { (M)->core.offsetof_k = offsetof(typeof((M)->kv_typ[0]), val.key); \ (M)->core.offsetof_v = offsetof(typeof((M)->kv_typ[0]), val.val); \ } \ -} while(0) +} while (0) /* Methods ********************************************************************/ |