fixed arena + some niceties

This commit is contained in:
snarmph 2025-10-21 10:31:58 +02:00
parent df0187bdb3
commit b601ace92b
3 changed files with 258 additions and 54 deletions

110
colla.h
View file

@ -1,37 +1,24 @@
#ifndef COLLA_HEADER
#define COLLA_HEADER
#if defined(_WIN32)
#define COLLA_WIN 1
#define COLLA_OSX 0
#define COLLA_LIN 0
#define COLLA_EMC 0
#elif defined(__EMSCRIPTEN__)
#define COLLA_WIN 0
#define COLLA_OSX 0
#define COLLA_LIN 0
#define COLLA_EMC 1
#elif defined(__linux__)
#define COLLA_WIN 0
#define COLLA_OSX 0
#define COLLA_LIN 1
#define COLLA_EMC 0
#elif defined(__APPLE__)
#define COLLA_WIN 0
#define COLLA_OSX 1
#define COLLA_LIN 0
#define COLLA_EMC 0
#endif
#if COLLA_LIN
#define _FILE_OFFSET_BITS 1
#endif
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdarg.h>
#include <uchar.h>
// when where compiling with tcc, we need to do a couple things outselves
#ifdef __TINYC__
typedef unsigned short char16_t;
// NOT SAFE, afaik tcc doesn't support thread_local
#ifndef thread_local
#define thread_local
#endif
#else
#include <uchar.h>
#ifndef thread_local
#define thread_local _Thread_local
#endif
#endif
// LIBC FUNCTIONS ///////////////////////////////
@ -72,6 +59,28 @@ void colla_cleanup(void);
#define COLLA_RELEASE 1
#endif
#if defined(_WIN32)
#define COLLA_WIN 1
#define COLLA_OSX 0
#define COLLA_LIN 0
#define COLLA_EMC 0
#elif defined(__EMSCRIPTEN__)
#define COLLA_WIN 0
#define COLLA_OSX 0
#define COLLA_LIN 0
#define COLLA_EMC 1
#elif defined(__linux__)
#define COLLA_WIN 0
#define COLLA_OSX 0
#define COLLA_LIN 1
#define COLLA_EMC 0
#elif defined(__APPLE__)
#define COLLA_WIN 0
#define COLLA_OSX 1
#define COLLA_LIN 0
#define COLLA_EMC 0
#endif
#if defined(__COSMOPOLITAN__)
#define COLLA_COSMO 1
#else
@ -536,6 +545,7 @@ strview_t istr_get_view(instream_t *ctx, char delim);
strview_t istr_get_view_either(instream_t *ctx, strview_t chars);
strview_t istr_get_view_len(instream_t *ctx, usize len);
strview_t istr_get_line(instream_t *ctx);
strview_t istr_get_word(instream_t *ctx);
// OUTPUT STREAM ////////////////////////////////
@ -554,6 +564,7 @@ char ostr_back(outstream_t *ctx);
str_t ostr_to_str(outstream_t *ctx);
strview_t ostr_as_view(outstream_t *ctx);
void ostr_rewind(outstream_t *ctx, usize from_beg);
void ostr_pop(outstream_t *ctx, usize count);
void ostr_print(outstream_t *ctx, const char *fmt, ...);
@ -798,6 +809,8 @@ typedef enum {
OS_LOG_SIMPLE = OS_LOG_NOTIME | OS_LOG_NOFILE,
} os_log_options_e;
void os_log_set_options(os_log_options_e opt);
os_log_options_e os_log_get_options(void);
void os_log_add_callback(log_callback_t cb);
void os_log_add_fp(oshandle_t fp, os_log_level_e level);
void os_log_print(const char *file, int line, os_log_level_e level, const char *fmt, ...);
@ -808,6 +821,10 @@ void os_log_set_colour_bg(os_log_colour_e foreground, os_log_colour_e background
oshandle_t os_stdout(void);
oshandle_t os_stdin(void);
// windows specific
oshandle_t os_win_conout(void);
oshandle_t os_win_conin(void);
#define print(...) fmt_print(__VA_ARGS__)
#define println(...) os_log_print(__FILE__, __LINE__, LOG_BASIC, __VA_ARGS__)
#define debug(...) os_log_print(__FILE__, __LINE__, LOG_DEBUG, __VA_ARGS__)
@ -823,6 +840,8 @@ typedef enum filemode_e {
OS_FILE_WRITE = 1 << 1,
} filemode_e;
str_t os_path_join(arena_t *arena, strview_t left, strview_t right);
bool os_file_exists(strview_t filename);
bool os_dir_exists(strview_t folder);
bool os_file_or_dir_exists(strview_t path);
@ -927,6 +946,22 @@ usize os_pad_to_page(usize byte_count);
// == THREAD ====================================
#ifndef thread_local
#define thread_local _Thread_local
#endif
typedef struct os_barrier_t os_barrier_t;
struct os_barrier_t {
i64 thread_count;
i64 thread_value;
i64 has_completed;
};
extern thread_local i64 os_thread_id;
extern i64 os_thread_count;
void os_barrier_sync(os_barrier_t *b);
typedef int (thread_func_t)(u64 thread_id, void *userdata);
oshandle_t os_thread_launch(thread_func_t func, void *userdata);
@ -935,6 +970,14 @@ bool os_thread_join(oshandle_t thread, int *code);
u64 os_thread_get_id(oshandle_t thread);
typedef struct i64range_t i64range_t;
struct i64range_t {
i64 min;
i64 max;
};
i64range_t os_lane_range(u64 values_count);
// == MUTEX =====================================
oshandle_t os_mutex_create(void);
@ -976,6 +1019,7 @@ struct job_queue_t {
oshandle_t mutex;
oshandle_t condvar;
oshandle_t *threads;
i64 *thread_ids;
int thread_count;
};
@ -989,6 +1033,18 @@ job_t *jq_pop_job(job_queue_t *queue);
#endif
// == ATOMICS ========================================
i64 atomic_set_i64(i64 *dest, i64 val);
i64 atomic_add_i64(i64 *dest, i64 val);
i64 atomic_and_i64(i64 *dest, i64 val);
// if (*dest == cmp) *dest = val
i64 atomic_cmp_i64(i64 *dest, i64 val, i64 cmp);
i64 atomic_inc_i64(i64 *dest);
i64 atomic_dec_i64(i64 *dest);
i64 atomic_or_i64(i64 *dest, i64 val);
i64 atomic_xor_i64(i64 *dest, i64 val);
// PARSERS //////////////////////////////////////
// == INI ============================================