summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2024-04-09 11:17:46 -0400
committerAndrew Opalach <andrew@akon.city> 2024-04-09 11:17:46 -0400
commit7f7566324b18833d2868ea45e2b28dcdc547d879 (patch)
tree952b6ef9e391e6a3dca1a4337a023e83b1f92b7e
parent4da4b06c8ae79370340a60ca83bd1f0206068ed3 (diff)
downloadlibalabaster-7f7566324b18833d2868ea45e2b28dcdc547d879.tar.gz
libalabaster-7f7566324b18833d2868ea45e2b28dcdc547d879.tar.bz2
libalabaster-7f7566324b18833d2868ea45e2b28dcdc547d879.zip
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 <andrew@akon.city>
-rw-r--r--include/al/array_typed.h2
-rw-r--r--include/al/atomic.h19
-rw-r--r--include/al/lib.h6
-rw-r--r--include/al/log.h8
-rw-r--r--include/al/random.h21
-rw-r--r--include/al/ring_buffer.h4
-rw-r--r--include/al/str.h3
-rw-r--r--include/al/wstr.h3
-rw-r--r--meson.build6
-rw-r--r--src/lib.c4
-rw-r--r--src/log.c31
-rw-r--r--src/ring_buffer.c2
-rw-r--r--tests/main.c4
13 files changed, 65 insertions, 48 deletions
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)))
diff --git a/meson.build b/meson.build
index bb73e4d..0ff7e86 100644
--- a/meson.build
+++ b/meson.build
@@ -17,13 +17,13 @@ if is_debug
alabaster_args += ['-D_DEBUG_']
endif
if supports_needed_gnu_extensions
- alabaster_args += ['-DHAVE_GNU_EXTENSIONS']
+ alabaster_args += ['-DAL_HAVE_GNU_EXTENSIONS']
endif
if have_posix_memalign
- alabaster_args += ['-DHAVE_POSIX_MEMALIGN']
+ alabaster_args += ['-DAL_HAVE_POSIX_MEMALIGN']
endif
if have_builtin_expect
- alabaster_args += ['-DHAVE_BUILTIN_EXPECT']
+ alabaster_args += ['-DAL_HAVE_BUILTIN_EXPECT']
endif
if is_msvc_specifically
alabaster_args += ['/arch:AVX']
diff --git a/src/lib.c b/src/lib.c
index 64a60a3..8838495 100644
--- a/src/lib.c
+++ b/src/lib.c
@@ -7,7 +7,7 @@ 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 HAVE_POSIX_MEMALIGN
+#ifdef AL_HAVE_POSIX_MEMALIGN
s32 (*_al_posix_memalign)(void **, size_t, size_t) = posix_memalign;
#endif
@@ -134,7 +134,7 @@ void al_free(void *ptr)
_al_free(ptr);
}
-#ifdef 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);
diff --git a/src/log.c b/src/log.c
index 5c41248..370df07 100644
--- a/src/log.c
+++ b/src/log.c
@@ -1,10 +1,15 @@
#include "../include/al/log.h"
-#define AL_LOG_SKIP 0
+#define AL_LOG_SKIP
#define AL_LOG_MESSAGE_SIZE 511
+#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 -> "
+#endif
-#if !AL_LOG_SKIP
+#ifndef AL_LOG_SKIP
static s32 al_print_default(void *userdata, char *s)
{
(void)userdata;
@@ -22,11 +27,15 @@ void al_set_print(s32 (*print_func)(void *, char *), void *userdata)
_al_log_userdata = 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)
+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
s32 prefix = snprintf(buffer, AL_LOG_MESSAGE_SIZE, AL_LOG_TEMPLATE, name, line, func, level, section);
+#else
+ (void)section;
+ s32 prefix = snprintf(buffer, AL_LOG_MESSAGE_SIZE, AL_LOG_TEMPLATE, name, line, func, level);
+#endif
prefix += vsnprintf(buffer + prefix, AL_LOG_MESSAGE_SIZE - prefix, fmt, args);
buffer[strcspn(buffer, "\r\n")] = '\0';
return buffer;
@@ -35,15 +44,14 @@ static char *get_buffer(const char *level, const char *section, const char *fmt,
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, ...)
+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
return 0;
#else
va_list args;
va_start(args, fmt);
-#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");
@@ -51,7 +59,7 @@ s32 _al_log(const char *level, const char *section, const char *name,
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);
@@ -59,20 +67,19 @@ s32 _al_log(const char *level, const char *section, const char *name,
#endif
}
-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_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
return 0;
#else
-#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/src/ring_buffer.c b/src/ring_buffer.c
index 751ea4a..c985d86 100644
--- a/src/ring_buffer.c
+++ b/src/ring_buffer.c
@@ -18,7 +18,7 @@ static inline u8 *previous(struct al_ring_buffer *buf, u8 *ptr)
return --ptr;
}
-static inline void add(struct al_ring_buffer *buf, volatile u8 **v, u8 *ptr, size_t n)
+static inline void add(struct al_ring_buffer *buf, u8 **v, u8 *ptr, size_t n)
{
ptr += n;
al_assert(ptr <= buf->end);
diff --git a/tests/main.c b/tests/main.c
index 622e969..2d50d72 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -1,7 +1,7 @@
#include <al/lib.h>
#include "array.h"
-#ifdef HAVE_GNU_EXTENSIONS
+#ifdef AL_HAVE_GNU_EXTENSIONS
#include "array_typed.h"
#endif
#include "str.h"
@@ -10,7 +10,7 @@
s32 main(void)
{
if (!array_tests_run()) goto fail;
-#ifdef HAVE_GNU_EXTENSIONS
+#ifdef AL_HAVE_GNU_EXTENSIONS
if (!array_typed_tests_run()) goto fail;
#endif
if (!str_tests_run()) goto fail;