Skip to content

Commit

Permalink
Add helper to allocate rest of a GglArena
Browse files Browse the repository at this point in the history
  • Loading branch information
archigup committed Oct 31, 2024
1 parent b94ba6f commit 53a16d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ggl-lib/include/ggl/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions ggl-lib/src/arena.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <assert.h>
Expand Down Expand Up @@ -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;
}

0 comments on commit 53a16d1

Please sign in to comment.