From 53a16d10e0596d114f0f35ad35ebff3a369e4fba Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Tue, 29 Oct 2024 18:04:16 -0700 Subject: [PATCH] Add helper to allocate rest of a GglArena --- ggl-lib/include/ggl/arena.h | 3 +++ ggl-lib/src/arena.c | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/ggl-lib/include/ggl/arena.h b/ggl-lib/include/ggl/arena.h index 50df1dd4c..589031b68 100644 --- a/ggl-lib/include/ggl/arena.h +++ b/ggl-lib/include/ggl/arena.h @@ -46,4 +46,7 @@ GglError ggl_arena_resize_last( /// Returns true if arena's mem contains ptr. bool ggl_arena_owns(const GglArena *arena, const void *ptr); +/// Allocates remaining space into a buffer. +GglBuffer ggl_arena_alloc_rest(GglArena *arena); + #endif diff --git a/ggl-lib/src/arena.c b/ggl-lib/src/arena.c index eeefa188f..da96d594c 100644 --- a/ggl-lib/src/arena.c +++ b/ggl-lib/src/arena.c @@ -3,6 +3,7 @@ // SPDX-License-Identifier: Apache-2.0 #include "ggl/arena.h" +#include "ggl/buffer.h" #include "ggl/error.h" #include "ggl/log.h" #include @@ -93,3 +94,10 @@ bool ggl_arena_owns(const GglArena *arena, const void *ptr) { uintptr_t ptr_int = (uintptr_t) ptr; return (ptr_int >= mem_int) && (ptr_int < mem_int + arena->CAPACITY); } + +GglBuffer ggl_arena_alloc_rest(GglArena *arena) { + GglBuffer rest = { .data = arena->MEM + arena->index, + .len = arena->CAPACITY - arena->index }; + arena->index = arena->CAPACITY; + return rest; +}