From 32afd10e99da476c851e10abff2aba12a0fc49ea Mon Sep 17 00:00:00 2001 From: Felix Kopp Date: Wed, 14 Oct 2020 11:47:42 +0000 Subject: [PATCH] Minor macro refactor --- include/ardix/util.h | 10 ++++++++++ lib/malloc.c | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/ardix/util.h b/include/ardix/util.h index 9423be3..b2320ba 100644 --- a/include/ardix/util.h +++ b/include/ardix/util.h @@ -16,6 +16,16 @@ #define container_of(ptr, type, member) \ ( (type *)((void *)(ptr) - offsetof(type, member)) ) +/** + * Get the size of a struct member. + * + * @param type The type of the containing struct. + * @param member The name of the member within the struct. + * @return The size of the member in bytes. + */ +#define SIZEOF_MEMBER(type, member) \ + (sizeof( ((type *)0)->member )) + /* * Copyright (c) 2020 Felix Kopp * diff --git a/lib/malloc.c b/lib/malloc.c index d81b21e..5128a17 100644 --- a/lib/malloc.c +++ b/lib/malloc.c @@ -75,7 +75,7 @@ struct memblk { }; /** The length of the `size` member in `struct memblk`. */ -#define MEMBLK_SIZE_LENGTH (sizeof( typeof(((struct memblk *)0)->size) )) +#define MEMBLK_SIZE_LENGTH SIZEOF_MEMBER(struct memblk, size) /** Total overhead per allocated block in bytes (2 * size_t). */ #define MEMBLK_OVERHEAD (2 * MEMBLK_SIZE_LENGTH)