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:
@@ -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
|
||||
Reference in New Issue
Block a user