Account for realloc moving the memory in gebs_arena_realloc()

This commit is contained in:
kamkow1
2025-05-19 23:55:14 +02:00
parent 78732d1aef
commit c1e0a5ec35
3 changed files with 23 additions and 20 deletions

View File

@ -8,7 +8,7 @@ typedef struct {
int main(void)
{
Gebs_Arena *arena = gebs_arena_make(1024);
Gebs_Arena *arena = gebs_arena_make(sizeof(int));
defer { gebs_arena_destroy(arena); }
Ints ints = {0};

4
gebs.c
View File

@ -9,11 +9,11 @@ int main(int argc, char ** argv)
gebs_mkdir("build");
}
if (gebs_needs_rebuild_many("build/self_rebuild", "example/self_rebuild.c")) {
if (gebs_needs_rebuild_many("build/self_rebuild", "example/self_rebuild.c", "gebs.h")) {
if (GEBS_CMD("gcc", "-ggdb", "-o", "build/self_rebuild", "example/self_rebuild.c") != 0)
return 1;
}
if (gebs_needs_rebuild_many("build/arena", "example/arena.c")) {
if (gebs_needs_rebuild_many("build/arena", "example/arena.c", "gebs.h")) {
if (GEBS_CMD("gcc", "-ggdb", "-o", "build/arena", "example/arena.c") != 0)
return 1;
}

37
gebs.h
View File

@ -23,6 +23,7 @@
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <inttypes.h>
#if GEBS_PLATFORM == GEBS_PLATFORM_POSIX
# include <unistd.h>
@ -33,6 +34,8 @@
# error "Unknown GEBS_PLATFORM"
#endif
#define discard __attribute__((unused))
// ----------------------------------------------------------------------------
// Defer macro
// ----------------------------------------------------------------------------
@ -239,9 +242,12 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv,
#define gebs_needs_rebuild_many(out, ...) \
({ \
const char *__deps[] = { __VA_ARGS__ }; \
bool __ok = true; \
bool __ok = false; \
for (size_t __i = 0; __i < sizeof(__deps)/sizeof(__deps[0]); __i++) { \
__ok = __ok && gebs_needs_rebuild((out), __deps[__i]); \
if (gebs_needs_rebuild((out), __deps[__i])) { \
__ok = true; \
break; \
} \
} \
__ok; \
})
@ -327,22 +333,19 @@ Gebs_Allocator gebs_default_allocator = {
.realloc = &gebs_default_allocator_realloc,
};
void *gebs_default_allocator_malloc(void *self, size_t size)
void *gebs_default_allocator_malloc(discard void *self, size_t size)
{
(void)self;
return malloc(size);
}
void gebs_default_allocator_free(void *self, void *memory)
void gebs_default_allocator_free(discard void *self, void *memory)
{
(void)self;
free(memory);
}
void *gebs_default_allocator_realloc(void *self, void *memory,
size_t prev_size, size_t new_size)
void *gebs_default_allocator_realloc(discard void *self, void *memory,
discard size_t prev_size, size_t new_size)
{
(void)self;
void *p = realloc(memory, new_size);
return p;
}
@ -411,18 +414,18 @@ void *gebs_arena_malloc(void *self, size_t size)
return a->region + (a->index - size);
}
void gebs_arena_free(void *self, void *memory)
void gebs_arena_free(discard void *self, discard void *memory)
{
(void)self;
(void)memory;
}
void *gebs_arena_realloc(void *self, void *memory, size_t prev_size, size_t new_size)
void *gebs_arena_realloc(void *self, void *memory,
discard size_t prev_size, size_t new_size)
{
(void)prev_size;
(void)memory;
gebs_arena_expand((Gebs_Arena *)self, new_size);
return memory;
Gebs_Arena *a = (Gebs_Arena *)self;
uintptr_t offset = (char *)memory - a->region;
gebs_arena_expand(a, a->size + new_size);
return a->region + offset;
}
void gebs_arena_reset(Gebs_Arena *a)