Thank you Viacheslav :)
Yes, I will implement the free() part as well to keep the memory under control.
Works now with output: 1
Need to try out non-trivial cases now.
#include <string>
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
char buffer[1048576];
char backupBuffer[1048576];
char *pTop = buffer;
void* customLuaAlloc(void* userData, void* ptr, size_t oldSize, size_t newSize)
{
if (newSize == 0)
{
//free(ptr);
return 0;
}
else {
//return realloc(ptr, newSize);
if (ptr != NULL) {
// Copy contents
memcpy(pTop, ptr, oldSize);
}
ptr = NULL;
char *pOldTop = pTop;
pTop += newSize;
return (void*)pOldTop;
}
}
int main()
{
lua_State* L = lua_newstate(customLuaAlloc, NULL);
luaL_openlibs(L);
luaL_dostring(L, "a = 1");
memcpy(backupBuffer, buffer, 1048576);
luaL_dostring(L, "a = 2");
memcpy(buffer, backupBuffer, 1048576);
luaL_dostring(L, "print(a)");
lua_close(L);
}
Thanks,
Abhijit