diff options
| author | 2025-03-31 17:47:38 -0400 | |
|---|---|---|
| committer | 2025-03-31 17:47:38 -0400 | |
| commit | f6a8686b2c209b0e25cd64a587d6e9fb55200ec4 (patch) | |
| tree | 5e681d42484d5433e49f21c21229fe7df29299ab /include/al | |
| parent | cf45d71c49a7e7364b315dcc45fe7eac060590df (diff) | |
| download | libalabaster-f6a8686b2c209b0e25cd64a587d6e9fb55200ec4.tar.gz libalabaster-f6a8686b2c209b0e25cd64a587d6e9fb55200ec4.tar.bz2 libalabaster-f6a8686b2c209b0e25cd64a587d6e9fb55200ec4.zip | |
all: Revert and add some style changes
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'include/al')
| -rw-r--r-- | include/al/array_sort.h | 31 | ||||
| -rw-r--r-- | include/al/array_typed.h | 4 | ||||
| -rw-r--r-- | include/al/lib.h | 75 | ||||
| -rw-r--r-- | include/al/list.h | 4 | ||||
| -rw-r--r-- | include/al/log.h | 4 | ||||
| -rw-r--r-- | include/al/macros.h | 49 | ||||
| -rw-r--r-- | include/al/random.h | 10 | ||||
| -rw-r--r-- | include/al/str.h | 21 | ||||
| -rw-r--r-- | include/al/test.h | 44 | ||||
| -rw-r--r-- | include/al/wstr.h | 19 |
10 files changed, 119 insertions, 142 deletions
diff --git a/include/al/array_sort.h b/include/al/array_sort.h index f10766f..a4c881e 100644 --- a/include/al/array_sort.h +++ b/include/al/array_sort.h @@ -17,22 +17,21 @@ while i < length(A) end while */ -#define AL_INSERSION_SORT(data, type, count, cmp) \ -AL_MACRO_WRAP \ -{ \ - u32 i = 1, k; \ - type x; \ - while (i < count) { \ - k = i; \ - x = (data)[i]; \ - while (k > 0 && cmp(&(data)[k - 1], &x) > 0) { \ - (data)[k] = (data)[k - 1]; \ - k--; \ - } \ - (data)[k] = x; \ - i++; \ - } \ -} AL_MACRO_END +#define AL_INSERSION_SORT(data, type, count, cmp) \ + do { \ + u32 i = 1, k; \ + type x; \ + while (i < count) { \ + k = i; \ + x = (data)[i]; \ + while (k > 0 && cmp(&(data)[k - 1], &x) > 0) { \ + (data)[k] = (data)[k - 1]; \ + k--; \ + } \ + (data)[k] = x; \ + i++; \ + } \ + } while (0) #ifdef AL_USE_STDLIB #define AL_STDLIB_QSORT(data, type, count, cmp) qsort(data, (size_t)count, sizeof(type), cmp) diff --git a/include/al/array_typed.h b/include/al/array_typed.h index e7a4f80..2092160 100644 --- a/include/al/array_typed.h +++ b/include/al/array_typed.h @@ -47,7 +47,7 @@ { \ return al_array_at(N, __VA_ARGS__)(array, array->count - 1); \ } \ - static void al_array_reserve(N, __VA_ARGS__)(array(N, __VA_ARGS__) *array, u32 count); \ + static inline void al_array_reserve(N, __VA_ARGS__)(array(N, __VA_ARGS__) *array, u32 count); \ static inline void al_array_push(N, __VA_ARGS__)(array(N, __VA_ARGS__) *array, T value) \ { \ al_array_reserve(N, __VA_ARGS__)(array, array->count + 1); \ @@ -84,7 +84,7 @@ } \ static inline void _al_array_reserve_internal(N, __VA_ARGS__)(array(N, __VA_ARGS__) *array, u32 size) \ { \ - array->alloc = al_default_growing_allocation((void **)&array->data, array->alloc, size, al_malloc, al_realloc); \ + array->alloc = al_growing_allocation((void **)&array->data, array->alloc, size, al_malloc, al_realloc); \ } \ static inline void al_array_free(N, __VA_ARGS__)(array(N, __VA_ARGS__) *array) \ { \ diff --git a/include/al/lib.h b/include/al/lib.h index ac47896..cb5101a 100644 --- a/include/al/lib.h +++ b/include/al/lib.h @@ -7,26 +7,33 @@ // Piggyback off c89atomic for pointer size macros. #include <c89atomic.h> +#if defined C89ATOMIC_64BIT +#define AL_WE_64BIT +#elif defined C89ATOMIC_32BIT +#define AL_WE_32BIT +#endif + +// https://forum.vcfed.org/index.php?threads/c-item-size-check-at-compile-time.1244920/ +#define AL_ASSERT_TYPE_SIZE(type, size) \ + typedef char type##__size_test[(!!(sizeof(type) == size)) * 2 - 1] + #define AL_USE_STDLIB //#define AL_MEMORY_TRACKING //#define AL_FORCE_DISABLE_OUTPUT -#if defined __GNUC__ || defined __clang__ -#define AL_UNUSED_FUNCTION_PUSH \ - _Pragma("GCC diagnostic push") \ - _Pragma("GCC diagnostic ignored \"-Wunused-function\"") -#define AL_UNUSED_FUNCTION_POP \ - _Pragma("GCC diagnostic pop") -#define AL_UNUSED_VARIABLE_PUSH \ - _Pragma("GCC diagnostic push") \ - _Pragma("GCC diagnostic ignored \"-Wunused-variable\"") -#define AL_UNUSED_VARIABLE_POP \ - _Pragma("GCC diagnostic pop") +#if defined __clang__ +#define AL_IGNORE_WARNING(warning) \ + _Pragma(XSTR(clang diagnostic push)) \ + _Pragma(XSTR(clang diagnostic ignored warning)) +#define AL_IGNORE_WARNING_END _Pragma(XSTR(clang diagnostic pop)) +#elif defined __GNUC__ +#define AL_IGNORE_WARNING(warning) \ + _Pragma(XSTR(GCC diagnostic push)) \ + _Pragma(XSTR(GCC diagnostic ignored warning)) +#define AL_IGNORE_WARNING_END _Pragma(XSTR(GCC diagnostic pop)) #else -#define AL_UNUSED_FUNCTION_PUSH -#define AL_UNUSED_FUNCTION_POP -#define AL_UNUSED_VARIABLE_PUSH -#define AL_UNUSED_VARIABLE_POP +#define AL_IGNORE_WARNING(warning) +#define AL_IGNORE_WARNING_END #endif #ifdef AL_USE_STDLIB @@ -86,17 +93,13 @@ void al_malloc_stats(size_t *current, size_t *peak, size_t *total); #define al_assert(expr) do { (void)sizeof(expr); } while (0) // NOLINT(bugprone-sizeof-expression) #endif -#define al_assert_and_return(...) \ -AL_MACRO_WRAP \ -{ \ +#define al_assert_and_return(...) do { \ al_assert(false); \ return __VA_ARGS__; \ -} AL_MACRO_END +} while (0) #define al_alloc_object(type) (type *)al_calloc(1, sizeof(type)) -AL_UNUSED_FUNCTION_PUSH - // https://stackoverflow.com/a/66346831 static inline size_t al_strnlen(const char *s, size_t n) { @@ -107,7 +110,7 @@ static inline size_t al_strnlen(const char *s, size_t n) static inline char *al_strndup(const char *s, size_t n) { size_t len = al_strnlen(s, n); - char *ret = al_malloc(len + 1); + char *ret = (char *)al_malloc(len + 1); al_memcpy(ret, s, len); ret[len] = '\0'; return ret; @@ -160,11 +163,8 @@ static inline char *al_memdup0(const char *s, size_t n) return buf; } -AL_UNUSED_FUNCTION_POP #endif -AL_UNUSED_FUNCTION_PUSH - static inline u32 al_next_power_of_two(u32 n) { n--; @@ -211,38 +211,35 @@ static inline u32 al_u32_dec_wrap(u32 v) return (v == 0) ? UINT32_MAX : v - 1; } -static inline u16 al_u16_inc_wrap(u16 v) +static inline u32 al_u32_add_wrap(u32 v, u32 add, u32 max) { - return (v == UINT16_MAX) ? 0 : v + 1; + return (v > max - add) ? (add - (max - v)) : v + add; } -static inline u32 al_add_wrap(u32 v, u32 add, u32 max) +static inline u16 al_u16_inc_wrap(u16 v) { - return (v > max - add) ? (add - (max - v)) : v + add; + return (v == UINT16_MAX) ? 0 : v + 1; } -static inline u32 al_u32_add_wrap(u32 v, u32 add) +static inline u16 al_u16_dec_wrap(u16 v) { - return (v > UINT32_MAX - add) ? (add - (UINT32_MAX - v)) : v + add; + return (v == 0) ? UINT16_MAX : v - 1; } -static inline u16 al_u16_add_wrap(u16 v, u16 add) +static inline u16 al_u16_add_wrap(u16 v, u16 add, u16 max) { - return (v > UINT16_MAX - add) ? (u16)(add - (UINT16_MAX - v)) : v + add; + return (v > max - add) ? (u16)(add - (max - v)) : v + add; } -AL_UNUSED_FUNCTION_POP - -#if defined C89ATOMIC_64BIT +#if defined AL_WE_64BIT #define MAX_ALLOC_32 UINT32_MAX -#elif defined C89ATOMIC_32BIT +#elif defined AL_WE_32BIT #define MAX_ALLOC_32 ((INT32_MAX - 1) / 4) #endif // Once a growing allocation expands past this number, only grow in multiples of it. extern u32 al_grow_step; - -u32 al_default_growing_allocation(void **ptr, u32 prev_size, u32 size, +u32 al_growing_allocation(void **ptr, u32 prev_size, u32 size, void *(*malloc_func)(size_t), void *(*realloc_func)(void *, size_t)); // Defaults to 4KB. diff --git a/include/al/list.h b/include/al/list.h index 333d2a4..6f33b93 100644 --- a/include/al/list.h +++ b/include/al/list.h @@ -4,7 +4,6 @@ #include "lib.h" #define AL_LIST_DEFINE(type, name) \ - AL_UNUSED_FUNCTION_PUSH \ typedef struct list_##name list_##name; \ struct list_##name { \ type value; \ @@ -109,8 +108,7 @@ list_free_node_##name(n); \ } \ return l; \ - } \ - AL_UNUSED_FUNCTION_POP + } #define list(name) list_##name diff --git a/include/al/log.h b/include/al/log.h index da3f21d..2fb0c26 100644 --- a/include/al/log.h +++ b/include/al/log.h @@ -59,13 +59,9 @@ s32 _al_logv(u8 level, const char *section, const char *name, const s32 line, co #define al_log_trace(section, fmt, ...) al_log_nop(section, fmt, ##__VA_ARGS__) #endif -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 - #endif // _AL_LOG_H diff --git a/include/al/macros.h b/include/al/macros.h index 715718a..3932c37 100644 --- a/include/al/macros.h +++ b/include/al/macros.h @@ -1,9 +1,6 @@ #ifndef _AL_MACROS_H #define _AL_MACROS_H -#define AL_MACRO_WRAP do { -#define AL_MACRO_END } while(0) - #define AL_PASTER_EVALUATOR(X, Y) X##_##Y #define AL_PASTER(X, Y) AL_PASTER_EVALUATOR(X, Y) @@ -23,47 +20,49 @@ #define UNLIKELY(x) x #endif -#if defined MAX || defined MIN || defined CLAMP || defined SWAP || defined ARRAY_SIZE || defined BOOLSTR +#if defined MIN || defined MAX || defined CLAMP || defined SWAP || defined ARRAY_SIZE || defined STR || defined XSTR || defined BOOLSTR #error "Conflicting definitions for common macros" #endif #ifdef AL_HAVE_GNU_EXTENSIONS -#define MIN(a, b) \ -({ \ - __typeof__(a) _a = a; \ - __typeof__(b) _b = b; \ - (void)(&_a == &_b); \ - _a < _b ? _a : _b; \ -}) -#define MAX(a, b) \ -({ \ - __typeof__(a) _a = a; \ - __typeof__(b) _b = b; \ - (void)(&_a == &_b); \ - _a > _b ? _a : _b; \ -}) +#define MIN(a, b) \ + ({ \ + __typeof__(a) ac = a; \ + __typeof__(b) bc = b; \ + (void)(&ac == &bc); \ + (ac < bc) ? ac : bc; \ + }) +#define MAX(a, b) \ + ({ \ + __typeof__(a) ac = a; \ + __typeof__(b) bc = b; \ + (void)(&ac == &bc); \ + (ac > bc) ? ac : bc; \ + }) #define CLAMP(n, lo, hi) MIN(hi, MAX(lo, n)) #else -// Compatible/Pedantic min/max. /* #error "MIN/MAX/CLAMP should not be used in this mode as they\ differ in behavior without support for statement expressions" */ +// Compatible/Pedantic min/max. #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MAX(a,b) ((a) > (b) ? (a) : (b)) #define CLAMP(n, lo, hi) MIN(hi, MAX(lo, n)) #endif #define SWAP(a, b) \ -AL_MACRO_WRAP \ -{ \ - __typeof__(a) tmp = a; \ - a = b; \ - b = tmp; \ -} AL_MACRO_END + do { \ + __typeof__(a) tmp = a; \ + a = b; \ + b = tmp; \ + } while (0) #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define STR(s) #s +#define XSTR(s) STR(s) + #define BOOLSTR(b) ((b) ? "true" : "false") #if defined KB || defined MB || defined GB || defined TB diff --git a/include/al/random.h b/include/al/random.h index 3d0d7cb..f92a77b 100644 --- a/include/al/random.h +++ b/include/al/random.h @@ -6,26 +6,22 @@ #define AL_RAND_MAX RAND_MAX -AL_UNUSED_FUNCTION_PUSH - extern u32 al_rand_seed; extern __thread bool al_rand_set; -static void al_rand_init(u32 seed) +static inline void al_rand_init(u32 seed) { al_rand_seed = seed; } -static s32 al_rand(void) +static inline s32 al_rand(void) { if (UNLIKELY(!al_rand_set)) { - // srand() is thread-local on Windows CRT. + // srand() is thread-local in Windows CRT. srand(al_rand_seed); al_rand_set = true; } return rand(); } -AL_UNUSED_FUNCTION_POP - #endif // _AL_RANDOM_H diff --git a/include/al/str.h b/include/al/str.h index fce2486..d7dab1c 100644 --- a/include/al/str.h +++ b/include/al/str.h @@ -10,9 +10,10 @@ typedef struct { char *__sized_by(alloc) data; } str; +#define AL_STR_NPOS ((u32)-1) + #define al_str_null() ((str){ 0, 0, NULL }) -#define al_str_npos ((u32)-1) #define al_str_at(s, i) (s)->data[i] #define al_str_atr(s, i) (s)->data[(s)->length - (i)] @@ -21,13 +22,11 @@ typedef struct { #define al_str_cs(c) ((str){ (u32)al_strnlen(c, sizeof(c)), 0, ((char *)(c)) }) // (s)ized #define al_str_cr(c) ((str){ (u32)al_strnlen(c, MAX_ALLOC_32), 0, ((char *)(c)) }) // (r)untime -#define al_str_w(c, i, n) ((str){ ((u32)(n)), 0, &(c)[i] }) +#define al_str_w(c, i, n) ((str){ ((u32)(n)), 0, (c) + (i) }) #define al_str_substr(s, start, end) al_str_w((s)->data, start, ((end) - (start))) -#define al_str_fmt(s) (s)->length, (s)->data -#define al_str_fmt_maxlen(s, l) MIN((s)->length, (l)), (s)->data - -AL_UNUSED_FUNCTION_PUSH +#define al_str_x(s) (s)->length, (s)->data // e(x)pand +#define al_str_x_maxlen(s, l) MIN((s)->length, (l)), (s)->data static inline bool al_str_is_empty(str *s) { @@ -41,7 +40,7 @@ static inline void al_str_free(str *s) static inline u32 al_str_reserve(str *s, u32 size) { - return al_default_growing_allocation((void **)&s->data, s->alloc, size, al_malloc, al_realloc); + return al_growing_allocation((void **)&s->data, s->alloc, size, al_malloc, al_realloc); } static inline void al_str_sized(str *s, u32 size) @@ -119,13 +118,13 @@ static inline bool al_str_eq(str *s, str *c) static inline u32 al_str_find(str *s, char c) { char *loc = (char *)al_memchr(s->data, c, s->length); - return loc ? (u32)(loc - s->data) : al_str_npos; + return loc ? (u32)(loc - s->data) : AL_STR_NPOS; } static inline u32 al_str_rfind(str *s, char c) { char *loc = (char *)al_memrchr(s->data, c, s->length); - return loc ? (u32)(loc - s->data) : al_str_npos; + return loc ? (u32)(loc - s->data) : AL_STR_NPOS; } // extremely slow. @@ -139,7 +138,7 @@ static inline u32 al_str_find_str(str *s, str *c) } } } - return al_str_npos; + return AL_STR_NPOS; } static inline u32 al_str_hash(str *s) @@ -152,8 +151,6 @@ static inline u32 al_str_hash(str *s) return hash; } -AL_UNUSED_FUNCTION_POP - s64 al_str_to_long(str *s, u32 base, bool *error); bool al_str_get_line(str *buffer, char sep, str *line); diff --git a/include/al/test.h b/include/al/test.h index 8fc37e3..bed3102 100644 --- a/include/al/test.h +++ b/include/al/test.h @@ -14,58 +14,56 @@ if (!test()) return false; #define AL_TEST_START(name) \ -AL_MACRO_WRAP \ -{ \ - al_printf(" "name"..."); \ - al_fflush(stdout); \ -} AL_MACRO_END + do { \ + al_printf(" "name"..."); \ + al_fflush(stdout); \ + } while (0) #define AL_TEST_END() \ al_printf("OK\n"); \ return true #define AL_TEST_DEF(a, b, T, cmp, explain) \ -AL_MACRO_WRAP \ -{ \ - AL_TEST_TYPE_##T _a = a; \ - AL_TEST_TYPE_##T _b = b; \ - if (cmp) { \ - al_printf("FAIL "#a"("AL_TEST_FMT_##T") "explain" "#b"("AL_TEST_FMT_##T")\n", \ - AL_TEST_PRINTF_##T(_a), AL_TEST_PRINTF_##T(_b)); \ - return false; \ - } \ -} AL_MACRO_END + do { \ + AL_TEST_TYPE_##T ac = a; \ + AL_TEST_TYPE_##T bc = b; \ + if (cmp) { \ + al_printf("FAIL "#a"("AL_TEST_FMT_##T") "explain" "#b"("AL_TEST_FMT_##T")\n", \ + AL_TEST_PRINTF_##T(ac), AL_TEST_PRINTF_##T(bc)); \ + return false; \ + } \ + } while (0) #define AL_TEST_EQ(a, b, T) \ - AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(_a, _b) != 0), \ + AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(ac, bc) != 0), \ "is not equal to") #define AL_TEST_NEQ(a, b, T) \ - AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(_a, _b) == 0), \ + AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(ac, bc) == 0), \ "is equal to") #define AL_TEST_GT(a, b, T) \ - AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(_a, _b) != 1), \ + AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(ac, bc) != 1), \ "is less than or equal to") #define AL_TEST_GTE(a, b, T) \ - AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(_a, _b) != 0 && AL_TEST_CMP_##T(_a, _b) != 1), \ + AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(ac, bc) != 0 && AL_TEST_CMP_##T(ac, bc) != 1), \ "is less than") #define AL_TEST_LT(a, b, T) \ - AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(_a, _b) != -1), \ + AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(ac, bc) != -1), \ "is greater than or equal to") #define AL_TEST_LTE(a, b, T) \ - AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(_a, _b) != 0 && AL_TEST_CMP_##T(_a, _b) != -1), \ + AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(ac, bc) != 0 && AL_TEST_CMP_##T(ac, bc) != -1), \ "is greater than") #define AL_TEST_TRUE(a) \ - AL_TEST_DEF(a, true, bool, (AL_TEST_CMP_bool(_a, _b) != 0), \ + AL_TEST_DEF(a, true, bool, (AL_TEST_CMP_bool(ac, bc) != 0), \ "is not") #define AL_TEST_FALSE(a) \ - AL_TEST_DEF(a, false, bool, (AL_TEST_CMP_bool(_a, _b) != 0), \ + AL_TEST_DEF(a, false, bool, (AL_TEST_CMP_bool(ac, bc) != 0), \ "is not") // bool diff --git a/include/al/wstr.h b/include/al/wstr.h index 3eb366c..5d5f6ac 100644 --- a/include/al/wstr.h +++ b/include/al/wstr.h @@ -12,9 +12,10 @@ typedef struct { wchar_t *__sized_by(alloc) data; } wstr; +#define AL_WSTR_NPOS ((u32)-1) + #define al_wstr_null() ((wstr){ 0, 0, NULL }) -#define al_wstr_npos ((u32)-1) #define al_wstr_at(w, i) (w)->data[i] #define al_wstr_atr(w, i) (w)->data[(w)->length - (i)] @@ -23,12 +24,10 @@ typedef struct { #define al_wstr_cs(wc) ((wstr){ (u32)wcsnlen(wc, sizeof(wc)), 0, ((wchar_t *)(wc)) }) // (s)ized #define al_wstr_cr(wc) ((wstr){ (u32)wcsnlen(wc, MAX_ALLOC_32), 0, ((wchar_t *)(wc)) }) // (r)untime -#define al_wstr_w(wc, i, n) ((wstr){ ((u32)(n)), 0, &(wc)[i] }) +#define al_wstr_w(wc, i, n) ((wstr){ ((u32)(n)), 0, (wc) + (i) }) #define al_wstr_substr(w, start, end) al_wstr_w((w)->data, start, ((end) - (start))) -#define al_wstr_fmt(w) (w)->data - -AL_UNUSED_FUNCTION_PUSH +#define al_wstr_x(w) (w)->data // e(x)pand static inline bool al_wstr_is_empty(wstr *w) { @@ -52,7 +51,7 @@ static inline void al_wstr_free(wstr *w) static inline u32 al_wstr_reserve(wstr *w, u32 size) { - return al_default_growing_allocation((void **)&w->data, w->alloc, size, al_malloc, al_realloc); + return al_growing_allocation((void **)&w->data, w->alloc, size, al_malloc, al_realloc); } static inline void al_wstr_sized(wstr *w, u32 size) @@ -188,7 +187,7 @@ static inline u32 al_wstr_find(wstr *w, wchar_t c) return i; } } - return al_wstr_npos; + return AL_WSTR_NPOS; } static inline u32 al_wstr_rfind(wstr *w, wchar_t c) @@ -198,13 +197,13 @@ static inline u32 al_wstr_rfind(wstr *w, wchar_t c) return i; } } - return al_wstr_npos; + return AL_WSTR_NPOS; } static inline bool al_wstr_tok(wstr *result, wchar_t delim) { u32 index = al_wstr_find(result, delim); - if (index == al_str_npos) { + if (index == AL_WSTR_NPOS) { return false; } *result = al_wstr_substr(result, index + 1, result->length); @@ -220,6 +219,4 @@ static inline s64 al_wstr_to_long(wstr *w, u32 base, bool *error) return num; } -AL_UNUSED_FUNCTION_POP - #endif // _AL_WSTR_H |