summaryrefslogtreecommitdiff
path: root/include/al
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2024-09-19 14:56:17 -0400
committerAndrew Opalach <andrew@akon.city> 2024-09-19 14:56:17 -0400
commit395d423e6a6fda56e02d9551cadc93d229fe71c5 (patch)
tree3824245372a458dd4ce6d18c73c51880cdf75d5d /include/al
parent7f7566324b18833d2868ea45e2b28dcdc547d879 (diff)
downloadlibalabaster-395d423e6a6fda56e02d9551cadc93d229fe71c5.tar.gz
libalabaster-395d423e6a6fda56e02d9551cadc93d229fe71c5.tar.bz2
libalabaster-395d423e6a6fda56e02d9551cadc93d229fe71c5.zip
Update
- Remove queue and liblfds - Add some helpers - Improve wstr compatibility Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'include/al')
-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
5 files changed, 56 insertions, 49 deletions
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