Account for realloc moving the memory in gebs_arena_realloc()
This commit is contained in:
@ -8,7 +8,7 @@ typedef struct {
|
|||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
Gebs_Arena *arena = gebs_arena_make(1024);
|
Gebs_Arena *arena = gebs_arena_make(sizeof(int));
|
||||||
defer { gebs_arena_destroy(arena); }
|
defer { gebs_arena_destroy(arena); }
|
||||||
|
|
||||||
Ints ints = {0};
|
Ints ints = {0};
|
||||||
|
4
gebs.c
4
gebs.c
@ -9,11 +9,11 @@ int main(int argc, char ** argv)
|
|||||||
gebs_mkdir("build");
|
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)
|
if (GEBS_CMD("gcc", "-ggdb", "-o", "build/self_rebuild", "example/self_rebuild.c") != 0)
|
||||||
return 1;
|
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)
|
if (GEBS_CMD("gcc", "-ggdb", "-o", "build/arena", "example/arena.c") != 0)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
37
gebs.h
37
gebs.h
@ -23,6 +23,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
#if GEBS_PLATFORM == GEBS_PLATFORM_POSIX
|
#if GEBS_PLATFORM == GEBS_PLATFORM_POSIX
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
@ -33,6 +34,8 @@
|
|||||||
# error "Unknown GEBS_PLATFORM"
|
# error "Unknown GEBS_PLATFORM"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define discard __attribute__((unused))
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// Defer macro
|
// Defer macro
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
@ -239,9 +242,12 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv,
|
|||||||
#define gebs_needs_rebuild_many(out, ...) \
|
#define gebs_needs_rebuild_many(out, ...) \
|
||||||
({ \
|
({ \
|
||||||
const char *__deps[] = { __VA_ARGS__ }; \
|
const char *__deps[] = { __VA_ARGS__ }; \
|
||||||
bool __ok = true; \
|
bool __ok = false; \
|
||||||
for (size_t __i = 0; __i < sizeof(__deps)/sizeof(__deps[0]); __i++) { \
|
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; \
|
__ok; \
|
||||||
})
|
})
|
||||||
@ -327,22 +333,19 @@ Gebs_Allocator gebs_default_allocator = {
|
|||||||
.realloc = &gebs_default_allocator_realloc,
|
.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);
|
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);
|
free(memory);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *gebs_default_allocator_realloc(void *self, void *memory,
|
void *gebs_default_allocator_realloc(discard void *self, void *memory,
|
||||||
size_t prev_size, size_t new_size)
|
discard size_t prev_size, size_t new_size)
|
||||||
{
|
{
|
||||||
(void)self;
|
|
||||||
void *p = realloc(memory, new_size);
|
void *p = realloc(memory, new_size);
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
@ -411,18 +414,18 @@ void *gebs_arena_malloc(void *self, size_t size)
|
|||||||
return a->region + (a->index - 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;
|
Gebs_Arena *a = (Gebs_Arena *)self;
|
||||||
(void)memory;
|
|
||||||
gebs_arena_expand((Gebs_Arena *)self, new_size);
|
uintptr_t offset = (char *)memory - a->region;
|
||||||
return memory;
|
gebs_arena_expand(a, a->size + new_size);
|
||||||
|
return a->region + offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
void gebs_arena_reset(Gebs_Arena *a)
|
void gebs_arena_reset(Gebs_Arena *a)
|
||||||
|
Reference in New Issue
Block a user