From 7f7566324b18833d2868ea45e2b28dcdc547d879 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Tue, 9 Apr 2024 11:17:46 -0400 Subject: Update - Separate al_str_c functions between compile-time and runtime - Remove volatile from atomics - Allow not specifying a section in the log - Cleanup Signed-off-by: Andrew Opalach --- include/al/array_typed.h | 2 +- include/al/atomic.h | 19 ++++++++++++------- include/al/lib.h | 6 +++--- include/al/log.h | 8 +++----- include/al/random.h | 21 +++++++++++++-------- include/al/ring_buffer.h | 4 ++-- include/al/str.h | 3 ++- include/al/wstr.h | 3 ++- 8 files changed, 38 insertions(+), 28 deletions(-) (limited to 'include/al') diff --git a/include/al/array_typed.h b/include/al/array_typed.h index 3334026..156dbcb 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 HAVE_GNU_EXTENSIONS +#ifndef AL_HAVE_GNU_EXTENSIONS #error "missing required GNU C extensions" #endif diff --git a/include/al/atomic.h b/include/al/atomic.h index 143b035..44766ff 100644 --- a/include/al/atomic.h +++ b/include/al/atomic.h @@ -15,33 +15,38 @@ #define al_atomic_ptr_load(p, order) c89atomic_load_explicit_ptr(((volatile void **)p), order) #define al_atomic_ptr_store(p, v, order) c89atomic_store_explicit_ptr(((volatile void **)p), (void *)v, order) -#define atomic_size_t volatile c89atomic_uint64 +#define atomic_size_t c89atomic_uint64 #define al_atomic_size_t_load(p, order) ((size_t)c89atomic_load_explicit_64(p, order)) #define al_atomic_size_t_store(p, v, order) c89atomic_store_explicit_64(p, ((size_t)v), order) -#define atomic_s64 volatile c89atomic_int64 +#define atomic_s64 c89atomic_int64 #define al_atomic_s64_load(p, order) ((s64)c89atomic_load_explicit_i64(p, order)) #define al_atomic_s64_store(p, v, order) c89atomic_store_explicit_i64(p, ((s64)v), order) -#define atomic_u64 volatile c89atomic_uint64 +#define atomic_u64 c89atomic_uint64 #define al_atomic_u64_load(p, order) ((u64)c89atomic_load_explicit_64(p, order)) #define al_atomic_u64_store(p, v, order) c89atomic_store_explicit_64(p, ((u64)v), order) #define al_atomic_u64_add(p, v, order) c89atomic_fetch_add_explicit_64(p, ((u64)v), order) #define al_atomic_u64_sub(p, v, order) c89atomic_fetch_sub_explicit_64(p, ((u64)v), order) -#define atomic_f64 volatile f64 +#define atomic_f64 f64 #define al_atomic_f64_load(p, order) ((f64)c89atomic_load_explicit_f64(p, order)) #define al_atomic_f64_store(p, v, order) c89atomic_store_explicit_f64(p, ((f64)v), order) -#define atomic_s32 volatile c89atomic_int32 +#define atomic_s32 c89atomic_int32 #define al_atomic_s32_load(p, order) ((s32)c89atomic_load_explicit_i32(p, order)) #define al_atomic_s32_store(p, v, order) c89atomic_store_explicit_i32(p, ((s32)v), order) -#define atomic_u8 volatile c89atomic_uint8 +#define atomic_u16 c89atomic_uint16 +#define al_atomic_u16_load(p, order) c89atomic_load_explicit_16(p, order) +#define al_atomic_u16_store(p, v, order) c89atomic_store_explicit_16(p, ((u16)v), order) +#define al_atomic_u16_add(p, v, order) c89atomic_fetch_add_explicit_16(p, ((u16)v), order) + +#define atomic_u8 c89atomic_uint8 #define al_atomic_u8_load(p, order) c89atomic_load_explicit_8(p, order) #define al_atomic_u8_store(p, v, order) c89atomic_store_explicit_8(p, ((u8)v), order) -#define atomic_bool volatile c89atomic_uint8 +#define atomic_bool c89atomic_uint8 #define al_atomic_bool_load(p, order) c89atomic_load_explicit_8(p, order) #define al_atomic_bool_store(p, v, order) c89atomic_store_explicit_8(p, ((bool)v), order) diff --git a/include/al/lib.h b/include/al/lib.h index aa0d0d2..f0da0ea 100644 --- a/include/al/lib.h +++ b/include/al/lib.h @@ -32,7 +32,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 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); @@ -95,7 +95,7 @@ AL_UNUSED_FUNCTION_POP #endif #ifndef LIKELY -#ifdef HAVE_BUILTIN_EXPECT +#ifdef AL_HAVE_BUILTIN_EXPECT #define LIKELY(x) __builtin_expect((x), 1) #else #define LIKELY(x) x @@ -103,7 +103,7 @@ AL_UNUSED_FUNCTION_POP #endif #ifndef UNLIKELY -#ifdef HAVE_BUILTIN_EXPECT +#ifdef AL_HAVE_BUILTIN_EXPECT #define UNLIKELY(x) __builtin_expect((x), 0) #else #define UNLIKELY(x) x diff --git a/include/al/log.h b/include/al/log.h index b3c57da..cf53503 100644 --- a/include/al/log.h +++ b/include/al/log.h @@ -9,10 +9,8 @@ void al_set_print(s32 (*print_func)(void *, char *), void *userdata); -s32 _al_log(const char *level, const char *section, const char *name, - const s32 line, const char *func, const char *fmt, ...); -s32 _al_logv(const char *level, const char *section, const char *name, - const s32 line, const char *func, const char *fmt, va_list args); +s32 _al_log(const char *level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, ...); +s32 _al_logv(const char *level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, va_list args); #define al_log(level, section, fmt, ...) _al_log(level, section, __LOCATION__, fmt, ##__VA_ARGS__); #define al_logv(level, section, fmt, args) _al_logv(level, section, __LOCATION__, fmt, args); @@ -24,7 +22,7 @@ s32 _al_logv(const char *level, const char *section, const char *name, #define al_log_debug(section, fmt, ...) al_log("debug", section, fmt, ##__VA_ARGS__) #else AL_UNUSED_FUNCTION_PUSH -static s32 al_log_nop(const char *section, const char *fmt, ...) +static inline s32 al_log_nop(const char *section, const char *fmt, ...) { (void)section; (void)fmt; return 0; } diff --git a/include/al/random.h b/include/al/random.h index 664f1ec..40c8654 100644 --- a/include/al/random.h +++ b/include/al/random.h @@ -2,11 +2,10 @@ #define _AL_RANDOM_H #include "lib.h" +#include "atomic.h" #define AL_RAND_MAX RAND_MAX -static u16 _al_rand_value = 1; - AL_UNUSED_FUNCTION_PUSH static void al_rand_init(void) @@ -14,16 +13,22 @@ static void al_rand_init(void) srand((u32)time(NULL)); } -// TEMP: "Random" that guarantees we won't get collisions for the current use-case. -static u16 al_rand_u16(void) +static s32 al_rand(void) { - if (_al_rand_value == UINT16_MAX) _al_rand_value = 1; - return _al_rand_value++; + return rand(); } -static s32 al_rand(void) +static atomic_u16 _al_rand_value = 1; + +// Temporary, can guarantee no collisions for the current use case. This functions per-file, +// so each file this is included in will have a seperate counter. +static u16 al_rand_u16(void) { - return rand(); + u16 ret = al_atomic_u16_add(&_al_rand_value, 1, AL_ATOMIC_RELAXED); + if (ret == UINT16_MAX) { + al_atomic_u16_store(&_al_rand_value, 1, AL_ATOMIC_RELAXED); + } + return ret; } AL_UNUSED_FUNCTION_POP diff --git a/include/al/ring_buffer.h b/include/al/ring_buffer.h index 686f47a..acfe4b0 100644 --- a/include/al/ring_buffer.h +++ b/include/al/ring_buffer.h @@ -6,8 +6,8 @@ struct al_ring_buffer { u8 *start; u8 *end; - volatile u8 *read; - volatile u8 *write; + u8 *read; + u8 *write; }; void al_ring_buffer_init(struct al_ring_buffer *buf, u8 *data, size_t length); diff --git a/include/al/str.h b/include/al/str.h index 0ab7520..6cef3b3 100644 --- a/include/al/str.h +++ b/include/al/str.h @@ -14,7 +14,8 @@ typedef struct { #define al_str_at(s, i) (s)->data[i] #define al_str_atr(s, i) (s)->data[(s)->len + i] -#define al_str_c(s) (&((str){ (u32)al_strlen(s), 0, ((char *)(s)) })) +#define al_str_c(s) (&((str){ (u32)(sizeof(s) - 1), 0, ((char *)(s)) })) +#define al_str_cr(s) (&((str){ (u32)al_strlen(s), 0, ((char *)(s)) })) // runtime #define al_str_w(s, i, n) (&((str){ ((u32)(n)), 0, &(s)[i] })) #define al_str_substr(s, start, end) al_str_w((s)->data, start, ((end) - (start))) diff --git a/include/al/wstr.h b/include/al/wstr.h index 2989ff9..263fa90 100644 --- a/include/al/wstr.h +++ b/include/al/wstr.h @@ -18,7 +18,8 @@ typedef struct { #define al_wstr_at(w, i) (w)->data[i] #define al_wstr_atr(w, i) (w)->data[(w)->len + i] -#define al_wstr_c(w) (&((wstr){ (u32)wcslen(w), 0, ((wchar_t *)(w)) })) +#define al_wstr_c(w) (&((wstr){ (u32)((sizeof(w) / sizeof(wchar_t)) - 1), 0, ((wchar_t *)(w)) })) +#define al_wstr_cr(w) (&((wstr){ (u32)wcslen(w), 0, ((wchar_t *)(w)) })) // runtime #define al_wstr_w(w, i, n) (&((wstr){ ((u32)(n)), 0, &al_wstr_at(w, i) })) #define al_wstr_substr(w, start, end) al_wstr_w(w, start, ((end) - (start))) -- cgit v1.2.3-101-g0448