lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi

I implemented a memory allocator.
To activate it, I call lua_newstate passing as arguments
- the pointer to the new allocator
- the pointer to a custom struct, of type tMemoryAccounting

The struct keeps informations necessary to our allocator algorithm.

Questions:
1) Concurrency

Our software can instantiate many instances via lua_newstate()
Each state shares the same allocator routine.
Each one has its own private struc.

Does Lua call the allocator with some sort of synchronization,
or should I protect the accesses to the struct (read and write) ?

Something like...
static void * _lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
    tMemoryAccounting * pMemoryAccounting = (tMemoryAccounting *) ud;
    lua_lock(pMemoryAccounting->L);
    ...
    lua_unlock(pMemoryAccounting->L);
}

Should I add a  lua_State *L  to tMemoryAccounting ?
Am I missing a lua_get_current_state(void) ?


2) handling exceptions in the allocator

The new allocator in specific circumstances decides to deny its operations, allocations and free
(also free because it checks for possible corruptions of the memory pointers).

My requirement is: letting Lua fails the current API with the error LUA_ERRMEM.
For example: we can decide, at a certain moment, the allocator have to fail allocations above 1 Mb. For stress-testing purposes, or for other reasons.
If so, the Lua routines should return an error to our software, which ends gracefully.

If I understand correctly:
- the allocator cannot call luaD_throw, because it calls atpanic and aborts the program;
- cannot use setjmp: jumping out of the allocator, all the cleanup code of Lua current routine (destructors, stack, etc..) is skipped.

The problem can be split in two sub-problems:
- how can the allocator set the Lua error state?
- how can the allocator stop Lua's current routine processing and return to the caller?

For requests of new allocations / realloc, the allocator can return NULL and set, for example   L->status = LUA_ERRMEM;    ?
What about free ?

M

_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org