Files
deb-lite-xl/src/arena_allocator.h
T
takase1121 14489482ec allocator: add arena_allocator
This allocator uses Lua userdatas for dynamic allocation
that is automatically freed when the current scope exits.
2024-11-25 21:29:28 +08:00

25 lines
793 B
C

/**
* 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