lua-users home
lua-l archive

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

Hello Ben and every buddy else,

I investigated a bit and it seems much simpler: If I call lua_Newstate instead of luaL_Newstate as I did until now, I am able to pass an allocator function which is used exclusively for memory management by Lua regarding the lua_State* it is assigned to.

The default allocator function used by luaL_Newstate() is this:

static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
 if (nsize == 0) {
   return NULL;
   return realloc(ptr, nsize);

If I would overwrite it in a way so that all memory used by my lua_State* is inside my own (pre-allocated) buffer, I could just save and re-load that buffer to/from disc and resume execution. What do you think, will this work? It sounds pretty easy so far.

Two questions to the l_alloc function shown above:
1. What is void* ud for? The src code says something about "auxiliary data to the allocation function". Why is it not used by that function?

2. What are the first two lines for?
They do absolutely nothing, right? Are they there to let the compiler check something?

Thank you + Regards

Ben Sunshine-Hill schrieb:
If you want to serialize *everything* in the Lua state, the easiest
approach is a Lua variant called lper. That basically does your #3.
It's unix-specific, though... I'm not sure how much work would be
involved in porting it to win32.


On Fri, Mar 20, 2009 at 7:10 AM, <> wrote:

I want to save the state of a Lua VM to a file to re-load and restart
execution later.

Three options come to my mind:

1. Serializing the global index table of the VM. I even found a library
called "Pluto" which promises to do this.

2. Trying to make a deep copy of the lua_State* pointing to a VM and
save/load the binary data.

3. Trying to overwrite malloc/free for the Lua.dll, place all memory the VM
holds in an pre-allocated chunk. Save and re-load that whole chunk. I'm not
sure If I'm yet experienced (in C/C++) enough to get this done. Probably

My Questions are:

1. Which of that options will work or which one are used and which not?
2. Are there any other options I missed?
3. Which option is most promising in terms of time and complexity?
4. Are there any other helper libraries (beside Pluto) which helps doing one
of the options?

Thank you!