v1.0.0 - Initial release
This commit is contained in:
@@ -12,10 +12,11 @@ in C using a linked list approach.
|
||||
|
||||
int main(void)
|
||||
{
|
||||
Arena a = {0}; // or arena_create(...) to specify the region capacity
|
||||
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);
|
||||
return 0;
|
||||
|
||||
110
arena.h
110
arena.h
@@ -1,3 +1,81 @@
|
||||
// arena.h - v1.0.0 - MIT License - https://github.com/seajee/arena.h
|
||||
// single header library for region-based memory management.
|
||||
//
|
||||
// License and changelog:
|
||||
//
|
||||
// See end of file.
|
||||
//
|
||||
// Compile-time options:
|
||||
//
|
||||
// Note: every compile-time option listed here should be configured before
|
||||
// the definition of the ARENA_IMPLEMENTATION macro.
|
||||
//
|
||||
// #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_REALLOC fails.
|
||||
//
|
||||
// #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 bytes)
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// 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:
|
||||
/*
|
||||
#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);
|
||||
return 0;
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
#ifndef ARENA_H_
|
||||
#define ARENA_H_
|
||||
|
||||
@@ -38,7 +116,7 @@ typedef struct Arena {
|
||||
size_t region_capacity;
|
||||
} Arena;
|
||||
|
||||
Arena arena_create(size_t min_capacity);
|
||||
Arena arena_create(size_t region_capacity);
|
||||
void *arena_alloc(Arena *a, size_t bytes);
|
||||
void arena_free(Arena *a);
|
||||
void arena_reset(Arena *a);
|
||||
@@ -135,3 +213,33 @@ void arena_reset(Arena *a)
|
||||
}
|
||||
|
||||
#endif // ARENA_IMPLEMENTATION
|
||||
|
||||
/*
|
||||
* Revision history:
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user