From 03e6780bdf46b5080efbe8d51106cccc43d5a292 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Sun, 17 Dec 2023 10:37:31 -0500 Subject: Update - Add queue (proof of concept liblfds wrapper) - Cleanup log code - Misc fixes Signed-off-by: Andrew Opalach --- .gitignore | 2 ++ include/al/array.h | 2 +- include/al/lib.h | 13 ++++++++- include/al/log.h | 31 +++++++++++---------- include/al/queue.h | 41 ++++++++++++++++++++++++++++ include/al/random.h | 4 --- include/al/str.h | 6 ---- include/al/wstr.h | 16 +++++++---- meson.build | 5 ++-- src/array.c | 2 +- src/log.c | 38 ++++++++++---------------- subprojects/liblfds.wrap | 8 ++++++ subprojects/packagefiles/liblfds/meson.build | 11 ++++++++ 13 files changed, 120 insertions(+), 59 deletions(-) create mode 100644 include/al/queue.h create mode 100644 subprojects/liblfds.wrap create mode 100644 subprojects/packagefiles/liblfds/meson.build diff --git a/.gitignore b/.gitignore index 47e95ff..0661a3f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ compile_commands.json .cache/ .clangd/ subprojects/c89atomic/ +subprojects/liblfds7.1.1/ +subprojects/packagecache/ diff --git a/include/al/array.h b/include/al/array.h index 07fd794..d91d7ce 100644 --- a/include/al/array.h +++ b/include/al/array.h @@ -8,4 +8,4 @@ #define AL_ARRAY_FREE al_free #include "../../src/array.c" -#endif //_AL_ARRAY_H +#endif // _AL_ARRAY_H diff --git a/include/al/lib.h b/include/al/lib.h index c94f817..b9b2318 100644 --- a/include/al/lib.h +++ b/include/al/lib.h @@ -24,6 +24,7 @@ #include #include #include +#include void al_malloc_init(void); void al_malloc_close(void); @@ -69,6 +70,16 @@ static inline s32 al_printf(const char *fmt, ...) #endif } +static inline s32 al_vprintf(const char *fmt, va_list args) +{ +#if AL_FORCE_DISABLE_OUTPUT + return 0; +#else + s32 ret = vprintf(fmt, args); + return ret; +#endif +} + static inline void *al_memrchr(const void *s, s32 c, size_t n) { const u8 *src = (const u8 *)s + n - 1; @@ -131,7 +142,7 @@ AL_UNUSED_FUNCTION_POP #ifndef _MSVC_ #define __al_thread __thread #else -#define __al_thread +#define __al_thread __declspec(thread) #endif AL_UNUSED_FUNCTION_PUSH diff --git a/include/al/log.h b/include/al/log.h index ae3d2d0..1848840 100644 --- a/include/al/log.h +++ b/include/al/log.h @@ -5,27 +5,28 @@ #include #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) +#define __LOCATION__ __FILENAME__, __LINE__, __FUNCTION__ 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_nop(const char *section, const char *fmt, ...); +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, __FILENAME__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__) -#define al_logv(level, section, fmt, args) \ - _al_logv(#level, section, __FILENAME__, __LINE__, __FUNCTION__, fmt, 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); -#define al_log_info(section, fmt, ...) al_log(info, section, fmt, ##__VA_ARGS__) -#define al_log_warn(section, fmt, ...) al_log(warn, section, fmt, ##__VA_ARGS__) -#define al_log_error(section, fmt, ...) al_log(error, section, fmt, ##__VA_ARGS__) +#define al_log_info(section, fmt, ...) al_log("info", section, fmt, ##__VA_ARGS__) +#define al_log_warn(section, fmt, ...) al_log("warn", section, fmt, ##__VA_ARGS__) +#define al_log_error(section, fmt, ...) al_log("error", section, fmt, ##__VA_ARGS__) #ifdef _DEBUG_ -#define al_log_debug(section, fmt, ...) al_log(debug, section, fmt, ##__VA_ARGS__) +#define al_log_debug(section, fmt, ...) al_log("debug", section, fmt, ##__VA_ARGS__) #else -#define al_log_debug(section, fmt, ...) _al_log_nop(section, fmt, ##__VA_ARGS__) +AL_UNUSED_FUNCTION_PUSH +static s32 al_log_nop(const char *section, const char *fmt, ...) { (void)section; (void)fmt; return 0; } +AL_UNUSED_FUNCTION_POP +#define al_log_debug(section, fmt, ...) al_log_nop(section, fmt, ##__VA_ARGS__) #endif -#endif //_AL_LOG_H +#endif // _AL_LOG_H diff --git a/include/al/queue.h b/include/al/queue.h new file mode 100644 index 0000000..b9c42b8 --- /dev/null +++ b/include/al/queue.h @@ -0,0 +1,41 @@ +#ifndef _AL_QUEUE_H +#define _AL_QUEUE_H + +#include + +#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/random.h b/include/al/random.h index c862835..664f1ec 100644 --- a/include/al/random.h +++ b/include/al/random.h @@ -3,10 +3,6 @@ #include "lib.h" -#if AL_USE_STDLIB -#include -#endif - #define AL_RAND_MAX RAND_MAX static u16 _al_rand_value = 1; diff --git a/include/al/str.h b/include/al/str.h index 406d66e..9a3c2de 100644 --- a/include/al/str.h +++ b/include/al/str.h @@ -14,15 +14,9 @@ typedef struct { #define al_str_at(s, i) (s)->data[i] #define al_str_atr(s, i) (s)->data[(s)->len + i] -#ifndef __cplusplus #define al_str_c(s) (&((str){ (u32)al_strlen(s), 0, ((char *)(s)) })) #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))) -#else -#define al_str_cc(s) (((str){ (u32)al_strlen(s), 0, ((char *)(s)) })) -#define al_str_ww(s, i, n) (((str){ ((u32)(n)), 0, &(s)[i] })) -#define al_str_substr(s, start, end) al_str_ww((s)->data, start, ((end) - (start))) -#endif #define AL_STR_FMT "%.*s" #define AL_STR_PRINTF(s) (s)->len, (s)->data diff --git a/include/al/wstr.h b/include/al/wstr.h index 97568db..99c0157 100644 --- a/include/al/wstr.h +++ b/include/al/wstr.h @@ -18,6 +18,9 @@ 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_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))) + #define AL_WSTR_FMT "%.*ls" #define AL_WSTR_PRINTF(w) (w)->len, (w)->data #define AL_WSTR_PRINTF_MAXLEN(w, l) AL_MIN((w)->len, (l)), (w)->data @@ -54,6 +57,13 @@ static inline void al_wstr_sized(wstr *w, u32 size) al_memset(w->data, 0, w->alloc); } +static inline void al_wstr_clone(wstr *dest, wstr *src) +{ + al_wstr_sized(dest, src->len); + al_memcpy(dest->data, src->data, src->len * sizeof(wchar_t)); + dest->len = src->len; +} + static inline void al_wstr_from_cstr(wstr *w, char *c) { size_t n = mbstowcs(NULL, c, 0) + 1; @@ -71,11 +81,7 @@ static inline void al_wstr_from_str(wstr *w, str *s) static inline s32 al_wstr_width(wstr *w) { - s32 width = 0; - for (u32 i = 0; i < w->len; i++) { - width += al_wchar_width(al_wstr_at(w, i)); - } - return width; + return wcswidth(w->data, w->len); } #endif // _AL_WSTR_H diff --git a/meson.build b/meson.build index a47d3eb..a230d4d 100644 --- a/meson.build +++ b/meson.build @@ -30,6 +30,7 @@ if is_msvc_specifically endif c89atomic = subproject('c89atomic').get_variable('c89atomic') +liblfds = subproject('liblfds').get_variable('liblfds') alabaster_src = [ 'src/lib.c', @@ -38,7 +39,7 @@ alabaster_src = [ 'src/ring_buffer.c' ] alabaster_inc = include_directories('include') -alabaster_deps = [c89atomic] +alabaster_deps = [c89atomic, liblfds] alabaster = declare_dependency(include_directories: alabaster_inc, dependencies: alabaster_deps, sources: alabaster_src, @@ -54,5 +55,5 @@ if get_option('build-tests') and not meson.is_subproject() if supports_needed_gnu_extensions tests_src += ['tests/array_typed.c'] endif - executable('tests', tests_src, dependencies: [alabaster], install: false) + executable('tests', tests_src, dependencies: alabaster, install: false) endif diff --git a/src/array.c b/src/array.c index e95b1a9..9b61cd9 100644 --- a/src/array.c +++ b/src/array.c @@ -24,9 +24,9 @@ do { \ al_array_init(dest); \ al_array_reserve(dest, (src).size); \ + (dest).size = (src).size; \ al_memcpy(&al_array_at(dest, 0), &al_array_at(src, 0), \ al_array_item_size(dest) * (dest).size); \ - (dest).size = (src).size; \ } while (0) #define al_array_push(arr, item) \ diff --git a/src/log.c b/src/log.c index 9a738df..5c41248 100644 --- a/src/log.c +++ b/src/log.c @@ -13,8 +13,8 @@ static s32 al_print_default(void *userdata, char *s) return ret; } -__al_thread s32 (*_al_print)(void *, char *) = al_print_default; -__al_thread void *_al_log_userdata = NULL; +static s32 (*_al_print)(void *, char *) = al_print_default; +static void *_al_log_userdata = NULL; void al_set_print(s32 (*print_func)(void *, char *), void *userdata) { @@ -22,11 +22,10 @@ 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); - al_memset(buffer, 0, AL_LOG_MESSAGE_SIZE); s32 prefix = snprintf(buffer, AL_LOG_MESSAGE_SIZE, AL_LOG_TEMPLATE, name, line, func, level, section); prefix += vsnprintf(buffer + prefix, AL_LOG_MESSAGE_SIZE - prefix, fmt, args); buffer[strcspn(buffer, "\r\n")] = '\0'; @@ -36,15 +35,8 @@ 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_nop(const char *section, const char *fmt, ...) -{ - (void)section; - (void)fmt; - return 0; -} - -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; @@ -52,10 +44,9 @@ s32 _al_log(const char *level, const char *section, const char *name, const s32 va_list args; va_start(args, fmt); #if AL_LOG_SKIP - s32 ret = printf(AL_LOG_TEMPLATE, name, line, func, level, section); - ret += vprintf(fmt, args); - size_t cspn = strcspn(fmt, "\r\n"); - if (cspn == strlen(fmt)) ret += printf("\n"); + 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 @@ -68,17 +59,16 @@ s32 _al_log(const char *level, const char *section, const char *name, const s32 #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 - s32 ret = printf(AL_LOG_TEMPLATE, name, line, func, level, section); - ret += vprintf(fmt, args); - size_t cspn = strcspn(fmt, "\r\n"); - if (cspn == strlen(fmt)) ret += printf("\n"); + 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 diff --git a/subprojects/liblfds.wrap b/subprojects/liblfds.wrap new file mode 100644 index 0000000..d91df59 --- /dev/null +++ b/subprojects/liblfds.wrap @@ -0,0 +1,8 @@ +[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 diff --git a/subprojects/packagefiles/liblfds/meson.build b/subprojects/packagefiles/liblfds/meson.build new file mode 100644 index 0000000..2428817 --- /dev/null +++ b/subprojects/packagefiles/liblfds/meson.build @@ -0,0 +1,11 @@ +project('liblfds', 'c', version: '7.1.1') + +liblfds_src = [ + 'liblfds711/src/lfds711_queue_bounded_manyproducer_manyconsumer/lfds711_queue_bounded_manyproducer_manyconsumer_cleanup.c', + 'liblfds711/src/lfds711_queue_bounded_manyproducer_manyconsumer/lfds711_queue_bounded_manyproducer_manyconsumer_dequeue.c', + 'liblfds711/src/lfds711_queue_bounded_manyproducer_manyconsumer/lfds711_queue_bounded_manyproducer_manyconsumer_enqueue.c', + 'liblfds711/src/lfds711_queue_bounded_manyproducer_manyconsumer/lfds711_queue_bounded_manyproducer_manyconsumer_init.c', + 'liblfds711/src/lfds711_queue_bounded_manyproducer_manyconsumer/lfds711_queue_bounded_manyproducer_manyconsumer_query.c', + 'liblfds711/src/lfds711_misc/lfds711_misc_internal_backoff_init.c' +] +liblfds = declare_dependency(sources: liblfds_src, include_directories: include_directories('./liblfds711/inc')) -- cgit v1.2.3-101-g0448