Compare commits

..

10 Commits

Author SHA1 Message Date
seajee
38decaf81a 1.0.3 - Renamed parameter bytes to size in arena_alloc() 2025-09-10 21:49:09 +02:00
seajee
9657cfdb43 v1.0.2 release 2025-09-06 02:22:52 +02:00
seajee
4c069c0945 v1.0.1 - Prevent name mangling of functions; don't reset region_capacity in arena_free() 2025-08-02 15:13:38 +02:00
seajee
e583d87285 Remove new line 2025-07-24 21:04:21 +02:00
seajee
529e3918e1 v1.0.0 - Initial release 2025-07-24 19:57:43 +02:00
seajee
562191e10c Complete Arena struct typedef 2025-07-24 16:00:29 +02:00
seajee
2c636c524d Simplify arena_alloc implementation 2025-07-24 15:55:26 +02:00
seajee
ff948662e5 Update README.md 2025-07-24 15:46:08 +02:00
seajee
f9b6b1e7f0 Add .gitignore 2025-07-24 15:39:15 +02:00
seajee
26fd7f1118 Improve modularity, introduce arena_create() 2025-07-24 15:38:21 +02:00
4 changed files with 233 additions and 43 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
test

View File

@@ -1,7 +1,8 @@
# arena.h
A single header library that implements
[region-based memory management](https://en.wikipedia.org/wiki/Region-based_memory_management) in C.
[region-based memory management](https://en.wikipedia.org/wiki/Region-based_memory_management)
in C using a linked list approach.
## Example
@@ -11,12 +12,13 @@ A single header library that implements
int main(void)
{
Arena a = {0};
Arena a = {0}; // or arena_create(...) to specify a custom region
// capacity just for this arena
int *x = arena_alloc(&a, sizeof(*x) * 69);
float *y = arena_alloc(&a, sizeof(*y) * 420);
int *x = arena_alloc(&a, sizeof(*x) * 32);
float *y = arena_alloc(&a, sizeof(*y) * 512);
arena_free(&a);
arena_free(&a); // The arena can still be reused
return 0;
}
```

250
arena.h
View File

@@ -1,84 +1,232 @@
// arena.h - v1.0.3 - MIT License - https://github.com/seajee/arena.h
// single header library for region-based memory management.
//
// [License and changelog]
//
// See end of file.
//
// [Single header library usage]
//
// Including this header file as such does what you normally expect.
//
// If you also need to include all of the function implementations you
// need to #define ARENA_IMPLEMENTATION before including the header file
// like this:
//
// #define ARENA_IMPLEMENTATION
// #include "arena.h"
//
// [Compile-time options]
//
// Note: every compile-time option listed here should be configured before
// the #include "arena.h"
//
// #define ARENA_NO_ASSERT
//
// This macro disables assertions that happen when a reallocation
// fails. The disabled assertions will be replaced with a simple
// condition that returns NULL when it fails. Note that ARENA_ASSERT
// is only used in arena_alloc().
//
// #define ARENA_REGION_CAPACITY new_region_capacity_in_bytes (4096)
//
// This macro defines the default capacity for arena regions.
//
// #define ARENA_ASSERT my_assert
//
// This macro defines an alternative function for assertions. In this
// library, ARENA_ASSERT is only used when arena_alloc() fails. Will
// be overwritten if ARENA_NO_ASSERT is defined.
//
// #define ARENA_REALLOC my_realloc
// #define ARENA_FREE my_free
//
// These macros define alternative functions for dynamic allocation
// and deallocation on the heap. They are only used for managing
// Arena_Region structures.
//
// [Function documentation]
//
// In this library, arenas are implemented as linked lists of regions. Each
// region will contain the allocated buffers. The following are all of the
// functions
//
// Arena arena_create(size_t region_capacity)
//
// This function initializes an arena with a specified region capacity. By
// default the region capacity is ARENA_REGION_CAPACITY, which can be
// configured by redefining it before including the header file. This function
// is not strictly necessary for initializing an arena (see example).
//
// void *arena_alloc(Arena *a, size_t size)
//
// This function allocates a buffer of <size> bytes into the specified arena
// and returns it's pointer.
//
// void arena_free(Arena *a)
//
// This function frees all of the regions allocated in the specified arena
// which invalidates all the pointers associated with the arena. The arena can
// still be reused. If the arena was created with arena_create() the
// configured region_capacity will be retained.
//
// void arena_reset(Arena *a)
//
// This function resets an arena by keeping all of the regions allocated but
// invalidates all of the pointers associated with the specified arena.
// Warning: this functions may cause fragmentation, consider setting an
// appropriate region capacity.
//
// [Example]
//
#if 0
#define ARENA_IMPLEMENTATION
#include "arena.h"
int main(void)
{
Arena a = {0}; // or arena_create(...) to specify a custom region
// capacity just for this arena
int *x = arena_alloc(&a, sizeof(*x) * 32);
float *y = arena_alloc(&a, sizeof(*y) * 512);
arena_free(&a); // The arena can still be reused
return 0;
}
#endif // [Example]
#ifndef ARENA_H_
#define ARENA_H_
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifndef ARENA_MIN_CAPACITY
#define ARENA_MIN_CAPACITY 4096
#endif // ARENA_MIN_CAPACITY
#ifndef ARENA_REGION_CAPACITY
# define ARENA_REGION_CAPACITY (8*1024)
#endif // ARENA_REGION_CAPACITY
#ifndef ARENA_NO_ASSERT
# ifndef ARENA_ASSERT
# include <assert.h>
# define ARENA_ASSERT assert
# endif // ARENA_ASSERT
#else
# define ARENA_ASSERT(...) ((void)0)
#endif // ARENA_NO_ASSERT
#ifndef ARENA_REALLOC
# include <stdlib.h>
# define ARENA_REALLOC realloc
#endif // ARENA_REALLOC
#ifndef ARENA_FREE
# include <stdlib.h>
# define ARENA_FREE free
#endif // ARENA_FREE
#ifdef __cplusplus
extern "C" { // Prevent name mangling of functions
#endif // __cplusplus
typedef struct Arena_Region Arena_Region;
struct Arena_Region {
Arena_Region *next;
size_t count;
size_t capacity;
Arena_Region *next;
uint8_t data[];
};
typedef struct {
typedef struct Arena {
Arena_Region *head;
Arena_Region *tail;
size_t region_capacity;
} Arena;
void *arena_alloc(Arena *a, size_t bytes);
Arena arena_create(size_t region_capacity);
void *arena_alloc(Arena *a, size_t size);
void arena_free(Arena *a);
void arena_reset(Arena *a);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // ARENA_H_
#ifdef ARENA_IMPLEMENTATION
void *arena_alloc(Arena *a, size_t bytes)
#ifdef __cplusplus
extern "C" { // Prevent name mangling of functions
#endif // __cplusplus
Arena arena_create(size_t region_capacity)
{
if (a == NULL) {
Arena a = {0};
a.region_capacity = region_capacity;
return a;
}
void *arena_alloc(Arena *a, size_t size)
{
if (a == NULL || size == 0) {
ARENA_ASSERT(!"Invalid, parameters");
return NULL;
}
size_t region_capacity = (a->region_capacity == 0
? ARENA_REGION_CAPACITY : a->region_capacity);
// Empty arena
if (a->head == NULL) {
size_t size = (bytes > ARENA_MIN_CAPACITY ? bytes : ARENA_MIN_CAPACITY);
a->head = (Arena_Region*)malloc(sizeof(*a->head) + size);
size_t alloc_size = (size > region_capacity ? size : region_capacity);
a->head = (Arena_Region*)ARENA_REALLOC(
NULL, sizeof(*a->head) + alloc_size);
if (a->head == NULL) {
ARENA_ASSERT(!"Reallocation failed");
return NULL;
}
memset(a->head, 0, sizeof(*a->head));
a->head->count = bytes;
a->head->capacity = size;
a->head->next = NULL;
a->head->count = size;
a->head->capacity = alloc_size;
a->tail = a->head;
return a->head->data;
}
// Not enough capacity
if (bytes > a->tail->capacity - a->tail->count) {
if (size > a->tail->capacity - a->tail->count) {
// Find first suitable region
while (a->tail->next != NULL && bytes > a->tail->capacity - a->tail->count) {
while (a->tail->next != NULL
&& size > a->tail->capacity - a->tail->count) {
a->tail = a->tail->next;
}
// If found allocate on that region
if (bytes <= a->tail->capacity - a->tail->count) {
a->tail->count += bytes;
return a->tail->data + a->tail->count;
}
// If not found create a new region
if (size > a->tail->capacity - a->tail->count) {
size_t alloc_size =
(size > region_capacity ? size : region_capacity);
a->tail->next = (Arena_Region*)ARENA_REALLOC(
NULL, sizeof(*a->tail) + alloc_size);
// If not create a new region
a->tail->next = (Arena_Region*)malloc(sizeof(*a->tail) + bytes);
if (a->tail->next == NULL) {
ARENA_ASSERT(!"Reallocation failed");
return NULL;
}
a->tail = a->tail->next;
memset(a->tail, 0, sizeof(*a->tail));
a->tail->count = bytes;
a->tail->capacity = bytes;
a->tail->next = NULL;
a->tail->count = size;
a->tail->capacity = alloc_size;
return a->tail->data;
}
}
a->tail->count += bytes;
return a->tail->data + a->tail->count;
a->tail->count += size;
return a->tail->data + a->tail->count - size;
}
void arena_free(Arena *a)
@@ -90,11 +238,13 @@ void arena_free(Arena *a)
Arena_Region *cur = a->head;
while (cur != NULL) {
Arena_Region *next = cur->next;
free(cur);
ARENA_FREE(cur);
cur = next;
}
memset(a, 0, sizeof(*a));
a->head = NULL;
a->tail = NULL;
// a->region_capacity = 0;
}
void arena_reset(Arena *a)
@@ -110,4 +260,42 @@ void arena_reset(Arena *a)
a->tail = a->head;
}
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // ARENA_IMPLEMENTATION
/*
* Revision history:
*
* 1.0.3 (2025-09-10) Renamed parameter bytes to size in arena_alloc()
* 1.0.2 (2025-09-06) Bug fixes; new ARENA_NO_ASSERT macro
* 1.0.1 (2025-08-02) Prevent name mangling of functions; don't reset
* region_capacity in arena_free()
* 1.0.0 (2025-07-24) Initial release
*/
/*
* MIT License
*
* Copyright (c) 2025 seajee
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

5
test.c
View File

@@ -2,13 +2,12 @@
#include <stdlib.h>
#ifdef DEBUG
#define malloc(n) (printf("%s:%d:%s: malloc(%ld)\n",\
__FILE__, __LINE__, __func__, (n)), malloc((n)));
#define realloc(p, s) (printf("%s:%d:%s: realloc(%p, %lu)\n",\
__FILE__, __LINE__, __func__, (p), (s)), realloc((p), (s)));
#define free(p) (printf("%s:%d:%s: free(%p)\n",\
__FILE__, __LINE__, __func__, (p)), free((p)));
#endif // DEBUG
#define ARENA_MIN_CAPACITY 400
#define ARENA_IMPLEMENTATION
#include "arena.h"