From d13172d535846f66954722c24c136eb657de7209 Mon Sep 17 00:00:00 2001 From: thetek Date: Sun, 12 Feb 2023 18:28:44 +0100 Subject: [PATCH] feat: (common) next_pow_of_two, min, max --- lib/debug.c | 8 +++----- lib/inc/common.h | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/debug.c b/lib/debug.c index 8bdab01..ac4b4fa 100644 --- a/lib/debug.c +++ b/lib/debug.c @@ -96,7 +96,7 @@ debug_free (void *ptr, const char *file, size_t line) * automatically inserts filename and line. */ void * -debug_malloc (size_t size, const char *file, size_t line) +debug_smalloc (size_t size, const char *file, size_t line) { void *ptr; @@ -117,13 +117,11 @@ debug_malloc (size_t size, const char *file, size_t line) * * @return the pointer to the reallocated memory, or NULL in case of error * - * @errno see realloc(3) - * - * @usage the header `debug.h` defines a macro `realloc(ptr, size)` that + * @usage the header `debug.h` defines a macro `srealloc(ptr, size)` that * automatically inserts filename and line. */ void * -debug_realloc (void *ptr, size_t size, const char *file, size_t line) +debug_srealloc (void *ptr, size_t size, const char *file, size_t line) { void *new_ptr; uintptr_t old_ptr; diff --git a/lib/inc/common.h b/lib/inc/common.h index b220295..59e9627 100644 --- a/lib/inc/common.h +++ b/lib/inc/common.h @@ -1,6 +1,20 @@ #ifndef CUTILS_COMMON_H_ #define CUTILS_COMMON_H_ +#include +#include + +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) + +#if UINTPTR_MAX == 0xffffffffffffffff +# define next_pow_of_two(n) (((size_t) 1) << (64 - __builtin_clzll (n))) +#elif UINTPTR_MAX == 0xffffffff +# define next_pow_of_two(n) (((size_t) 1) << (64 - __builtin_clz (n))) +#else +# error unsupported architecture: size_t expected to be 64-bit or 32-bit. +#endif + void *smalloc (size_t size); void *srealloc (void *ptr, size_t size);