allocator: add arena_allocator

This allocator uses Lua userdatas for dynamic allocation
that is automatically freed when the current scope exits.
This commit is contained in:
takase1121
2024-11-25 21:29:28 +08:00
parent f0d7e22dbf
commit 14489482ec
3 changed files with 70 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
/**
* An arena allocator using the Lua state; similar to luaL_Buffer.
* Initialize the arena with lxl_arena_init(), and you can use lxl_arena_malloc(),
* lxl_arena_zero() to allocate (and optionally zero) the memory.
* lxl_arena_free() can be optionally used to free memory, but this is generally not needed.
*/
#ifndef LUA_ALLOCATOR_H
#define LUA_ALLOCATOR_H
#include <lua.h>
typedef struct lxl_arena {
lua_State *L;
int ref;
} lxl_arena;
void lxl_arena_init(lua_State *L, lxl_arena *arena);
void *lxl_arena_malloc(lxl_arena *arena, size_t size);
void *lxl_arena_zero(lxl_arena *arena, size_t size);
char *lxl_arena_copy(lxl_arena *arena, void *ptr, size_t len);
char *lxl_arena_strdup(lxl_arena *arena, const char *str);
void lxl_arena_free(lxl_arena *arena, void *ptr);
#endif