vtgl

terminal emulator implemented in OpenGL
git clone anongit@rnpnr.xyz:vtgl.git
Log | Files | Refs | Feed | LICENSE

Commit: 974e3f2ae6f0c1b4489cf8176b028758730719db
Parent: 8d86a2a26fbc0789a81375698ca7f6050e35d6c9
Author: Randy Palamar
Date:   Mon, 11 Nov 2024 10:58:12 -0700

generalize platform memory allocation

runtime allocations are still not allowed; this is just to cleanup some code

Diffstat:
Mos_unix.c | 28++++++++++++++++------------
Mplatform_linux_x11.c | 21+++++++++------------
Mtest.c | 2+-
Mutil.c | 11++++++++++-
Mutil.h | 2++
Mvtgl.c | 2+-
6 files changed, 39 insertions(+), 27 deletions(-)

diff --git a/os_unix.c b/os_unix.c @@ -42,20 +42,24 @@ os_fatal(s8 msg) _exit(1); } -static Arena -os_new_arena(size cap) +static MemoryBlock +posix_alloc(size requested_size) { - Arena a; + MemoryBlock result = {0}; - size pagesize = sysconf(_SC_PAGESIZE); - if (cap % pagesize != 0) - cap += pagesize - cap % pagesize; - - a.beg = mmap(0, cap, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); - if (a.beg == MAP_FAILED) - return (Arena){0}; - a.end = a.beg + cap; - return a; + size page_size = sysconf(_SC_PAGESIZE); + size alloc_size = requested_size; + + if (alloc_size % page_size!= 0) + alloc_size += page_size - alloc_size % page_size; + + void *memory = mmap(0, alloc_size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + if (memory != MAP_FAILED) { + result.memory = memory; + result.size = alloc_size; + } + + return result; } static PLATFORM_GET_FILESTATS_FN(posix_get_file_stats) diff --git a/platform_linux_x11.c b/platform_linux_x11.c @@ -376,18 +376,15 @@ i32 main(i32 argc, char *argv[], char *envp[]) { { - /* TODO: remove this step */ - Arena tmp = os_new_arena(16 * MEGABYTE); - linux_ctx.memory.memory = tmp.beg; - linux_ctx.memory.memory_size = tmp.end - tmp.beg; - } - #ifdef _DEBUG - { - Arena debug_memory = os_new_arena(128 * MEGABYTE); - linux_ctx.memory.debug_memory = debug_memory.beg; - linux_ctx.memory.debug_memory_size = debug_memory.end - debug_memory.beg; + MemoryBlock terminal_memory = posix_alloc(16 * MEGABYTE); + linux_ctx.memory.memory = terminal_memory.memory; + linux_ctx.memory.memory_size = terminal_memory.size; +#ifdef _DEBUG + MemoryBlock debug_memory = posix_alloc(128 * MEGABYTE); + linux_ctx.memory.debug_memory = debug_memory.memory; + linux_ctx.memory.debug_memory_size = debug_memory.size; +#endif } - #endif linux_ctx.memory.platform_api.read = posix_read; linux_ctx.memory.platform_api.write = posix_write; @@ -401,7 +398,7 @@ main(i32 argc, char *argv[], char *envp[]) linux_ctx.memory.platform_api.add_file_watch = linux_add_file_watch; linux_ctx.memory.platform_api.path_separator = '/'; - linux_ctx.platform_memory = os_new_arena(2 * MEGABYTE); + linux_ctx.platform_memory = arena_from_memory_block(posix_alloc(2 * MEGABYTE)); linux_ctx.error_stream = stream_alloc(&linux_ctx.platform_memory, MEGABYTE / 4); iv2 cells = {.x = -1, .y = -1}; diff --git a/test.c b/test.c @@ -355,7 +355,7 @@ DebugMetadata debug_metadata[__COUNTER__]; int main(void) { - Arena memory = os_new_arena(32 * MEGABYTE); + Arena memory = arena_from_memory_block(posix_alloc(32 * MEGABYTE)); Term term = {0}; Stream log = stream_alloc(&memory, 4 * MEGABYTE); diff --git a/util.c b/util.c @@ -97,6 +97,15 @@ mem_clear(void *p_, u8 c, size len) return p; } +static Arena +arena_from_memory_block(MemoryBlock memory) +{ + Arena result; + result.beg = memory.memory; + result.end = memory.memory + memory.size; + return result; +} + #define alloc(a, t, n) (t *)alloc_(a, sizeof(t), _Alignof(t), n) static void * alloc_(Arena *a, size len, size align, size count) @@ -113,7 +122,7 @@ alloc_(Arena *a, size len, size align, size count) } static Arena -make_arena(Arena *a, size size) +sub_arena(Arena *a, size size) { Arena result = {0}; result.beg = alloc_(a, size, 64, 1); diff --git a/util.h b/util.h @@ -96,6 +96,8 @@ typedef union { typedef struct { u8 *beg, *end; } Arena; typedef struct { Arena *arena; u8 *old_beg; } TempArena; +typedef struct { void *memory; size size; } MemoryBlock; + typedef struct { size len; u8 *data; } s8; #define s8(s) (s8){.len = ARRAY_COUNT(s) - 1, .data = (u8 *)s} diff --git a/vtgl.c b/vtgl.c @@ -309,7 +309,7 @@ make_render_ctx(Arena *a, GLCtx *gl, FontAtlas *fa) result.gl = gl; result.fa = fa; result.rpb = alloc(a, RenderPushBuffer, 1); - result.a = make_arena(a, 4 * MEGABYTE); + result.a = sub_arena(a, 4 * MEGABYTE); return result; }