This commit is contained in:
2026-07-07 06:48:32 +02:00
parent 57329340e2
commit 7b6af7028f

26
arena.h
View File

@@ -1,4 +1,4 @@
// arena.h - v1.2.2 - MIT License // arena.h - v1.2.3 - MIT License
// single header library for region-based memory management. // single header library for region-based memory management.
// //
// [License and changelog] // [License and changelog]
@@ -96,6 +96,12 @@
// This function allocates a buffer of <size> bytes into the specified arena // This function allocates a buffer of <size> bytes into the specified arena
// and returns it's pointer. // and returns it's pointer.
// //
// void *arena_calloc(Arena *a, size_t count, size_t size)
//
// This function allocates a buffer of <count> * <size> bytes into the
// specified arena and returns it's pointer. The resulting buffer will be
// zero-initialized just like the standard calloc() function.
//
// void *arena_copy(Arena *a, const void *src, size_t size); // void *arena_copy(Arena *a, const void *src, size_t size);
// //
// This function allocates a buffer of <size> bytes into the arena and copies // This function allocates a buffer of <size> bytes into the arena and copies
@@ -218,6 +224,7 @@ void arena_trim(Arena *a);
// Allocation and other utilities // Allocation and other utilities
void *arena_alloc(Arena *a, size_t size); void *arena_alloc(Arena *a, size_t size);
void *arena_calloc(Arena *a, size_t count, size_t size);
void *arena_copy(Arena *a, const void *src, size_t size); void *arena_copy(Arena *a, const void *src, size_t size);
char *arena_strdup(Arena *a, const char *s); char *arena_strdup(Arena *a, const char *s);
char *arena_sprintf(Arena *a, const char *fmt, ...); char *arena_sprintf(Arena *a, const char *fmt, ...);
@@ -382,6 +389,22 @@ void *arena_alloc(Arena *a, size_t size)
return a->tail->data; return a->tail->data;
} }
void *arena_calloc(Arena *a, size_t count, size_t size)
{
if (a == NULL) {
ARENA_ASSERT(false && "Invalid parameters");
return NULL;
}
void *result = arena_alloc(a, count * size);
if (result != NULL) {
memset(result, 0, count * size);
}
return result;
}
void *arena_copy(Arena *a, const void *src, size_t size) void *arena_copy(Arena *a, const void *src, size_t size)
{ {
if (a == NULL || src == NULL) { if (a == NULL || src == NULL) {
@@ -518,6 +541,7 @@ size_t arena__align(size_t n)
/* /*
* Revision history: * Revision history:
* *
* 1.2.3 (2026-07-07) New function: arena_calloc()
* 1.2.2 (2026-07-07) Fix various memory problems; * 1.2.2 (2026-07-07) Fix various memory problems;
* make ARENA_REGION_GROWTH_FACTOR an integer * make ARENA_REGION_GROWTH_FACTOR an integer
* 1.2.1 (2026-04-08) Region growth factor handling with new * 1.2.1 (2026-04-08) Region growth factor handling with new