Compare commits

...

4 Commits

Author SHA1 Message Date
db8389f7d5 scratch arena thread safety 2025-06-28 01:29:22 +02:00
5e6291cdc6 Fix typo 2025-06-27 23:06:07 +02:00
596bb1ef76 RULE_ARRAY() to define deps with a static array 2025-06-27 20:10:38 +02:00
595b7d06f1 alias sb_read_file_alloc() 2025-06-25 13:54:52 +02:00

66
gebs.h
View File

@ -90,6 +90,10 @@ int main(int argc, char ** argv)
#include <string.h>
#include <limits.h>
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
# include <pthread.h>
#endif
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
@ -400,6 +404,21 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv,
__ok; \
})
#define gebs_needs_rebuild_many_array(out, array) \
({ \
bool __ok = false; \
if (sizeof((array))/sizeof((array)[0]) == 0) { \
__ok = true; \
} \
for (size_t __i = 0; __i < sizeof((array))/sizeof((array)[0]); __i++) { \
if (gebs_needs_rebuild((out), (array)[__i])) { \
__ok = true; \
break; \
} \
} \
__ok; \
})
#define gebs_make_compile_flags(...) \
do { \
if (gebs_needs_rebuild("compile_flags.txt", __FILE__)) { \
@ -419,12 +438,20 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv,
} while(0)
#define GEBS_RULE(target, ...) if (needs_rebuild_many(target, __VA_ARGS__))
#define GEBS_RULE_ARRAY(target, array) if (gebs_needs_rebuild_many_array(target, (array)))
// ----------------------------------------------------------------------------
// Scratch arena
// ----------------------------------------------------------------------------
extern Gebs_Arena gebs_scratch_arena;
typedef struct {
Gebs_Arena value;
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_t lock;
#endif
} Gebs_Scratch_Arena;
extern Gebs_Scratch_Arena gebs_scratch_arena;
void gebs_scratch_arena_reset(void);
char *gebs_fmt(const char *fmt, ...);
@ -800,22 +827,25 @@ void gebs_arena_destroy(Gebs_Arena *a)
// Scratch arena
// ----------------------------------------------------------------------------
Gebs_Arena gebs_scratch_arena;
Gebs_Scratch_Arena gebs_scratch_arena;
__attribute__((constructor))
void gebs_scratch_areana_init(void)
{
gebs_scratch_arena = gebs_arena_get();
gebs_scratch_arena.value = gebs_arena_get();
}
__attribute__((destructor))
void gebs_scratch_areana_free(void)
{
gebs_arena_destroy(&gebs_scratch_arena);
gebs_arena_destroy(&gebs_scratch_arena.value);
}
char *gebs_fmt(const char *fmt, ...)
{
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_lock(&gebs_scratch_arena.lock);
#endif
va_list list;
va_start(list, fmt);
@ -824,23 +854,41 @@ char *gebs_fmt(const char *fmt, ...)
size_t size = vsnprintf(nil, 0, fmt, list_copy);
va_end(list_copy);
char *buf = gebs_malloc(&gebs_scratch_arena, size+1);
char *buf = gebs_malloc(&gebs_scratch_arena.value, size+1);
vsprintf(buf, fmt, list);
va_end(list);
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_unlock(&gebs_scratch_arena.lock);
#endif
return buf;
}
void gebs_scratch_arena_reset(void)
{
gebs_arena_reset(&gebs_scratch_arena);
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_lock(&gebs_scratch_arena.lock);
#endif
gebs_arena_reset(&gebs_scratch_arena.value);
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_unlock(&gebs_scratch_arena.lock);
#endif
}
char *gebs_cwd(void)
{
char *cwd = gebs_malloc(&gebs_scratch_arena, PATH_MAX);
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_lock(&gebs_scratch_arena.lock);
#endif
char *cwd = gebs_malloc(&gebs_scratch_arena.value, PATH_MAX);
if (getcwd(cwd, PATH_MAX) == nil) {
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_unlock(&gebs_scratch_arena.lock);
#endif
return nil;
}
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_unlock(&gebs_scratch_arena.lock);
#endif
return cwd;
}
@ -971,6 +1019,7 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv,
#define sb_finish gebs_sb_finish
#define sb_append_nstr gebs_sb_append_nstr
#define sb_read_file gebs_sb_read_file
#define sb_read_file_alloc gebs_sb_read_file_alloc
#define rename1 gebs_rename
#define remove1 gebs_remove
@ -979,7 +1028,7 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv,
#define Cmd Gebs_Cmd
#define cmd_append_alloc gebs_cmd_append_alloc
#define cmd_free_aloc gebs_cmd_free_alloc
#define cmd_free_alloc gebs_cmd_free_alloc
#define cmd_append gebs_cmd_append
#define cmd_free gebs_cmd_free
#define cmd_run_alloc gebs_cmd_run_alloc
@ -1003,6 +1052,7 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv,
#define rebuild_self gebs_rebuild_self
#define make_compile_flags gebs_make_compile_flags
#define RULE GEBS_RULE
#define RULE_ARRAY GEBS_RULE_ARRAY
#define scratch_arena gebs_scratch_arena
#define fmt gebs_fmt