I've a DLL (the Lua embeddable scripting interpreter). It basically gives the C program a pointer (lua_State*) which points to a register-VM living in the C-programms heap memory.
(as a side note: the VM can be runned to execute scripts in interaction with the C host program it is embedded in).
There are different approaches to save and recover the actual state a script running inside a VM (think of a savegame). The one I came up with would (imaginary for now) work like this:
-instead of letting the memory used by the VM be distributed all over the heap (managed by the OS) which makes it pretty un-saveable, redirect the OS calls to malloc/realloc/free to my own versions which manage to hold the whole VM in I tight array-like buffer which can be saved stright away to disc for example as binary data
Problem: I've never done such a thing and need to be pointed to possible pitfalls.
- first thing would probably be to link my own malloc/realloc/free implementations to the Lua.dll so they have preference over the ones living inside the windows c-lib. Uh…how can this be done if I don't want to replace the whole c-lib but rather just that 3 functions?
- for the second step I would have to allocate a buffer on which my malloc/free functions operate. If the Lua.dll aquires memory through a call to my own malloc, I would have to find a block of enough continousely free space inside my buffer, save the start and lenght for it inside my (hopefully efficient) management data structure and return the pointer to the caller. Free and realloc would work in ta similar way. If I run out of continousely aligned free space inside my buffer I could either try to defragment it or aquire a new,lager buffer from the OS, move my data to it and so on.
my questions are:
This "plan" exists only on my (unexperienced) mind and is verified by nobody until now, so I'm very unsure. Do you think it would work like I expect it? Did I oversee something?
And how could I solve the first problem? I could build a static lib with the 3 functions inside and link it to the Lua.dll. But how do I tell the linker what it shall ignore the OS malloc/free functions? I work on WinXP + VS2005.
Thank you for any comments!