diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/al/array_typed.h | 62 | ||||
| -rw-r--r-- | include/al/atomic.h | 41 | ||||
| -rw-r--r-- | include/al/lib.h | 97 | ||||
| -rw-r--r-- | include/al/macros.h | 84 | ||||
| -rw-r--r-- | include/al/random.h | 13 | ||||
| -rw-r--r-- | include/al/str.h | 9 | ||||
| -rw-r--r-- | include/al/types.h | 1 | ||||
| -rw-r--r-- | include/al/wstr.h | 2 |
8 files changed, 186 insertions, 123 deletions
diff --git a/include/al/array_typed.h b/include/al/array_typed.h index a48b230..ae93bf4 100644 --- a/include/al/array_typed.h +++ b/include/al/array_typed.h @@ -5,38 +5,40 @@ #error "missing required GNU C extensions" #endif -#define AL_EMPTY_DEFAULT(...) AL_EMPTY_DEFAULT_0(__VA_ARGS__, , ) -#define AL_EMPTY_DEFAULT_0(FUNC, T, S, ...) FUNC(T, S) -#define AL_PASTER_EVALUATOR(X, Y) X##_##Y -#define AL_PASTER(X, Y) AL_PASTER_EVALUATOR(X, Y) +#include "macros.h" -#define _array(N, ...) AL_PASTER(array, AL_PASTER(N, __VA_ARGS__)) -#define _al_array_init(N, ...) AL_PASTER(_array_init, AL_PASTER(N, __VA_ARGS__)) -#define _al_array_data(N, ...) AL_PASTER(_array_data, AL_PASTER(N, __VA_ARGS__)) -#define _al_array_reserve(N, ...) AL_PASTER(_array_reserve, AL_PASTER(N, __VA_ARGS__)) -#define __al_array_reserve_internal(N, ...) AL_PASTER(_array_reserve_internal, AL_PASTER(N, __VA_ARGS__)) -#define _al_array_at(N, ...) AL_PASTER(_array_at, AL_PASTER(N, __VA_ARGS__)) -#define _al_array_last(N, ...) AL_PASTER(_array_last, AL_PASTER(N, __VA_ARGS__)) -#define _al_array_push(N, ...) AL_PASTER(_array_push, AL_PASTER(N, __VA_ARGS__)) -#define _al_array_pop(N, ...) AL_PASTER(_array_pop, AL_PASTER(N, __VA_ARGS__)) -#define _al_array_remove_at(N, ...) AL_PASTER(_array_remove_at, AL_PASTER(N, __VA_ARGS__)) -#define _al_array_pop_at(N, ...) AL_PASTER(_array_pop_at, AL_PASTER(N, __VA_ARGS__)) -#define _al_array_insert(N, ...) AL_PASTER(_array_insert, AL_PASTER(N, __VA_ARGS__)) -#define _al_array_free(N, ...) AL_PASTER(_array_free, AL_PASTER(N, __VA_ARGS__)) +#define AL_ARRAY_EMPTY_DEFAULT_0(FUNC, T, S, ...) FUNC(T, S) +#define AL_ARRAY_EMPTY_DEFAULT(...) AL_ARRAY_EMPTY_DEFAULT_0(__VA_ARGS__, , ) +#define AL_ARRAY_PASTER_EVALUATOR(X, Y) X##_##Y +#define AL_ARRAY_PASTER(X, Y) AL_ARRAY_PASTER_EVALUATOR(X, Y) -#define array(...) AL_EMPTY_DEFAULT(_array, __VA_ARGS__) -#define al_array_init(...) AL_EMPTY_DEFAULT(_al_array_init, __VA_ARGS__) -#define al_array_data(...) AL_EMPTY_DEFAULT(_al_array_data, __VA_ARGS__) -#define _al_array_reserve_internal(...) AL_EMPTY_DEFAULT(__al_array_reserve_internal, __VA_ARGS__) -#define al_array_reserve(...) AL_EMPTY_DEFAULT(_al_array_reserve, __VA_ARGS__) -#define al_array_at(...) AL_EMPTY_DEFAULT(_al_array_at, __VA_ARGS__) -#define al_array_last(...) AL_EMPTY_DEFAULT(_al_array_last, __VA_ARGS__) -#define al_array_push(...) AL_EMPTY_DEFAULT(_al_array_push, __VA_ARGS__) -#define al_array_pop(...) AL_EMPTY_DEFAULT(_al_array_pop, __VA_ARGS__) -#define al_array_remove_at(...) AL_EMPTY_DEFAULT(_al_array_remove_at, __VA_ARGS__) -#define al_array_pop_at(...) AL_EMPTY_DEFAULT(_al_array_pop_at, __VA_ARGS__) -#define al_array_insert(...) AL_EMPTY_DEFAULT(_al_array_insert, __VA_ARGS__) -#define al_array_free(...) AL_EMPTY_DEFAULT(_al_array_free, __VA_ARGS__) +#define _array(N, ...) AL_ARRAY_PASTER(array, AL_ARRAY_PASTER(N, __VA_ARGS__)) +#define _al_array_init(N, ...) AL_ARRAY_PASTER(_array_init, AL_ARRAY_PASTER(N, __VA_ARGS__)) +#define _al_array_data(N, ...) AL_ARRAY_PASTER(_array_data, AL_ARRAY_PASTER(N, __VA_ARGS__)) +#define _al_array_reserve(N, ...) AL_ARRAY_PASTER(_array_reserve, AL_ARRAY_PASTER(N, __VA_ARGS__)) +#define __al_array_reserve_internal(N, ...) AL_ARRAY_PASTER(_array_reserve_internal, AL_ARRAY_PASTER(N, __VA_ARGS__)) +#define _al_array_at(N, ...) AL_ARRAY_PASTER(_array_at, AL_ARRAY_PASTER(N, __VA_ARGS__)) +#define _al_array_last(N, ...) AL_ARRAY_PASTER(_array_last, AL_ARRAY_PASTER(N, __VA_ARGS__)) +#define _al_array_push(N, ...) AL_ARRAY_PASTER(_array_push, AL_ARRAY_PASTER(N, __VA_ARGS__)) +#define _al_array_pop(N, ...) AL_ARRAY_PASTER(_array_pop, AL_ARRAY_PASTER(N, __VA_ARGS__)) +#define _al_array_remove_at(N, ...) AL_ARRAY_PASTER(_array_remove_at, AL_ARRAY_PASTER(N, __VA_ARGS__)) +#define _al_array_pop_at(N, ...) AL_ARRAY_PASTER(_array_pop_at, AL_ARRAY_PASTER(N, __VA_ARGS__)) +#define _al_array_insert(N, ...) AL_ARRAY_PASTER(_array_insert, AL_ARRAY_PASTER(N, __VA_ARGS__)) +#define _al_array_free(N, ...) AL_ARRAY_PASTER(_array_free, AL_ARRAY_PASTER(N, __VA_ARGS__)) + +#define array(...) AL_ARRAY_EMPTY_DEFAULT(_array, __VA_ARGS__) +#define al_array_init(...) AL_ARRAY_EMPTY_DEFAULT(_al_array_init, __VA_ARGS__) +#define al_array_data(...) AL_ARRAY_EMPTY_DEFAULT(_al_array_data, __VA_ARGS__) +#define _al_array_reserve_internal(...) AL_ARRAY_EMPTY_DEFAULT(__al_array_reserve_internal, __VA_ARGS__) +#define al_array_reserve(...) AL_ARRAY_EMPTY_DEFAULT(_al_array_reserve, __VA_ARGS__) +#define al_array_at(...) AL_ARRAY_EMPTY_DEFAULT(_al_array_at, __VA_ARGS__) +#define al_array_last(...) AL_ARRAY_EMPTY_DEFAULT(_al_array_last, __VA_ARGS__) +#define al_array_push(...) AL_ARRAY_EMPTY_DEFAULT(_al_array_push, __VA_ARGS__) +#define al_array_pop(...) AL_ARRAY_EMPTY_DEFAULT(_al_array_pop, __VA_ARGS__) +#define al_array_remove_at(...) AL_ARRAY_EMPTY_DEFAULT(_al_array_remove_at, __VA_ARGS__) +#define al_array_pop_at(...) AL_ARRAY_EMPTY_DEFAULT(_al_array_pop_at, __VA_ARGS__) +#define al_array_insert(...) AL_ARRAY_EMPTY_DEFAULT(_al_array_insert, __VA_ARGS__) +#define al_array_free(...) AL_ARRAY_EMPTY_DEFAULT(_al_array_free, __VA_ARGS__) #define AL_ARRAY_DEFINE_FUNCTIONS(N, T, ...) \ static inline T *al_array_at(N, __VA_ARGS__)(array(N, __VA_ARGS__) *a, u32 i) \ diff --git a/include/al/atomic.h b/include/al/atomic.h index 594413c..a8bb6b2 100644 --- a/include/al/atomic.h +++ b/include/al/atomic.h @@ -3,7 +3,7 @@ #include <c89atomic.h> -#include "types.h" +#include "lib.h" #define AL_ATOMIC_RELAXED c89atomic_memory_order_relaxed #define AL_ATOMIC_CONSUME c89atomic_memory_order_consume @@ -30,26 +30,28 @@ typedef f32 c89atomic_f32; #define AL_ATOMIC_DEFINE(T, C, S) \ typedef c89atomic_##C atomic(T); \ - static inline T al_atomic_load(T)(atomic(T) *a, int order) \ + static inline T al_atomic_load(T)(atomic(T) *a, s32 order) \ { \ return c89atomic_load_explicit_##S(a, order); \ } \ - static inline void al_atomic_store(T)(atomic(T) *a, T value, int order) \ + static inline void al_atomic_store(T)(atomic(T) *a, T value, s32 order) \ { \ c89atomic_store_explicit_##S(a, value, order); \ } \ - static inline T al_atomic_add(T)(atomic(T) *a, T value, int order) \ + static inline T al_atomic_compare_and_swap(T)(atomic(T) *a, T expected, T desired) \ + { \ + return c89atomic_compare_and_swap_##S(a, expected, desired); \ + } + +#define AL_ATOMIC_DEFINE_EXT(T, C, S) \ + static inline T al_atomic_add(T)(atomic(T) *a, T value, s32 order) \ { \ return c89atomic_fetch_add_explicit_##S(a, value, order); \ } \ - static inline T al_atomic_sub(T)(atomic(T) *a, T value, int order) \ + static inline T al_atomic_sub(T)(atomic(T) *a, T value, s32 order) \ { \ return c89atomic_fetch_sub_explicit_##S(a, value, order); \ } \ - static inline T al_atomic_compare_and_swap(T)(atomic(T) *a, T expected, T desired) \ - { \ - return c89atomic_compare_and_swap_##S(a, expected, desired); \ - } #if defined C89ATOMIC_64BIT AL_ATOMIC_DEFINE(size_t, uint64, 64) @@ -57,14 +59,35 @@ AL_ATOMIC_DEFINE(size_t, uint64, 64) AL_ATOMIC_DEFINE(size_t, uint32, 32) #endif AL_ATOMIC_DEFINE(u64, uint64, 64); +AL_ATOMIC_DEFINE_EXT(u64, uint64, 64); AL_ATOMIC_DEFINE(s64, int64, i64); AL_ATOMIC_DEFINE(u32, uint32, 32); AL_ATOMIC_DEFINE(s32, int32, i32); +AL_ATOMIC_DEFINE_EXT(s32, int32, i32); AL_ATOMIC_DEFINE(u16, uint16, 16); +AL_ATOMIC_DEFINE_EXT(u16, uint16, 16); AL_ATOMIC_DEFINE(s16, int16, i16); AL_ATOMIC_DEFINE(u8, uint8, 8); AL_ATOMIC_DEFINE(s8, int8, i8); AL_ATOMIC_DEFINE(f64, f64, f64); AL_ATOMIC_DEFINE(f32, f32, f32); +static inline f32 _al_atomic_add_f32(atomic(f32) *a, f32 value, s32 order) +{ + (void)a; + (void)value; + (void)order; + al_assert(false); + return 0.f; +} + +static inline f32 _al_atomic_sub_f32(atomic(f32) *a, f32 value, s32 order) +{ + (void)a; + (void)value; + (void)order; + al_assert(false); + return 0.f; +} + #endif // _AL_ATOMIC_H diff --git a/include/al/lib.h b/include/al/lib.h index 5748293..e13a7c7 100644 --- a/include/al/lib.h +++ b/include/al/lib.h @@ -7,7 +7,7 @@ //#define AL_MEMORY_TRACKING //#define AL_FORCE_DISABLE_OUTPUT -#if defined __clang__ || defined __GNUC__ +#if defined __GNUC__ || defined __clang__ #define AL_UNUSED_FUNCTION_PUSH \ _Pragma("GCC diagnostic push") \ _Pragma("GCC diagnostic ignored \"-Wunused-function\"") @@ -25,8 +25,6 @@ #include <stdarg.h> #include <assert.h> #include <ctype.h> -#include <math.h> -#include <time.h> #ifdef AL_MEMORY_TRACKING void al_malloc_init(void); @@ -112,72 +110,15 @@ static inline void *al_memrchr(const void *s, s32 c, size_t n) return NULL; } -AL_UNUSED_FUNCTION_POP -#endif - -#ifndef LIKELY -#ifdef AL_HAVE_BUILTIN_EXPECT -#define LIKELY(x) __builtin_expect((x), 1) -#else -#define LIKELY(x) x -#endif -#endif - -#ifndef UNLIKELY -#ifdef AL_HAVE_BUILTIN_EXPECT -#define UNLIKELY(x) __builtin_expect((x), 0) -#else -#define UNLIKELY(x) x -#endif -#endif - -#if __GNUC__ >= 3 -#define AL_MAX(a, b) \ -({ \ - __typeof__(a) _a = a; \ - __typeof__(b) _b = b; \ - _a > _b ? _a : _b; \ -}) -#define AL_MIN(a, b) \ -({ \ - __typeof__(a) _a = a; \ - __typeof__(b) _b = b; \ - _a < _b ? _a : _b; \ -}) -#else -#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])) - -#define AL_BOOLSTR(b) ((b) ? "true" : "false") - -#ifndef _MSVC_ -#define __al_thread __thread -#else -#define __al_thread __declspec(thread) -#endif - -#define __al_fallthrough __attribute__((fallthrough)) - -#if __has_attribute(__counted_by__) -#define __al_counted_by(member) __attribute__((__counted_by__(member))) -#else -#define __al_counted_by(member) -#endif +static inline char *al_memdup0(const char *s, size_t n) +{ + char *buf = (char *)al_malloc(n + 1); + al_memcpy(buf, s, n); + buf[n] = '\0'; + return buf; +} -#if __has_attribute(__sized_by__) -#define __al_sized_by(member) __attribute__((__sized_by__(member))) -#else -#define __al_sized_by(member) +AL_UNUSED_FUNCTION_POP #endif AL_UNUSED_FUNCTION_PUSH @@ -197,6 +138,26 @@ static inline bool al_isspace(char c) return (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'); } +static inline bool al_isupper(char c) +{ + return isupper(c); +} + +static inline bool al_islower(char c) +{ + return islower(c); +} + +static inline char al_tolower(char c) +{ + return al_isupper(c) ? c - 'A' + 'a' : c; +} + +static inline char al_toupper(char c) +{ + return al_islower(c) ? c - 'a' + 'A' : c; +} + static inline u32 al_u32_inc_wrap(u32 v) { return (v == UINT32_MAX) ? 0 : v + 1; diff --git a/include/al/macros.h b/include/al/macros.h new file mode 100644 index 0000000..b767191 --- /dev/null +++ b/include/al/macros.h @@ -0,0 +1,84 @@ +#ifndef _AL_MACROS_H +#define _AL_MACROS_H + +#if defined LIKELY || defined UNLIKELY +#error "Conflicting definitions for LIKELY/UNLIKELY" +#endif + +#ifdef AL_HAVE_BUILTIN_EXPECT +#define LIKELY(x) __builtin_expect((x), 1) +#else +#define LIKELY(x) x +#endif + +#ifdef AL_HAVE_BUILTIN_EXPECT +#define UNLIKELY(x) __builtin_expect((x), 0) +#else +#define UNLIKELY(x) x +#endif + +#if defined MAX || defined MIN || defined CLAMP || defined SWAP || defined ARRAY_SIZE || defined BOOLSTR +#error "Conflicting definitions for common macros" +#endif + +#define MAX(a, b) \ +({ \ + __typeof__(a) _a = a; \ + __typeof__(b) _b = b; \ + (void)(&_a == &_b); \ + _a > _b ? _a : _b; \ +}) + +#define MIN(a, b) \ +({ \ + __typeof__(a) _a = a; \ + __typeof__(b) _b = b; \ + (void)(&_a == &_b); \ + _a < _b ? _a : _b; \ +}) + +#define CLAMP(n, lo, hi) MIN(hi, MAX(lo, n)) + +#define SWAP(a, b) do { \ + __typeof__(a) tmp = a; \ + a = b; \ + b = tmp; \ +} while (0) + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) + +#define BOOLSTR(b) ((b) ? "true" : "false") + +#if defined KB || defined MB || defined GB || defined TB || defined PB || defined EB +#error "Conflicting definitions for byte size macros" +#endif + +// http://www.blackbeltcoder.com/Articles/controls/precisely-defining-kilobytes-megabytes-and-gigabytes +#define KB(n) (((u64)0x400)*((u64)(n))) +#define MB(n) (((u64)0x100000)*((u64)(n))) +#define GB(n) (((u64)0x40000000)*((u64)(n))) +#define TB(n) (((u64)0x10000000000)*((u64)(n))) +#define PB(n) (((u64)0x4000000000000)*((u64)(n))) +#define EB(n) (((u64)0x1000000000000000)*((u64)(n))) + +#if defined __counted_by || defined __sized_by +#error "Conflicting definitions for common attributes" +#endif + +#if __has_attribute(__counted_by__) +#define __counted_by(member) __attribute__((__counted_by__(member))) +#else +#define __counted_by(member) +#endif + +#if __has_attribute(__sized_by__) +#define __sized_by(member) __attribute__((__sized_by__(member))) +#else +#define __sized_by(member) +#endif + +#ifdef _MSVC_ +#define __thread __declspec(thread) +#endif + +#endif // _AL_MACROS_H diff --git a/include/al/random.h b/include/al/random.h index 31da3f3..358a915 100644 --- a/include/al/random.h +++ b/include/al/random.h @@ -2,7 +2,8 @@ #define _AL_RANDOM_H #include "lib.h" -#include "atomic.h" + +#include <time.h> #define AL_RAND_MAX RAND_MAX @@ -18,16 +19,6 @@ static s32 al_rand(void) return rand(); } -// Each file that includes this header will have a separate counter. -static atomic(u16) _al_inc_value = 0; - -// This is apart of random because currently al_inc_u16() is used as a substitute -// for proper random id generation. -static u16 al_inc_u16(void) -{ - return al_atomic_add(u16)(&_al_inc_value, 1, AL_ATOMIC_RELEASE); -} - AL_UNUSED_FUNCTION_POP #endif // _AL_RANDOM_H diff --git a/include/al/str.h b/include/al/str.h index b06bf57..0878f5b 100644 --- a/include/al/str.h +++ b/include/al/str.h @@ -2,11 +2,12 @@ #define _AL_STR_H #include "lib.h" +#include "macros.h" typedef struct { u32 len; u32 alloc; - char *data; + char *__sized_by(alloc) data; } str; #define al_str_zero() (str){ 0, 0, NULL } @@ -22,7 +23,7 @@ typedef struct { #define AL_STR_FMT "%.*s" #define AL_STR_PRINTF(s) (s)->len, (s)->data -#define AL_STR_PRINTF_MAXLEN(s, l) AL_MIN((s)->len, (l)), (s)->data +#define AL_STR_PRINTF_MAXLEN(s, l) MIN((s)->len, (l)), (s)->data AL_UNUSED_FUNCTION_PUSH @@ -79,7 +80,7 @@ static inline void al_str_from(str *s, const char *c_str) static inline void al_str_to_lower(str *s) { for (u32 i = 0; i < s->len; i++) { - al_str_at(s, i) = tolower(al_str_at(s, i)); + al_str_at(s, i) = al_tolower(al_str_at(s, i)); } } @@ -123,7 +124,7 @@ static inline s32 al_str_cmp(str *s, str *c, u32 start, u32 len) static inline bool al_str_eq(str *s, str *c) { - return al_str_cmp(s, c, 0, AL_MAX(s->len, c->len)) == 0; + return al_str_cmp(s, c, 0, MAX(s->len, c->len)) == 0; } static inline s32 al_str_find(str *s, char c) diff --git a/include/al/types.h b/include/al/types.h index cf3d0e8..fc2bb10 100644 --- a/include/al/types.h +++ b/include/al/types.h @@ -21,6 +21,7 @@ #include <stdbool.h> #include <stdint.h> #include <float.h> +#include <limits.h> #include <sys/types.h> typedef int8_t s8; diff --git a/include/al/wstr.h b/include/al/wstr.h index 8113575..444cc32 100644 --- a/include/al/wstr.h +++ b/include/al/wstr.h @@ -9,7 +9,7 @@ typedef struct { u32 len; u32 alloc; - wchar_t *data; + wchar_t *__sized_by(alloc) data; } wstr; #define al_wstr_zero() (wstr){ 0, 0, NULL } |