This commit is contained in:
snarmph 2025-10-21 10:30:53 +02:00
commit df0187bdb3
3 changed files with 587 additions and 104 deletions

126
colla.h
View file

@ -40,7 +40,13 @@ extern void *memmove(void *dst, const void *src, size_t size);
#define static_assert(cond, ...) _Static_assert(cond, "" __VA_ARGS__)
/////////////////////////////////////////////////
typedef enum {
COLLA_DARRAY_BLOCK_SIZE = 64,
COLLA_RG_MAX_MATCHES = 16,
COLLA_OS_ARENA_SIZE = 1 << 20, // MB(1)
COLLA_OS_MAX_WAITABLE_HANDLES = 256,
COLLA_LOG_MAX_CALLBACKS = 22,
} colla_constants_e;
// CORE MODULES /////////////////////////////////
@ -278,8 +284,6 @@ for_each (chunk, arr) {
}
*/
#define DARRAY_DEFAULT_BLOCK_SIZE (64)
#define darr_define(struct_name, item_type) typedef struct struct_name struct_name; \
struct struct_name { \
item_type *items; \
@ -292,7 +296,7 @@ for_each (chunk, arr) {
#define darr__alloc_first(arena, arr) do { \
(arr) = (arr) ? (arr) : alloc(arena, typeof(*arr)); \
(arr)->head = (arr)->head ? (arr)->head : (arr); \
(arr)->block_size = (arr)->block_size ? (arr)->block_size : DARRAY_DEFAULT_BLOCK_SIZE; \
(arr)->block_size = (arr)->block_size ? (arr)->block_size : COLLA_DARRAY_BLOCK_SIZE; \
(arr)->items = alloc(arena, typeof(*arr->items), arr->block_size); \
colla_assert((arr)->count == 0); \
} while (0)
@ -433,6 +437,7 @@ strview_t strv_init_str(str_t str);
bool strv_is_empty(strview_t ctx);
bool strv_equals(strview_t a, strview_t b);
int strv_compare(strview_t a, strview_t b);
usize strv_get_utf8_len(strview_t v);
char strv_front(strview_t ctx);
char strv_back(strview_t ctx);
@ -587,6 +592,26 @@ bool ibstr_get_i16(ibstream_t *ib, i16 *out);
bool ibstr_get_i32(ibstream_t *ib, i32 *out);
bool ibstr_get_i64(ibstream_t *ib, i64 *out);
// SIMPLE REGEX /////////////////////////////////
// only supports *, every star matches until the following character
// is found, e.g.
// ab*e
// abcde
// matches (cd)
typedef struct rg_match_t rg_match_t;
struct rg_match_t {
strview_t text[COLLA_RG_MAX_MATCHES];
int count;
bool matches;
};
rg_match_t rg_match(strview_t rg, strview_t text);
bool rg_match_easy(strview_t rg, strview_t text);
/////////////////////////////////////////////////
// ARENA ////////////////////////////////////////
#if COLLA_WIN && !COLLA_TCC
@ -658,7 +683,6 @@ void arena_pop(arena_t *arena, usize amount);
// OS LAYER /////////////////////////////////////
#define OS_ARENA_SIZE (MB(1))
#define OS_WAIT_INFINITE (0xFFFFFFFF)
typedef struct oshandle_t oshandle_t;
@ -666,11 +690,20 @@ struct oshandle_t {
uptr data;
};
typedef enum {
OS_ARCH_X86,
OS_ARCH_ARM,
OS_ARCH_IA64,
OS_ARCH_AMD64,
OS_ARCH_ARM64,
} os_arch_e;
typedef struct os_system_info_t os_system_info_t;
struct os_system_info_t {
u32 processor_count;
u64 page_size;
str_t machine_name;
os_arch_e architecture;
};
void os_init(void);
@ -688,8 +721,6 @@ oshandle_t os_handle_zero(void);
bool os_handle_match(oshandle_t a, oshandle_t b);
bool os_handle_valid(oshandle_t handle);
#define OS_MAX_WAITABLE_HANDLES 256
typedef enum {
OS_WAIT_FINISHED,
OS_WAIT_ABANDONED,
@ -741,8 +772,36 @@ typedef enum os_log_colour_e {
LOG_COL__COUNT,
} os_log_colour_e;
void os_log_print(os_log_level_e level, const char *fmt, ...);
void os_log_printv(os_log_level_e level, const char *fmt, va_list args);
typedef struct log_event_t log_event_t;
struct log_event_t {
va_list args;
const char *fmt;
const char *file;
int line;
struct tm *time;
os_log_level_e level;
void *udata;
};
typedef struct log_callback_t log_callback_t;
struct log_callback_t {
void (*fn)(log_event_t *ev);
void *udata;
os_log_level_e level;
};
typedef enum {
OS_LOG_DEFAULT = 0,
OS_LOG_NOTIME = 1 << 0,
OS_LOG_NOFILE = 1 << 1,
OS_LOG_NOCRASH = 1 << 2,
OS_LOG_SIMPLE = OS_LOG_NOTIME | OS_LOG_NOFILE,
} os_log_options_e;
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, ...);
void os_log_printv(const char *file, int line, os_log_level_e level, const char *fmt, va_list args);
void os_log_set_colour(os_log_colour_e colour);
void os_log_set_colour_bg(os_log_colour_e foreground, os_log_colour_e background);
@ -750,12 +809,12 @@ oshandle_t os_stdout(void);
oshandle_t os_stdin(void);
#define print(...) fmt_print(__VA_ARGS__)
#define println(...) os_log_print(LOG_BASIC, __VA_ARGS__)
#define debug(...) os_log_print(LOG_DEBUG, __VA_ARGS__)
#define info(...) os_log_print(LOG_INFO, __VA_ARGS__)
#define warn(...) os_log_print(LOG_WARN, __VA_ARGS__)
#define err(...) os_log_print(LOG_ERR, __VA_ARGS__)
#define fatal(...) os_log_print(LOG_FATAL, __VA_ARGS__)
#define println(...) os_log_print(__FILE__, __LINE__, LOG_BASIC, __VA_ARGS__)
#define debug(...) os_log_print(__FILE__, __LINE__, LOG_DEBUG, __VA_ARGS__)
#define info(...) os_log_print(__FILE__, __LINE__, LOG_INFO, __VA_ARGS__)
#define warn(...) os_log_print(__FILE__, __LINE__, LOG_WARN, __VA_ARGS__)
#define err(...) os_log_print(__FILE__, __LINE__, LOG_ERR, __VA_ARGS__)
#define fatal(...) os_log_print(__FILE__, __LINE__, LOG_FATAL, __VA_ARGS__)
// == FILE ======================================
@ -895,6 +954,39 @@ void os_cond_broadcast(oshandle_t cond);
void os_cond_wait(oshandle_t cond, oshandle_t mutex, int milliseconds);
// == JOB QUEUE =================================
typedef void (job_func_f)(void *userdata);
typedef struct job_t job_t;
struct job_t {
job_t *next;
job_func_f *func;
void *userdata;
};
typedef struct job_queue_t job_queue_t;
struct job_queue_t {
job_t *jobs;
job_t *freelist;
bool should_stop;
bool stop_when_finished;
int reader;
int writer;
oshandle_t mutex;
oshandle_t condvar;
oshandle_t *threads;
int thread_count;
};
// pass 0 to worker count to use max workers (os_get_system_info().processor_count)
job_queue_t *jq_init(arena_t *arena, int worker_count);
void jq_stop(job_queue_t *queue);
// no need to call this if you call jq_stop
void jq_cleanup(job_queue_t *queue);
void jq_push(arena_t *arena, job_queue_t *queue, job_func_f *func, void *userdata);
job_t *jq_pop_job(job_queue_t *queue);
#endif
// PARSERS //////////////////////////////////////
@ -971,7 +1063,6 @@ struct ini_pretty_opts_t {
void ini_pretty_print(ini_t *ini, const ini_pretty_opts_t *options);
// == JSON ===========================================
typedef enum jsontype_e {
@ -987,6 +1078,7 @@ typedef enum jsonflags_e {
JSON_DEFAULT = 0,
JSON_NO_TRAILING_COMMAS = 1 << 0,
JSON_NO_COMMENTS = 1 << 1,
JSON_ONLY_OBJECT_START = 1 << 2,
} jsonflags_e;
typedef struct json_t json_t;
@ -1146,6 +1238,7 @@ http_res_t http_parse_res(arena_t *arena, strview_t response);
str_t http_req_to_str(arena_t *arena, http_req_t *req);
str_t http_res_to_str(arena_t *arena, http_res_t *res);
http_header_t *http_add_header(arena_t *arena, http_header_t *headers, strview_t key, strview_t value);
bool http_has_header(http_header_t *headers, strview_t key);
void http_set_header(http_header_t *headers, strview_t key, strview_t value);
strview_t http_get_header(http_header_t *headers, strview_t key);
@ -1287,5 +1380,6 @@ void pretty_printv(arena_t scratch, const char *fmt, va_list args);
str_t pretty_print_get_string(arena_t *arena, const char *fmt, ...);
str_t pretty_print_get_stringv(arena_t *arena, const char *fmt, va_list args);
usize pretty_print_get_length(strview_t view);
#endif