Compare commits
2 Commits
596bb1ef76
...
db8389f7d5
Author | SHA1 | Date | |
---|---|---|---|
db8389f7d5 | |||
5e6291cdc6 |
48
gebs.h
48
gebs.h
@ -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>
|
||||
@ -440,7 +444,14 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv,
|
||||
// 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, ...);
|
||||
@ -816,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);
|
||||
|
||||
@ -840,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;
|
||||
}
|
||||
|
||||
@ -996,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
|
||||
|
Reference in New Issue
Block a user