summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore5
-rw-r--r--include/al/atomic.h2
-rw-r--r--include/al/lib.h32
-rw-r--r--include/al/queue.h41
-rw-r--r--include/al/str.h6
-rw-r--r--include/al/wstr.h24
-rw-r--r--meson.build19
-rw-r--r--src/array.c2
-rw-r--r--src/lib.c4
-rw-r--r--src/log.c2
-rw-r--r--subprojects/liblfds.wrap8
11 files changed, 75 insertions, 70 deletions
diff --git a/.gitignore b/.gitignore
index 0661a3f..8572582 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,6 @@
-build*/
-compile_commands.json
.cache/
.clangd/
+build*/
+compile_commands.json
subprojects/c89atomic/
-subprojects/liblfds7.1.1/
subprojects/packagecache/
diff --git a/include/al/atomic.h b/include/al/atomic.h
index 44766ff..1b733ad 100644
--- a/include/al/atomic.h
+++ b/include/al/atomic.h
@@ -36,6 +36,8 @@
#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 al_atomic_s32_add(p, v, order) c89atomic_fetch_add_explicit_i32(p, ((s32)v), order)
+#define al_atomic_s32_sub(p, v, order) c89atomic_fetch_sub_explicit_i32(p, ((s32)v), order)
#define atomic_u16 c89atomic_uint16
#define al_atomic_u16_load(p, order) c89atomic_load_explicit_16(p, order)
diff --git a/include/al/lib.h b/include/al/lib.h
index f0da0ea..71e0672 100644
--- a/include/al/lib.h
+++ b/include/al/lib.h
@@ -24,6 +24,7 @@
#include <stdarg.h>
#include <assert.h>
#include <ctype.h>
+#include <math.h>
#include <time.h>
void al_malloc_init(void);
@@ -53,6 +54,11 @@ void al_set_alloc(void *(*malloc_func)(size_t), void *(*calloc_func)(size_t, siz
#define al_assert(expr) do { (void)sizeof(expr); } while (0) // NOLINT(bugprone-sizeof-expression)
#endif
+#define al_assert_and_return(expr) do { \
+ al_assert(expr); \
+ return false; \
+} while(0)
+
#define al_alloc_object(type) (type *)al_calloc(1, sizeof(type))
AL_UNUSED_FUNCTION_PUSH
@@ -127,9 +133,14 @@ AL_UNUSED_FUNCTION_POP
#define AL_MAX(x, y) (((x) > (y)) ? (x) : (y))
#define AL_MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif
-
#define AL_CLAMP(n, lo, hi) AL_MIN(hi, AL_MAX(lo, n))
+#define AL_SWAP(a, b, type) do { \
+ type tmp = a; \
+ a = b; \
+ b = tmp; \
+} while (0)
+
#define AL_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#ifndef _MSVC_
@@ -139,12 +150,26 @@ AL_UNUSED_FUNCTION_POP
#define AL_PASTER(X, Y) AL_PASTER_EVALUATOR(X, Y)
#endif
+#define AL_BOOLSTR(b) ((b) ? "true" : "false")
+
#ifndef _MSVC_
#define __al_thread __thread
#else
#define __al_thread __declspec(thread)
#endif
+#if __has_attribute(__counted_by__)
+#define __al_counted_by(member) __attribute__((__counted_by__(member)))
+#else
+#define __al_counted_by(member)
+#endif
+
+#if __has_attribute(__sized_by__)
+#define __al_sized_by(member) __attribute__((__sized_by__(member)))
+#else
+#define __al_sized_by(member)
+#endif
+
AL_UNUSED_FUNCTION_PUSH
static inline size_t al_next_power_of_two(size_t n)
@@ -167,6 +192,11 @@ static inline u32 al_u32_inc_wrap(u32 v)
return (v == UINT32_MAX) ? 0 : v + 1;
}
+static inline u16 al_u16_inc_wrap(u16 v)
+{
+ return (v == UINT16_MAX) ? 0 : v + 1;
+}
+
static inline u32 al_u32_add_wrap(u32 v, u32 add)
{
return (((u64)v + add) > UINT32_MAX) ? (add - (UINT32_MAX - v)) : v + add;
diff --git a/include/al/queue.h b/include/al/queue.h
deleted file mode 100644
index b9c42b8..0000000
--- a/include/al/queue.h
+++ /dev/null
@@ -1,41 +0,0 @@
-#ifndef _AL_QUEUE_H
-#define _AL_QUEUE_H
-
-#include <liblfds711.h>
-
-#include "lib.h"
-
-typedef struct {
- struct lfds711_queue_bmm_state qbmms;
- struct lfds711_queue_bmm_element *qbmme_array;
-} queue;
-
-AL_UNUSED_FUNCTION_PUSH
-
-static inline void al_queue_init(queue *queue, size_t elems)
-{
- elems = al_next_power_of_two(elems);
- queue->qbmme_array = al_malloc(elems * sizeof(struct lfds711_queue_bmm_element));
- lfds711_queue_bmm_init_valid_on_current_logical_core(&queue->qbmms, queue->qbmme_array, elems, NULL);
-}
-
-static inline s32 al_queue_push(queue *queue, void *value)
-{
- return lfds711_queue_bmm_enqueue(&queue->qbmms, NULL, value);
-}
-
-static inline s32 al_queue_pop(queue *queue, void **value)
-{
- void *key;
- return lfds711_queue_bmm_dequeue(&queue->qbmms, &key, value);
-}
-
-static inline void al_queue_free(queue *queue)
-{
- lfds711_queue_bmm_cleanup(&queue->qbmms, NULL);
- al_free(queue->qbmme_array);
-}
-
-AL_UNUSED_FUNCTION_POP
-
-#endif // _AL_QUEUE_H
diff --git a/include/al/str.h b/include/al/str.h
index 6cef3b3..d5c7266 100644
--- a/include/al/str.h
+++ b/include/al/str.h
@@ -9,7 +9,7 @@ typedef struct {
char *data;
} str;
-#define AL_STR_EMPTY (str){ 0, 0, NULL }
+#define al_str_zero() (str){ 0, 0, NULL }
#define al_str_at(s, i) (s)->data[i]
#define al_str_atr(s, i) (s)->data[(s)->len + i]
@@ -52,7 +52,7 @@ static inline u32 al_str_reserve(str *s, u32 size)
static inline void al_str_sized(str *s, u32 size)
{
- *s = AL_STR_EMPTY;
+ *s = al_str_zero();
s->alloc = al_str_reserve(s, size);
}
@@ -66,7 +66,7 @@ static inline void al_str_clone(str *dest, str *src)
static inline void al_str_from(str *s, const char *c_str)
{
if (!c_str) {
- *s = AL_STR_EMPTY;
+ *s = al_str_zero();
return;
}
size_t len = al_strlen(c_str);
diff --git a/include/al/wstr.h b/include/al/wstr.h
index 263fa90..7c7e441 100644
--- a/include/al/wstr.h
+++ b/include/al/wstr.h
@@ -1,6 +1,5 @@
#ifndef _AL_WSTR_H
#define _AL_WSTR_H
-
#include "types.h"
#include "lib.h"
#include "str.h"
@@ -13,7 +12,7 @@ typedef struct {
wchar_t *data;
} wstr;
-#define AL_WSTR_EMPTY (wstr){ 0, 0, NULL }
+#define al_wstr_zero() (wstr){ 0, 0, NULL }
#define al_wstr_at(w, i) (w)->data[i]
#define al_wstr_atr(w, i) (w)->data[(w)->len + i]
@@ -31,7 +30,12 @@ AL_UNUSED_FUNCTION_PUSH
static inline s32 al_wchar_width(wchar_t wc)
{
+#ifdef AL_HAVE_WIDE_STRING
return wcwidth(wc);
+#else
+ (void)wc;
+ return 0;
+#endif
}
static inline void al_wstr_free(wstr *w)
@@ -56,7 +60,7 @@ static inline u32 al_wstr_reserve(wstr *w, u32 size)
static inline void al_wstr_sized(wstr *w, u32 size)
{
- *w = AL_WSTR_EMPTY;
+ *w = al_wstr_zero();
w->alloc = al_wstr_reserve(w, size * sizeof(wchar_t));
al_memset(w->data, 0, w->alloc);
}
@@ -83,9 +87,22 @@ static inline void al_wstr_from_str(wstr *w, str *s)
al_free(c_str);
}
+static inline void al_wstr_to_str(wstr *w, str *s)
+{
+ u32 len = (u32)wcstombs(NULL, w->data, 0);
+ al_str_sized(s, len);
+ s->len = len;
+ wcstombs(s->data, w->data, s->len);
+}
+
static inline s32 al_wstr_width(wstr *w)
{
+#ifdef AL_HAVE_WIDE_STRING
return wcswidth(w->data, w->len);
+#else
+ (void)w;
+ return 0;
+#endif
}
static inline s32 al_wstr_cmp(wstr *w, wstr *c, u32 start, u32 len)
@@ -102,5 +119,4 @@ static inline s32 al_wstr_rfind(wstr *w, wchar_t c)
}
AL_UNUSED_FUNCTION_POP
-
#endif // _AL_WSTR_H
diff --git a/meson.build b/meson.build
index 0ff7e86..7251227 100644
--- a/meson.build
+++ b/meson.build
@@ -11,26 +11,35 @@ supports_needed_gnu_extensions = not is_msvc_specifically
have_posix_memalign = compiler.has_function(
'posix_memalign', prefix: '#define _POSIX_C_SOURCE 200112L\n#include <stdlib.h>')
have_builtin_expect = compiler.has_function('__builtin_expect')
+have_wide_string = compiler.has_function('wcswidth')
+
+alabaster_args = []
-alabaster_args = ['-D_POSIX_C_SOURCE=200112L', '-D_XOPEN_SOURCE=500']
if is_debug
alabaster_args += ['-D_DEBUG_']
endif
+
+if is_msvc_specifically
+ alabaster_args += ['/arch:AVX']
+endif
+
if supports_needed_gnu_extensions
alabaster_args += ['-DAL_HAVE_GNU_EXTENSIONS']
endif
+
if have_posix_memalign
alabaster_args += ['-DAL_HAVE_POSIX_MEMALIGN']
endif
+
if have_builtin_expect
alabaster_args += ['-DAL_HAVE_BUILTIN_EXPECT']
endif
-if is_msvc_specifically
- alabaster_args += ['/arch:AVX']
+
+if have_wide_string
+ alabaster_args += ['-DAL_HAVE_WIDE_STRING']
endif
c89atomic = subproject('c89atomic').get_variable('c89atomic')
-liblfds = subproject('liblfds').get_variable('liblfds')
alabaster_src = [
'src/lib.c',
@@ -39,7 +48,7 @@ alabaster_src = [
'src/ring_buffer.c'
]
alabaster_inc = include_directories('include')
-alabaster_deps = [c89atomic, liblfds]
+alabaster_deps = [c89atomic]
alabaster = declare_dependency(include_directories: alabaster_inc,
dependencies: alabaster_deps, sources: alabaster_src,
diff --git a/src/array.c b/src/array.c
index 6af0f40..5175d6f 100644
--- a/src/array.c
+++ b/src/array.c
@@ -8,7 +8,7 @@
struct { \
u32 size; \
u32 alloc; \
- type *data; \
+ type *__al_sized_by(alloc) data; \
}
#define al_array_init(arr) ((arr).size = 0, (arr).alloc = 0, (arr).data = NULL)
diff --git a/src/lib.c b/src/lib.c
index 8838495..063da23 100644
--- a/src/lib.c
+++ b/src/lib.c
@@ -1,5 +1,3 @@
-// Testing allocation tracking stuff. (Very scuffed, not thread-safe!)
-
#include "../include/al/lib.h"
#if AL_USE_STDLIB
@@ -11,8 +9,8 @@ static void (*_al_free)(void *) = free;
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
struct alloc_t {
void *ptr;
diff --git a/src/log.c b/src/log.c
index 370df07..409e94d 100644
--- a/src/log.c
+++ b/src/log.c
@@ -1,6 +1,6 @@
#include "../include/al/log.h"
-#define AL_LOG_SKIP
+//#define AL_LOG_SKIP
#define AL_LOG_MESSAGE_SIZE 511
#define AL_LOG_USE_SECTION
#ifdef AL_LOG_USE_SECTION
diff --git a/subprojects/liblfds.wrap b/subprojects/liblfds.wrap
deleted file mode 100644
index d91df59..0000000
--- a/subprojects/liblfds.wrap
+++ /dev/null
@@ -1,8 +0,0 @@
-[wrap-file]
-directory = liblfds7.1.1
-
-source_url = https://liblfds.org/downloads/liblfds%20release%207.1.1%20source.tar.bz2
-source_filename = liblfds%20release%207.1.1%20source.tar.bz2
-source_hash = 4cb93868e5830bc02214b90bb76bdce836c38007757eec96d56eb5d9a4b78c47
-
-patch_directory = liblfds