From 6c36b3da63ce0c0a4a2281d182f1b4de2e5bef69 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Sun, 13 Oct 2024 15:53:32 -0400 Subject: all: #ifdef -> #if defined Signed-off-by: Andrew Opalach --- include/al/array_typed.h | 2 +- include/al/atomic.h | 4 ++-- include/al/lib.h | 20 ++++++++++++-------- include/al/log.h | 5 ++++- include/al/types.h | 6 +++--- include/al/wstr.h | 7 ++++--- src/lib.c | 24 ++++++++++++------------ src/log.c | 34 ++++++++++++++++++++++++---------- tests/main.c | 4 ++-- 9 files changed, 64 insertions(+), 42 deletions(-) diff --git a/include/al/array_typed.h b/include/al/array_typed.h index a48b230..09289f2 100644 --- a/include/al/array_typed.h +++ b/include/al/array_typed.h @@ -1,7 +1,7 @@ #ifndef _AL_ARRAY_TYPED_H #define _AL_ARRAY_TYPED_H -#ifndef AL_HAVE_GNU_EXTENSIONS +#if !defined AL_HAVE_GNU_EXTENSIONS #error "missing required GNU C extensions" #endif diff --git a/include/al/atomic.h b/include/al/atomic.h index 7f8a087..7d070ab 100644 --- a/include/al/atomic.h +++ b/include/al/atomic.h @@ -20,8 +20,8 @@ #define al_atomic_sub(T) _al_atomic_sub_##T // Special case for pointers. -#define _al_atomic_load_void(p, order) c89atomic_load_explicit_ptr(((volatile void **)p), order) -#define _al_atomic_store_void(p, v, order) c89atomic_store_explicit_ptr(((volatile void **)p), (void *)v, order) +#define _al_atomic_load_void(p, order) c89atomic_load_explicit_ptr((volatile void **)p, order) +#define _al_atomic_store_void(p, v, order) c89atomic_store_explicit_ptr((volatile void **)p, (void *)v, order) // c89atomic compat. typedef f64 c89atomic_f64; diff --git a/include/al/lib.h b/include/al/lib.h index 32b91ad..606d24d 100644 --- a/include/al/lib.h +++ b/include/al/lib.h @@ -4,9 +4,10 @@ #include "types.h" #define AL_USE_STDLIB 1 + #define AL_FORCE_DISABLE_OUTPUT 0 -#if defined(__clang__) || defined(__GNUC__) +#if defined __clang__ || defined __GNUC__ #define AL_UNUSED_FUNCTION_PUSH \ _Pragma("GCC diagnostic push") \ _Pragma("GCC diagnostic ignored \"-Wunused-function\"") @@ -33,7 +34,7 @@ void *al_malloc(size_t n); void *al_calloc(size_t n, size_t size); void *al_realloc(void *ptr, size_t n); void al_free(void *ptr); -#ifdef AL_HAVE_POSIX_MEMALIGN +#if defined AL_HAVE_POSIX_MEMALIGN int al_posix_memalign(void **ptr, size_t alignment, size_t n); #endif void al_malloc_stats(u32 *current, u32 *peak, u32 *total); @@ -48,7 +49,7 @@ void al_set_alloc(void *(*malloc_func)(size_t), void *(*calloc_func)(size_t, siz #define al_bzero bzero #define al_strlen strlen #define al_sprintf sprintf -#ifdef _DEBUG_ +#if defined _DEBUG_ #define al_assert assert #else #define al_assert(expr) do { (void)sizeof(expr); } while (0) // NOLINT(bugprone-sizeof-expression) @@ -66,6 +67,7 @@ AL_UNUSED_FUNCTION_PUSH static inline s32 al_printf(const char *fmt, ...) { #if AL_FORCE_DISABLE_OUTPUT + (void)fmt; return 0; #else va_list args; @@ -79,6 +81,8 @@ static inline s32 al_printf(const char *fmt, ...) static inline s32 al_vprintf(const char *fmt, va_list args) { #if AL_FORCE_DISABLE_OUTPUT + (void)fmt; + (void)args; return 0; #else s32 ret = vprintf(fmt, args); @@ -100,16 +104,16 @@ static inline void *al_memrchr(const void *s, s32 c, size_t n) AL_UNUSED_FUNCTION_POP #endif -#ifndef LIKELY -#ifdef AL_HAVE_BUILTIN_EXPECT +#if !defined LIKELY +#if defined AL_HAVE_BUILTIN_EXPECT #define LIKELY(x) __builtin_expect((x), 1) #else #define LIKELY(x) x #endif #endif -#ifndef UNLIKELY -#ifdef AL_HAVE_BUILTIN_EXPECT +#if !defined UNLIKELY +#if defined AL_HAVE_BUILTIN_EXPECT #define UNLIKELY(x) __builtin_expect((x), 0) #else #define UNLIKELY(x) x @@ -145,7 +149,7 @@ AL_UNUSED_FUNCTION_POP #define AL_BOOLSTR(b) ((b) ? "true" : "false") -#ifndef _MSVC_ +#if !defined _MSVC_ #define __al_thread __thread #else #define __al_thread __declspec(thread) diff --git a/include/al/log.h b/include/al/log.h index cf53503..87b25c2 100644 --- a/include/al/log.h +++ b/include/al/log.h @@ -18,15 +18,18 @@ s32 _al_logv(const char *level, const char *section, const char *name, const s32 #define al_log_info(section, fmt, ...) al_log("info", section, fmt, ##__VA_ARGS__) #define al_log_warn(section, fmt, ...) al_log("warn", section, fmt, ##__VA_ARGS__) #define al_log_error(section, fmt, ...) al_log("error", section, fmt, ##__VA_ARGS__) -#ifdef _DEBUG_ +#if defined _DEBUG_ #define al_log_debug(section, fmt, ...) al_log("debug", section, fmt, ##__VA_ARGS__) #else AL_UNUSED_FUNCTION_PUSH + static inline s32 al_log_nop(const char *section, const char *fmt, ...) { (void)section; (void)fmt; return 0; } + AL_UNUSED_FUNCTION_POP + #define al_log_debug(section, fmt, ...) al_log_nop(section, fmt, ##__VA_ARGS__) #endif diff --git a/include/al/types.h b/include/al/types.h index 3a04539..136d8bd 100644 --- a/include/al/types.h +++ b/include/al/types.h @@ -1,7 +1,7 @@ #ifndef _AL_TYPES_H #define _AL_TYPES_H -#ifdef _POSIX_C_SOURCE +#if defined _POSIX_C_SOURCE #if _POSIX_C_SOURCE < 200112L #error "libalabaster requires _POSIX_C_SOURCE >= 200112L" #endif @@ -9,7 +9,7 @@ #define _POSIX_C_SOURCE 200112L #endif -#ifdef _XOPEN_SOURCE +#if defined _XOPEN_SOURCE #if _XOPEN_SOURCE < 500 #error "libalabaster requires _XOPEN_SOURCE >= 500" #endif @@ -35,7 +35,7 @@ typedef uint64_t u64; typedef float f32; typedef double f64; -#ifdef _MSC_VER +#if defined _MSC_VER #include typedef SSIZE_T ssize_t; #endif diff --git a/include/al/wstr.h b/include/al/wstr.h index ed2c051..1202882 100644 --- a/include/al/wstr.h +++ b/include/al/wstr.h @@ -1,6 +1,6 @@ #ifndef _AL_WSTR_H #define _AL_WSTR_H -#include "types.h" + #include "lib.h" #include "str.h" @@ -31,7 +31,7 @@ AL_UNUSED_FUNCTION_PUSH static inline s32 al_wchar_width(wchar_t wc) { -#ifdef AL_HAVE_WIDE_STRING +#if defined AL_HAVE_WIDE_STRING return wcwidth(wc); #else (void)wc; @@ -98,7 +98,7 @@ static inline void al_wstr_to_str(wstr *w, str *s) static inline s32 al_wstr_width(wstr *w) { -#ifdef AL_HAVE_WIDE_STRING +#if defined AL_HAVE_WIDE_STRING return wcswidth(w->data, w->len); #else (void)w; @@ -120,4 +120,5 @@ static inline s32 al_wstr_rfind(wstr *w, wchar_t c) } AL_UNUSED_FUNCTION_POP + #endif // _AL_WSTR_H diff --git a/src/lib.c b/src/lib.c index 0033463..8f67be3 100644 --- a/src/lib.c +++ b/src/lib.c @@ -12,13 +12,13 @@ static void *(*_al_malloc)(size_t) = malloc; static void *(*_al_calloc)(size_t, size_t) = calloc; static void *(*_al_realloc)(void *, size_t) = realloc; static void (*_al_free)(void *) = free; -#ifdef AL_HAVE_POSIX_MEMALIGN +#if defined AL_HAVE_POSIX_MEMALIGN s32 (*_al_posix_memalign)(void **, size_t, size_t) = posix_memalign; #endif // Testing allocation tracking stuff (Very scuffed, not thread-safe!). -#define MEMORY_TRACKING 0 -#if MEMORY_TRACKING +#define AL_MEMORY_TRACKING 0 +#if AL_MEMORY_TRACKING struct alloc_t { void *ptr; size_t size; @@ -41,14 +41,14 @@ static u32 total_allocated = 0; void al_malloc_init(void) { -#if MEMORY_TRACKING +#if AL_MEMORY_TRACKING al_array_init(allocations); #endif } void al_malloc_close(void) { -#if MEMORY_TRACKING +#if AL_MEMORY_TRACKING al_array_free(allocations); #endif } @@ -56,7 +56,7 @@ void al_malloc_close(void) void *al_malloc(size_t n) { void *ptr = _al_malloc(n); -#if MEMORY_TRACKING +#if AL_MEMORY_TRACKING printf("**al_malloc(%zu)\n", n); struct alloc_t a = { .ptr = ptr, @@ -75,7 +75,7 @@ void *al_malloc(size_t n) void *al_calloc(size_t n, size_t size) { void *ptr = _al_calloc(n, size); -#if MEMORY_TRACKING +#if AL_MEMORY_TRACKING printf("**al_calloc(%zu, %zu)\n", n, size); struct alloc_t a = { .ptr = ptr, @@ -94,7 +94,7 @@ void *al_calloc(size_t n, size_t size) void *al_realloc(void *ptr, size_t n) { void *nptr = _al_realloc(ptr, n); -#if MEMORY_TRACKING +#if AL_MEMORY_TRACKING printf("**al_realloc(%p, %zu)\n", ptr, n); for (u32 i = 0; i < allocations.size; i++) { struct alloc_t *a = &al_array_at(allocations, i); @@ -125,7 +125,7 @@ void *al_realloc(void *ptr, size_t n) void al_free(void *ptr) { -#if MEMORY_TRACKING +#if AL_MEMORY_TRACKING printf("**al_free(%p)\n", ptr); for (u32 i = 0; i < allocations.size; i++) { struct alloc_t *a = &al_array_at(allocations, i); @@ -139,11 +139,11 @@ void al_free(void *ptr) _al_free(ptr); } -#ifdef AL_HAVE_POSIX_MEMALIGN +#if defined AL_HAVE_POSIX_MEMALIGN s32 al_posix_memalign(void **ptr, size_t alignment, size_t n) { s32 res = _al_posix_memalign(ptr, alignment, n); -#if MEMORY_TRACKING +#if AL_MEMORY_TRACKING size_t actual_size = (n + alignment) - ((n + alignment) % alignment); printf("**al_posix_memalign(%zu, %zu(%zu))\n", alignment, n, actual_size); struct alloc_t a = { @@ -163,7 +163,7 @@ s32 al_posix_memalign(void **ptr, size_t alignment, size_t n) void al_malloc_stats(u32 *current, u32 *peak, u32 *total) { -#if MEMORY_TRACKING +#if AL_MEMORY_TRACKING *current = currently_allocated; *peak = peak_allocated; *total = total_allocated; diff --git a/src/log.c b/src/log.c index 4ae2184..3466320 100644 --- a/src/log.c +++ b/src/log.c @@ -1,15 +1,16 @@ #include "../include/al/log.h" -#define AL_LOG_SKIP -#define AL_LOG_MESSAGE_SIZE 511 -#define AL_LOG_USE_SECTION -#ifdef AL_LOG_USE_SECTION +#define AL_LOG_SKIP 0 +#define AL_LOG_USE_SECTION 1 +#if AL_LOG_USE_SECTION #define AL_LOG_TEMPLATE "%s:%d %s(): %s -> (%s) " #else #define AL_LOG_TEMPLATE "%s:%d %s(): %s -> " #endif -#ifndef AL_LOG_SKIP +#define AL_LOG_MESSAGE_SIZE 511 + +#if !AL_LOG_SKIP static s32 al_print_default(void *userdata, char *s) { (void)userdata; @@ -30,7 +31,7 @@ void al_set_print(s32 (*print_func)(void *, char *), void *userdata) static char *get_buffer(const char *level, const char *section, const char *fmt, const char *name, const s32 line, const char *func, va_list args) { char *buffer = al_calloc(1, AL_LOG_MESSAGE_SIZE + 1); -#ifdef AL_LOG_USE_SECTION +#if AL_LOG_USE_SECTION s32 prefix = snprintf(buffer, AL_LOG_MESSAGE_SIZE, AL_LOG_TEMPLATE, name, line, func, level, section); #else (void)section; @@ -47,12 +48,18 @@ void al_set_print(s32 (*print_func)(void *, char *), void *userdata) { (void)pri s32 _al_log(const char *level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, ...) { #if AL_FORCE_DISABLE_OUTPUT + (void)level; + (void)section; + (void)name; + (void)line; + (void)func; + (void)fmt; return 0; #else va_list args; va_start(args, fmt); if (!section) section = ""; -#ifdef AL_LOG_SKIP +#if AL_LOG_SKIP s32 ret = al_printf(AL_LOG_TEMPLATE, name, line, func, level, section); ret += al_vprintf(fmt, args); if (strcspn(fmt, "\r\n") == al_strlen(fmt)) ret += al_printf("\n"); @@ -60,7 +67,7 @@ s32 _al_log(const char *level, const char *section, const char *name, const s32 char *buffer = get_buffer(level, section, fmt, name, line, func, args); #endif va_end(args); -#ifdef AL_LOG_SKIP +#if AL_LOG_SKIP return ret; #else return _al_print(_al_log_userdata, buffer); @@ -71,17 +78,24 @@ s32 _al_log(const char *level, const char *section, const char *name, const s32 s32 _al_logv(const char *level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, va_list args) { #if AL_FORCE_DISABLE_OUTPUT + (void)level; + (void)section; + (void)name; + (void)line; + (void)func; + (void)fmt; + (void)args; return 0; #else if (!section) section = ""; -#ifdef AL_LOG_SKIP +#if AL_LOG_SKIP s32 ret = al_printf(AL_LOG_TEMPLATE, name, line, func, level, section); ret += al_vprintf(fmt, args); if (strcspn(fmt, "\r\n") == al_strlen(fmt)) ret += al_printf("\n"); #else char *buffer = get_buffer(level, section, fmt, name, line, func, args); #endif -#ifdef AL_LOG_SKIP +#if AL_LOG_SKIP return ret; #else return _al_print(_al_log_userdata, buffer); diff --git a/tests/main.c b/tests/main.c index 1c8afe2..e263dd5 100644 --- a/tests/main.c +++ b/tests/main.c @@ -2,7 +2,7 @@ #include "lib.h" #include "array.h" -#ifdef AL_HAVE_GNU_EXTENSIONS +#if defined AL_HAVE_GNU_EXTENSIONS #include "array_typed.h" #endif #include "str.h" @@ -12,7 +12,7 @@ s32 main(void) { if (!lib_tests_run()) goto fail; if (!array_tests_run()) goto fail; -#ifdef AL_HAVE_GNU_EXTENSIONS +#if defined AL_HAVE_GNU_EXTENSIONS if (!array_typed_tests_run()) goto fail; #endif if (!str_tests_run()) goto fail; -- cgit v1.2.3-101-g0448