diff options
| author | 2023-12-17 10:37:31 -0500 | |
|---|---|---|
| committer | 2023-12-17 10:37:31 -0500 | |
| commit | 03e6780bdf46b5080efbe8d51106cccc43d5a292 (patch) | |
| tree | 5e04a0921caf2fbaf93c4652329f61416b269a4a /include | |
| parent | 55be64bcb8a770baba256a4598964b226c47fafe (diff) | |
| download | libalabaster-03e6780bdf46b5080efbe8d51106cccc43d5a292.tar.gz libalabaster-03e6780bdf46b5080efbe8d51106cccc43d5a292.tar.bz2 libalabaster-03e6780bdf46b5080efbe8d51106cccc43d5a292.zip | |
Update
- Add queue (proof of concept liblfds wrapper)
- Cleanup log code
- Misc fixes
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'include')
| -rw-r--r-- | include/al/array.h | 2 | ||||
| -rw-r--r-- | include/al/lib.h | 13 | ||||
| -rw-r--r-- | include/al/log.h | 31 | ||||
| -rw-r--r-- | include/al/queue.h | 41 | ||||
| -rw-r--r-- | include/al/random.h | 4 | ||||
| -rw-r--r-- | include/al/str.h | 6 | ||||
| -rw-r--r-- | include/al/wstr.h | 16 |
7 files changed, 81 insertions, 32 deletions
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 <stdarg.h> #include <assert.h> #include <ctype.h> +#include <time.h> 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 <al/lib.h> #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 <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/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 <time.h> -#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 |