Files
integration/luprex/core/cpp/luasnap.hpp

61 lines
1.6 KiB
C++

/////////////////////////////////////////////////////////////////////////////
//
// LUASNAP
//
// A lua interpreter that can be checkpointed (snapshotted). This makes it
// possible to roll the entire interpreter back to a previously-snapshotted
// state.
//
// To accomplish this, we take advantage of the 'allocf' parameter to
// lua_newstate. This lets us hook the lua allocator, which in turn lets us
// find every block of memory currently in use by lua. To snapshot, we 'memcpy'
// every block of memory that lua is using into a buffer. To rollback, we just
// 'memcpy' the data back.
//
// The current implementation is a proof-of-concept. It's quite wasteful of
// memory, roughly doubling the amount of RAM that LUA uses. It's also not
// super-fast, since it allocates tons of tiny blocks. But it totally
// works, so it demonstrate that this method of snapshot and rollback is
// feasible.
//
/////////////////////////////////////////////////////////////////////////////
#ifndef LUASNAP_HPP
#define LUASNAP_HPP
#include "luastack.hpp"
class LuaSnapData;
class LuaSnap {
private:
lua_State *state_;
LuaSnapData *data_;
public:
LuaSnap();
~LuaSnap();
// Get the lua intepreter.
//
lua_State *state() const { return state_; }
// Return true if there's a saved snapshot.
//
bool have_snapshot() const;
// snapshot the state of the lua interpreter.
//
// If there is already a snapshot, this panics.
//
void snapshot();
// Rollback the lua intepreter to the snapshotted state.
//
// If there is no snapshot, this panics.
//
void rollback();
};
#endif // LUASNAP_HPP