v1.2.1 - Region growth factor handling with new ARENA_REGION_GROWTH_FACTOR macro
This commit is contained in:
21
arena.h
21
arena.h
@@ -1,4 +1,4 @@
|
|||||||
// arena.h - v1.2.0 - MIT License
|
// arena.h - v1.2.1 - MIT License
|
||||||
// single header library for region-based memory management.
|
// single header library for region-based memory management.
|
||||||
//
|
//
|
||||||
// [License and changelog]
|
// [License and changelog]
|
||||||
@@ -25,6 +25,11 @@
|
|||||||
//
|
//
|
||||||
// This macro defines the default capacity for arena regions.
|
// This macro defines the default capacity for arena regions.
|
||||||
//
|
//
|
||||||
|
// #define ARENA_REGION_GROWTH_FACTOR new_region_growth_factor (1.5f)
|
||||||
|
//
|
||||||
|
// This macro defines the growth factor of the capacity for every
|
||||||
|
// subsequent region in all arenas.
|
||||||
|
//
|
||||||
// #define ARENA_ALIGNMENT alignment (8)
|
// #define ARENA_ALIGNMENT alignment (8)
|
||||||
//
|
//
|
||||||
// This macro defines the memory alignment used for allocations.
|
// This macro defines the memory alignment used for allocations.
|
||||||
@@ -153,6 +158,10 @@ int main(void)
|
|||||||
# define ARENA_REGION_CAPACITY (8*1024)
|
# define ARENA_REGION_CAPACITY (8*1024)
|
||||||
#endif // ARENA_REGION_CAPACITY
|
#endif // ARENA_REGION_CAPACITY
|
||||||
|
|
||||||
|
#ifndef ARENA_REGION_GROWTH_FACTOR
|
||||||
|
# define ARENA_REGION_GROWTH_FACTOR 1.5f
|
||||||
|
#endif // ARENA_REGION_GROWTH_FACTOR
|
||||||
|
|
||||||
#ifndef ARENA_ALIGNMENT
|
#ifndef ARENA_ALIGNMENT
|
||||||
# define ARENA_ALIGNMENT alignof(max_align_t)
|
# define ARENA_ALIGNMENT alignof(max_align_t)
|
||||||
#endif // ARENA_ALIGNMENT
|
#endif // ARENA_ALIGNMENT
|
||||||
@@ -300,8 +309,16 @@ void *arena_alloc(Arena *a, size_t size)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the next region capacity multiplied by the growth factor
|
||||||
size_t region_capacity = (a->region_capacity == 0
|
size_t region_capacity = (a->region_capacity == 0
|
||||||
? ARENA_REGION_CAPACITY : a->region_capacity);
|
? ARENA_REGION_CAPACITY : a->region_capacity);
|
||||||
|
if (a->tail != NULL) {
|
||||||
|
region_capacity = a->tail->capacity * ARENA_REGION_GROWTH_FACTOR;
|
||||||
|
// Edge case for small growth factors
|
||||||
|
if (region_capacity <= a->tail->capacity) {
|
||||||
|
region_capacity = a->tail->capacity + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Calculate and check region allocation size
|
// Calculate and check region allocation size
|
||||||
size_t alloc_size = (size > region_capacity ? size : region_capacity);
|
size_t alloc_size = (size > region_capacity ? size : region_capacity);
|
||||||
@@ -491,6 +508,8 @@ size_t arena__align(size_t n)
|
|||||||
/*
|
/*
|
||||||
* Revision history:
|
* Revision history:
|
||||||
*
|
*
|
||||||
|
* 1.2.1 (2026-04-08) Region growth factor handling with new
|
||||||
|
* ARENA_REGION_GROWTH_FACTOR macro
|
||||||
* 1.2.0 (2026-04-08) New functions: arena_sprintf(), arena_snapshot(),
|
* 1.2.0 (2026-04-08) New functions: arena_sprintf(), arena_snapshot(),
|
||||||
* arena_rewind() and arena_trim()
|
* arena_rewind() and arena_trim()
|
||||||
* 1.1.3 (2026-01-14) Align Arena_Region data flexible array member
|
* 1.1.3 (2026-01-14) Align Arena_Region data flexible array member
|
||||||
|
|||||||
7
test.c
7
test.c
@@ -9,6 +9,7 @@
|
|||||||
#endif // DEBUG
|
#endif // DEBUG
|
||||||
|
|
||||||
#define ARENA_IMPLEMENTATION
|
#define ARENA_IMPLEMENTATION
|
||||||
|
#define ARENA_REGION_GROWTH_FACTOR 2
|
||||||
#include "arena.h"
|
#include "arena.h"
|
||||||
|
|
||||||
void arena_print(Arena arena)
|
void arena_print(Arena arena)
|
||||||
@@ -50,8 +51,14 @@ int main(void)
|
|||||||
arena_print(a);
|
arena_print(a);
|
||||||
|
|
||||||
char *str = arena_sprintf(&a, "Formatted string %x", 0xCAFE);
|
char *str = arena_sprintf(&a, "Formatted string %x", 0xCAFE);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < 1000; ++i) {
|
||||||
|
str = arena_sprintf(&a, "Formatted string %d", i);
|
||||||
|
}
|
||||||
|
|
||||||
printf("%s\n", str);
|
printf("%s\n", str);
|
||||||
|
|
||||||
|
arena_print(a);
|
||||||
arena_free(&a);
|
arena_free(&a);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user