diff options
| -rw-r--r-- | include/al/array_sort.h | 2 | ||||
| -rw-r--r-- | include/al/array_typed.h | 2 | ||||
| -rw-r--r-- | include/al/lib.h | 25 | ||||
| -rw-r--r-- | include/al/log.h | 2 | ||||
| -rw-r--r-- | include/al/types.h | 6 | ||||
| -rw-r--r-- | include/al/wstr.h | 4 | ||||
| -rwxr-xr-x | scripts/line_count.sh | 2 | ||||
| -rw-r--r-- | src/array.c | 6 | ||||
| -rw-r--r-- | src/lib.c | 26 | ||||
| -rw-r--r-- | src/log.c | 24 | ||||
| -rw-r--r-- | tests/main.c | 4 |
11 files changed, 50 insertions, 53 deletions
diff --git a/include/al/array_sort.h b/include/al/array_sort.h index 8d138d0..18b00f5 100644 --- a/include/al/array_sort.h +++ b/include/al/array_sort.h @@ -18,7 +18,7 @@ } \ } while (0) -#if AL_USE_STDLIB +#ifdef AL_USE_STDLIB #define AL_STDLIB_QSORT(data, type, count, cmp) qsort(data, count, sizeof(type), cmp) #endif diff --git a/include/al/array_typed.h b/include/al/array_typed.h index 09289f2..a48b230 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 -#if !defined AL_HAVE_GNU_EXTENSIONS +#ifndef AL_HAVE_GNU_EXTENSIONS #error "missing required GNU C extensions" #endif diff --git a/include/al/lib.h b/include/al/lib.h index 6e0b1a9..a8f2b9f 100644 --- a/include/al/lib.h +++ b/include/al/lib.h @@ -3,9 +3,8 @@ #include "types.h" -#define AL_USE_STDLIB 1 - -#define AL_FORCE_DISABLE_OUTPUT 0 +#define AL_USE_STDLIB +//#define AL_FORCE_DISABLE_OUTPUT #if defined __clang__ || defined __GNUC__ #define AL_UNUSED_FUNCTION_PUSH \ @@ -18,7 +17,7 @@ #define AL_UNUSED_FUNCTION_POP #endif -#if AL_USE_STDLIB +#ifdef AL_USE_STDLIB #include <stdlib.h> #include <string.h> #include <stdio.h> @@ -34,7 +33,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); -#if defined AL_HAVE_POSIX_MEMALIGN +#ifdef 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); @@ -50,7 +49,7 @@ void al_set_alloc(void *(*malloc_func)(size_t), void *(*calloc_func)(size_t, siz #define al_strlen strlen #define al_snprintf snprintf #define al_vsnprintf vsnprintf -#if defined _DEBUG_ +#ifdef _DEBUG_ #define al_assert assert #else #define al_assert(expr) do { (void)sizeof(expr); } while (0) // NOLINT(bugprone-sizeof-expression) @@ -67,7 +66,7 @@ AL_UNUSED_FUNCTION_PUSH static inline s32 al_printf(const char *fmt, ...) { -#if AL_FORCE_DISABLE_OUTPUT +#ifdef AL_FORCE_DISABLE_OUTPUT (void)fmt; return 0; #else @@ -81,7 +80,7 @@ static inline s32 al_printf(const char *fmt, ...) static inline s32 al_vprintf(const char *fmt, va_list args) { -#if AL_FORCE_DISABLE_OUTPUT +#ifdef AL_FORCE_DISABLE_OUTPUT (void)fmt; (void)args; return 0; @@ -105,16 +104,16 @@ static inline void *al_memrchr(const void *s, s32 c, size_t n) AL_UNUSED_FUNCTION_POP #endif -#if !defined LIKELY -#if defined AL_HAVE_BUILTIN_EXPECT +#ifndef LIKELY +#ifdef AL_HAVE_BUILTIN_EXPECT #define LIKELY(x) __builtin_expect((x), 1) #else #define LIKELY(x) x #endif #endif -#if !defined UNLIKELY -#if defined AL_HAVE_BUILTIN_EXPECT +#ifndef UNLIKELY +#ifdef AL_HAVE_BUILTIN_EXPECT #define UNLIKELY(x) __builtin_expect((x), 0) #else #define UNLIKELY(x) x @@ -150,7 +149,7 @@ AL_UNUSED_FUNCTION_POP #define AL_BOOLSTR(b) ((b) ? "true" : "false") -#if !defined _MSVC_ +#ifndef _MSVC_ #define __al_thread __thread #else #define __al_thread __declspec(thread) diff --git a/include/al/log.h b/include/al/log.h index 87b25c2..9f5dc91 100644 --- a/include/al/log.h +++ b/include/al/log.h @@ -18,7 +18,7 @@ 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__) -#if defined _DEBUG_ +#ifdef _DEBUG_ #define al_log_debug(section, fmt, ...) al_log("debug", section, fmt, ##__VA_ARGS__) #else AL_UNUSED_FUNCTION_PUSH diff --git a/include/al/types.h b/include/al/types.h index 45a8a54..cf3d0e8 100644 --- a/include/al/types.h +++ b/include/al/types.h @@ -1,7 +1,7 @@ #ifndef _AL_TYPES_H #define _AL_TYPES_H -#if defined _POSIX_C_SOURCE +#ifdef _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 -#if defined _XOPEN_SOURCE +#ifdef _XOPEN_SOURCE #if _XOPEN_SOURCE < 500 #error "libalabaster requires _XOPEN_SOURCE >= 500" #endif @@ -39,7 +39,7 @@ typedef double f64; // Use `long` and `ulong`, if needed, for API compatibility. typedef unsigned long ulong; -#if defined _MSC_VER +#ifdef _MSC_VER #include <BaseTsd.h> typedef SSIZE_T ssize_t; #endif diff --git a/include/al/wstr.h b/include/al/wstr.h index 1202882..8113575 100644 --- a/include/al/wstr.h +++ b/include/al/wstr.h @@ -31,7 +31,7 @@ AL_UNUSED_FUNCTION_PUSH static inline s32 al_wchar_width(wchar_t wc) { -#if defined AL_HAVE_WIDE_STRING +#ifdef 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) { -#if defined AL_HAVE_WIDE_STRING +#ifdef AL_HAVE_WIDE_STRING return wcswidth(w->data, w->len); #else (void)w; diff --git a/scripts/line_count.sh b/scripts/line_count.sh index aadb675..9b46c9e 100755 --- a/scripts/line_count.sh +++ b/scripts/line_count.sh @@ -1,2 +1,2 @@ #! /usr/bin/env sh -find ./src ./include -type f | xargs wc -l +find ./src ./include -type f -exec wc -l {} + diff --git a/src/array.c b/src/array.c index 2b06d23..9920267 100644 --- a/src/array.c +++ b/src/array.c @@ -1,7 +1,3 @@ -#if !defined AL_ARRAY_MALLOC || !defined AL_ARRAY_REALLOC || !defined AL_ARRAY_FREE -#error "define all of AL_ARRAY_MALLOC, AL_ARRAY_REALLOC, and AL_ARRAY_FREE before including array.c" -#endif - #include "../include/al/lib.h" #include "../include/al/array_sort.h" @@ -83,7 +79,7 @@ (arr).size -= e - i; \ } while (0) -#if AL_USE_STDLIB +#ifdef AL_USE_STDLIB #define al_array_sort(arr, type, cmp) AL_STDLIB_QSORT((arr).data, type, (arr).size, cmp) #else #define al_array_sort(arr, type, cmp) AL_INSERSION_SORT((arr).data, type, (arr).size, cmp) @@ -7,18 +7,18 @@ AL_ASSERT_TYPE_SIZE(int, 4); AL_ASSERT_TYPE_SIZE(unsigned, 4); -#if AL_USE_STDLIB +#ifdef AL_USE_STDLIB 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; -#if defined AL_HAVE_POSIX_MEMALIGN +#ifdef 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 AL_MEMORY_TRACKING 0 -#if AL_MEMORY_TRACKING +//#define AL_MEMORY_TRACKING +#ifdef 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 AL_MEMORY_TRACKING +#ifdef AL_MEMORY_TRACKING al_array_init(allocations); #endif } void al_malloc_close(void) { -#if AL_MEMORY_TRACKING +#ifdef 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 AL_MEMORY_TRACKING +#ifdef 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 AL_MEMORY_TRACKING +#ifdef 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 AL_MEMORY_TRACKING +#ifdef 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 AL_MEMORY_TRACKING +#ifdef 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); } -#if defined AL_HAVE_POSIX_MEMALIGN +#ifdef 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 AL_MEMORY_TRACKING +#ifdef 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 AL_MEMORY_TRACKING +#ifdef AL_MEMORY_TRACKING *current = currently_allocated; *peak = peak_allocated; *total = total_allocated; @@ -1,8 +1,8 @@ #include "../include/al/log.h" -#define AL_LOG_SKIP 0 -#define AL_LOG_USE_SECTION 1 -#if AL_LOG_USE_SECTION +#define AL_LOG_SKIP +#define AL_LOG_USE_SECTION +#ifdef AL_LOG_USE_SECTION #define AL_LOG_TEMPLATE "%s:%d %s(): %s -> (%s) " #else #define AL_LOG_TEMPLATE "%s:%d %s(): %s -> " @@ -10,7 +10,7 @@ #define AL_LOG_MESSAGE_MAX 511 -#if !AL_LOG_SKIP +#ifndef AL_LOG_SKIP static s32 al_print_default(void *userdata, char *s) { (void)userdata; @@ -28,10 +28,11 @@ void al_set_print(s32 (*print_func)(void *, char *), void *userdata) _al_log_userdata = userdata; } +#ifndef AL_FORCE_DISABLE_OUTPUT 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_malloc(AL_LOG_MESSAGE_MAX + 1); -#if AL_LOG_USE_SECTION +#ifdef AL_LOG_USE_SECTION s32 prefix = al_snprintf(buffer, AL_LOG_MESSAGE_MAX, AL_LOG_TEMPLATE, name, line, func, level, section); #else (void)section; @@ -41,13 +42,14 @@ static char *get_buffer(const char *level, const char *section, const char *fmt, buffer[strcspn(buffer, "\r\n")] = '\0'; return buffer; } +#endif #else void al_set_print(s32 (*print_func)(void *, char *), void *userdata) { (void)print_func; (void)userdata; } #endif 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 +#ifdef AL_FORCE_DISABLE_OUTPUT (void)level; (void)section; (void)name; @@ -59,7 +61,7 @@ s32 _al_log(const char *level, const char *section, const char *name, const s32 va_list args; va_start(args, fmt); if (!section) section = ""; -#if AL_LOG_SKIP +#ifdef 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"); @@ -67,7 +69,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); -#if AL_LOG_SKIP +#ifdef AL_LOG_SKIP return ret; #else return _al_print(_al_log_userdata, buffer); @@ -77,7 +79,7 @@ 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 +#ifdef AL_FORCE_DISABLE_OUTPUT (void)level; (void)section; (void)name; @@ -88,14 +90,14 @@ s32 _al_logv(const char *level, const char *section, const char *name, const s32 return 0; #else if (!section) section = ""; -#if AL_LOG_SKIP +#ifdef 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 -#if AL_LOG_SKIP +#ifdef AL_LOG_SKIP return ret; #else return _al_print(_al_log_userdata, buffer); diff --git a/tests/main.c b/tests/main.c index 2c13ce9..5964a04 100644 --- a/tests/main.c +++ b/tests/main.c @@ -3,7 +3,7 @@ #include "lib.h" #include "array.h" -#if defined AL_HAVE_GNU_EXTENSIONS +#ifdef AL_HAVE_GNU_EXTENSIONS #include "array_typed.h" #endif #include "str.h" @@ -15,7 +15,7 @@ s32 main(void) if (!lib_tests_run()) goto fail; if (!array_tests_run()) goto fail; -#if defined AL_HAVE_GNU_EXTENSIONS +#ifdef AL_HAVE_GNU_EXTENSIONS if (!array_typed_tests_run()) goto fail; #endif if (!str_tests_run()) goto fail; |