Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion example/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ void on_deinit(sp_app_t* app) {
}

sp_app_config_t app_main(s32 num_args, const c8** args) {
sp_mem_t a = sp_mem_os_new();
sp_mem_heap_t* heap = sp_mem_heap_new();
sp_mem_t a = sp_mem_heap_as_allocator(heap);
state_t* state = sp_alloc_type(a, state_t);
state->allocator = a;

Expand Down
4 changes: 3 additions & 1 deletion example/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include "sp.h"

s32 run(s32 num_args, const c8** args) {
sp_da(u32) years = sp_da_new(sp_mem_os_new(), u32);
sp_mem_heap_t* heap = sp_mem_heap_new();
sp_mem_t mem = sp_mem_heap_as_allocator(heap);
sp_da(u32) years = sp_da_new(mem, u32);
sp_da_push(years, 1969);
sp_da_push(years, 1972);
sp_da_for(years, it) {
Expand Down
3 changes: 2 additions & 1 deletion example/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ void specifier(s64 n, c8 fill, sp_fmt_align_t align, u8 width) {
}

s32 run(s32 num_args, const c8** args) {
sp_mem_t mem = sp_mem_os_new();
sp_mem_heap_t* heap = sp_mem_heap_new();
sp_mem_t mem = sp_mem_heap_as_allocator(heap);

sp_log("{.green} has Zig/Rust style format specifiers (fill, align, width), plus named directives which may:", sp_fmt_cstr("sp.h"));
sp_log("- {} text from a format argument", sp_fmt_cstr("render"));
Expand Down
3 changes: 2 additions & 1 deletion example/hash_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ void header(const c8* key, const c8* value) {
}

s32 run(s32 num_args, const c8** args) {
sp_mem_t a = sp_mem_os_new();
sp_mem_heap_t* heap = sp_mem_heap_new();
sp_mem_t a = sp_mem_heap_as_allocator(heap);

struct {
sp_ht(s32, u32) integer;
Expand Down
3 changes: 2 additions & 1 deletion example/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include "sp.h"

s32 run(s32 num_args, const c8** args) {
sp_mem_t mem = sp_mem_os_new();
sp_mem_heap_t* heap = sp_mem_heap_new();
sp_mem_t mem = sp_mem_heap_as_allocator(heap);

sp_io_file_reader_t r = sp_zero;

Expand Down
3 changes: 2 additions & 1 deletion example/ls.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ s32 compare_entries(const void* pa, const void* pb) {
}

s32 run(s32 num_args, const c8** args) {
sp_mem_t mem = sp_mem_os_new();
sp_mem_heap_t* heap = sp_mem_heap_new();
sp_mem_t mem = sp_mem_heap_as_allocator(heap);
sp_str_t cwd = sp_fs_get_cwd(mem);
sp_str_t dir = cwd;
if (num_args == 2) dir = sp_fs_join_path(mem, cwd, sp_str_view(args[1]));
Expand Down
4 changes: 3 additions & 1 deletion example/msvc.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ s32 msvc_main(s32 argc, const c8** argv) {
(void)argv;

sp_msvc_t result = sp_zero;
sp_msvc_err_t err = sp_msvc_find(sp_mem_os_new(), SP_MSVC_ARCH_X64, &result);
sp_mem_heap_t* heap = sp_mem_heap_new();
sp_mem_t mem = sp_mem_heap_as_allocator(heap);
sp_msvc_err_t err = sp_msvc_find(mem, SP_MSVC_ARCH_X64, &result);

if (err) {
sp_log("sp_msvc_find failed: {}", sp_fmt_int(err));
Expand Down
3 changes: 2 additions & 1 deletion example/palette.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ sp_app_config_t app_main(s32 num_args, const c8** args) {
if (num_args > 2) saturation = sp_parse_s32(sp_str_view(args[2]));
if (num_args > 3) value = sp_parse_s32(sp_str_view(args[3]));

sp_mem_t mem = sp_mem_os_new();
sp_mem_heap_t* heap = sp_mem_heap_new();
sp_mem_t mem = sp_mem_heap_as_allocator(heap);
app_t* app = sp_alloc_type(mem, app_t);
app->mem = mem;
app->saved_colors = sp_da_new(app->mem, sp_color_t);
Expand Down
5 changes: 3 additions & 2 deletions example/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ s32 prompt_main(s32 argc, const c8** argv) {
{ "Knight Rider", demo_knight_rider },
};

sp_mem_t mem = sp_mem_os_new();
sp_mem_heap_t* heap = sp_mem_heap_new();
sp_mem_t mem = sp_mem_heap_as_allocator(heap);

sp_ht(const c8*, sp_prompt_demo_fn_t) demos = sp_zero;
sp_cstr_ht_init(mem, demos);
Expand All @@ -398,7 +399,7 @@ s32 prompt_main(s32 argc, const c8** argv) {
run = *fn;
}

sp_prompt_ctx_t* ctx = sp_prompt_begin(sp_mem_os_new());
sp_prompt_ctx_t* ctx = sp_prompt_begin(mem);
if (!ctx) {
return SP_PROMPT_ERROR;
}
Expand Down
4 changes: 3 additions & 1 deletion example/prompt_fancy.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,9 @@ static sp_str_t fancy_plan_note(sp_mem_t mem, const c8* kind, const c8* name, co

s32 fancy_main(s32 argc, const c8** argv) {
bool scripted = fancy_has_arg(argc, argv, "--scripted");
sp_prompt_ctx_t* ctx = sp_prompt_begin(sp_mem_os_new());
sp_mem_heap_t* heap = sp_mem_heap_new();
sp_mem_t mem = sp_mem_heap_as_allocator(heap);
sp_prompt_ctx_t* ctx = sp_prompt_begin(mem);
if (ctx == SP_NULLPTR) {
sp_log("failed to initialize prompt");
return 1;
Expand Down
3 changes: 2 additions & 1 deletion example/wc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ s32 run(s32 num_args, const c8** args) {
return 1;
}

sp_mem_t mem = sp_mem_os_new();
sp_mem_heap_t* heap = sp_mem_heap_new();
sp_mem_t mem = sp_mem_heap_as_allocator(heap);

sp_str_t cwd = sp_fs_get_cwd(mem);
sp_str_t path = sp_fs_join_path(mem, cwd, sp_str_view(args[1]));
Expand Down
4 changes: 2 additions & 2 deletions example/zero_copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ static void report(const c8* label, run_t run) {
}

s32 run(s32 num_args, const c8** args) {
(void)num_args; (void)args;
sp_mem_t mem = sp_mem_arena_as_allocator(sp_mem_arena_new(sp_mem_os_new()));
sp_mem_heap_t* heap = sp_mem_heap_new();
sp_mem_t mem = sp_mem_heap_as_allocator(heap);

sp_str_t cwd = sp_fs_get_cwd(mem);
sp_str_t src = sp_str_concat(mem, cwd, sp_str_lit("/io.src"));
Expand Down
30 changes: 20 additions & 10 deletions sp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3158,7 +3158,13 @@ SP_TYPEDEF_FN(void, sp_tls_once_fn_t);
SP_TYPEDEF_FN(void, sp_tls_deinit_fn_t, void*);

typedef struct {
sp_mem_t mem;
struct {
sp_mem_heap_t* heap;
} allocators;
struct {
sp_mem_t heap;
sp_mem_t page;
} mem;
sp_mem_arena_t* scratch [2];
sp_env_t env;
struct {
Expand Down Expand Up @@ -7117,7 +7123,7 @@ void* sp_rb_grow_ex(void* arr, u32 stride, u32 capacity) {
// @format
void sp_fmt_directive_register(const c8* name, sp_fmt_directive_t directive) {
sp_tls_rt_t* tls = sp_tls_rt_get();
sp_str_t id = sp_str_from_cstr(tls->mem, name);
sp_str_t id = sp_str_from_cstr(tls->mem.heap, name);
sp_str_ht_insert(tls->format.directives, id, directive);
}

Expand Down Expand Up @@ -7993,12 +7999,13 @@ void sp_rt_init() {
void sp_tls_rt_deinit(void* ptr) {
if (!ptr) return;
sp_tls_rt_t* tls = (sp_tls_rt_t*)ptr;
if (tls->std.out) sp_mem_allocator_free(tls->mem, tls->std.out, sizeof(sp_io_stream_writer_t));
if (tls->std.err) sp_mem_allocator_free(tls->mem, tls->std.err, sizeof(sp_io_stream_writer_t));
if (tls->std.out) sp_mem_allocator_free(tls->mem.heap, tls->std.out, sizeof(sp_io_stream_writer_t));
if (tls->std.err) sp_mem_allocator_free(tls->mem.heap, tls->std.err, sizeof(sp_io_stream_writer_t));
sp_str_ht_free(tls->format.directives);
sp_carr_for(tls->scratch, it) {
sp_mem_arena_destroy(tls->scratch[it]);
}
sp_mem_heap_destroy(tls->allocators.heap);
sp_sys_free(tls, sizeof(sp_tls_rt_t));
}

Expand All @@ -8011,16 +8018,18 @@ sp_tls_rt_t* sp_tls_rt_get() {
// before doing anything else so you can call functions that allocate
// while initializing the other TLS stuff.
tls = sp_sys_alloc_type(sp_tls_rt_t);
tls->mem = sp_mem_os_new();
tls->allocators.heap = sp_mem_heap_new();
tls->mem.heap = sp_mem_heap_as_allocator(tls->allocators.heap);
tls->mem.page = sp_mem_os_new();
sp_tls_set(sp_rt.tls.key, tls);

sp_carr_for(tls->scratch, it) {
tls->scratch[it] = sp_mem_arena_new(tls->mem);
tls->scratch[it] = sp_mem_arena_new(tls->mem.page);
}
// std.out/std.err are wired lazily (see sp_tls_std_out/_err). Wiring them here
// would take the address of sp_io_stream_writer_write in code that sp_main always
// reaches, which on WASM forces a fd_write import even for programs that never write.
sp_str_ht_init(tls->mem, tls->format.directives);
sp_str_ht_init(tls->mem.heap, tls->format.directives);
sp_fmt_register_builtins();
sp_sys_tls_init(tls);
}
Expand All @@ -8029,15 +8038,15 @@ sp_tls_rt_t* sp_tls_rt_get() {

sp_io_writer_t* sp_tls_std_out(sp_tls_rt_t* tls) {
if (!tls->std.out) {
tls->std.out = sp_alloc_type(tls->mem, sp_io_stream_writer_t);
tls->std.out = sp_alloc_type(tls->mem.heap, sp_io_stream_writer_t);
sp_io_stream_writer_from_fd(tls->std.out, sp_sys_stdout, SP_IO_CLOSE_MODE_NONE);
}
return &tls->std.out->base;
}

sp_io_writer_t* sp_tls_std_err(sp_tls_rt_t* tls) {
if (!tls->std.err) {
tls->std.err = sp_alloc_type(tls->mem, sp_io_stream_writer_t);
tls->std.err = sp_alloc_type(tls->mem.heap, sp_io_stream_writer_t);
sp_io_stream_writer_from_fd(tls->std.err, sp_sys_stderr, SP_IO_CLOSE_MODE_NONE);
}
return &tls->std.err->base;
Expand Down Expand Up @@ -14356,7 +14365,8 @@ SP_API s32 sp_app_run_free(sp_app_t* app) {
}

SP_API s32 sp_app_run(sp_app_config_t config) {
sp_mem_t mem = sp_mem_os_new();
sp_mem_heap_t* heap = sp_mem_heap_new();
sp_mem_t mem = sp_mem_heap_as_allocator(heap);
sp_app_t* app = sp_app_new(mem, config);
s32 rc = (app->mode == SP_APP_MODE_FREE)
? sp_app_run_free(app)
Expand Down
18 changes: 16 additions & 2 deletions test/cv.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
#include "utest.h"
SP_TEST_MAIN()

struct cv {
sp_mem_heap_t* heap;
sp_mem_t mem;
};

UTEST_F_SETUP(cv) {
ut.heap = sp_mem_heap_new();
ut.mem = sp_mem_heap_as_allocator(ut.heap);
}

UTEST_F_TEARDOWN(cv) {
sp_mem_heap_destroy(ut.heap);
}

UTEST(cv, init_destroy) {
SKIP_ON_WASM()
SKIP_ON_FREESTANDING()
Expand Down Expand Up @@ -339,13 +353,13 @@ s32 consumer_fn(void* userdata) {
return 0;
}

UTEST(cv, multithread_producer_consumer) {
UTEST_F(cv, multithread_producer_consumer) {
SKIP_ON_WASM()
SKIP_ON_FREESTANDING()
sp_cv_t cv = sp_zero;
sp_mutex_t mutex = sp_zero;
sp_rb(s32) buffer = SP_NULLPTR;
sp_rb_init(sp_mem_os_new(), buffer);
sp_rb_init(ut.mem, buffer);

sp_cv_init(&cv);
sp_mutex_init(&mutex, SP_MUTEX_PLAIN);
Expand Down
7 changes: 5 additions & 2 deletions test/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ SP_TEST_MAIN()
#endif

struct env {
sp_mem_heap_t* heap;
sp_mem_t mem;
};

UTEST_F_SETUP(env) {
ut.mem = sp_mem_os_new();
ut.heap = sp_mem_heap_new();
ut.mem = sp_mem_heap_as_allocator(ut.heap);
}

UTEST_F_TEARDOWN(env) {
sp_mem_heap_destroy(ut.heap);
}

UTEST_F(env, init_empty) {
Expand Down Expand Up @@ -265,7 +268,7 @@ UTEST_F(env, iterate) {
}

UTEST_F(env, iterate_matches_capture) {
sp_env_t captured = sp_env_capture(sp_mem_os_new());
sp_env_t captured = sp_env_capture(ut.mem);

sp_os_env_it_t it = sp_os_env_it_begin();
u32 it_count = 0;
Expand Down
2 changes: 1 addition & 1 deletion test/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static sp_ht(sp_str_t, sp_fmt_directive_t) sp_fmt_directives = SP_NULLPTR;
void sp_fmt_directive_reset() {
sp_tls_rt_t* tls = sp_tls_rt_get();
sp_str_ht_free(tls->format.directives);
sp_str_ht_init(tls->mem, tls->format.directives);
sp_str_ht_init(tls->mem.heap, tls->format.directives);
sp_fmt_register_builtins();
}

Expand Down
28 changes: 20 additions & 8 deletions test/fs/get_cwd.c
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
#include "fs.h"

UTEST(fs_get_cwd, contract) {
struct fs_get_cwd {
sp_mem_heap_t* heap;
sp_mem_t mem;
};

UTEST_F_SETUP(fs_get_cwd) {
ut.heap = sp_mem_heap_new();
ut.mem = sp_mem_heap_as_allocator(ut.heap);
}

UTEST_F_TEARDOWN(fs_get_cwd) {
sp_mem_heap_destroy(ut.heap);
}

UTEST_F(fs_get_cwd, contract) {
SKIP_ON_WASM()
sp_mem_t a = sp_mem_os_new();
sp_str_t cwd = sp_fs_get_cwd(a);
sp_str_t cwd = sp_fs_get_cwd(ut.mem);
ASSERT_TRUE(sp_fs_is_dir(cwd));
ASSERT_TRUE(sp_fs_is_absolute(cwd));
}

UTEST(fs_get_cwd, unlinked_cwd_does_not_leak_deleted_suffix) {
UTEST_F(fs_get_cwd, unlinked_cwd_does_not_leak_deleted_suffix) {
// @spader
#if !defined(SP_LINUX)
UTEST_SKIP("unlinked-cwd is a Linux-only scenario");
#else
sp_mem_t a = sp_mem_os_new();
sp_str_t original = sp_fs_get_cwd(a);
sp_str_t original = sp_fs_get_cwd(ut.mem);
ASSERT_GT(original.len, 0);

sp_str_t sandbox = sp_fs_join_path(a, original, sp_str_lit(".tmp/unlinked_cwd_test"));
sp_str_t sandbox = sp_fs_join_path(ut.mem, original, sp_str_lit(".tmp/unlinked_cwd_test"));
sp_fs_remove_dir(sandbox);
ASSERT_EQ(sp_fs_create_dir(sandbox), SP_OK);

ASSERT_EQ(sp_sys_chdir_s(sandbox), 0);
ASSERT_EQ(sp_fs_remove_dir(sandbox), SP_OK);

sp_str_t cwd = sp_fs_get_cwd(a);
sp_str_t cwd = sp_fs_get_cwd(ut.mem);

// restore cwd before any assertion can fail, so other tests aren't poisoned
sp_sys_chdir_s(original);
Expand Down
Loading
Loading