Skip to content

Commit

Permalink
Replace the old Lua small block allocator with a new one (for #74)
Browse files Browse the repository at this point in the history
Renamed original .c file to .cpp to make VS2008 happy
  • Loading branch information
fgenesis committed Jan 23, 2021
1 parent e4b2ee6 commit db079a5
Show file tree
Hide file tree
Showing 9 changed files with 828 additions and 426 deletions.
44 changes: 22 additions & 22 deletions Aquaria/ScriptInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "SDL.h"
#include "ScriptInterface.h"
#include "../BBGE/ScriptObject.h"
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#include "luaalloc.h"

#include "SDL.h"
#include "ScriptInterface.h"
#include "../BBGE/ScriptObject.h"

#include "ReadXML.h"

Expand Down Expand Up @@ -11498,7 +11500,7 @@ static const struct {
//============================================================================================

ScriptInterface::ScriptInterface()
: baseState(NULL), _sballoc(8, 128)
: baseState(NULL), _LA(NULL)
{
}

Expand All @@ -11513,6 +11515,8 @@ void ScriptInterface::init()

allowUnsafeFunctions = dsq->user.system.allowDangerousScriptFunctions;

if(!_LA)
_LA = luaalloc_create(NULL, NULL);
if (!baseState)
baseState = createLuaVM();
}
Expand All @@ -11523,15 +11527,9 @@ void ScriptInterface::reset()
init();
}

void *ScriptInterface::the_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
ScriptInterface *this_ = (ScriptInterface*)ud;
return this_->_sballoc.Alloc(ptr, nsize, osize);
}

lua_State *ScriptInterface::createLuaVM()
{
lua_State *state = lua_newstate(the_alloc, this); /* opens Lua */
lua_State *state = lua_newstate(_LA ? luaalloc : NULL, _LA); /* opens Lua */
luaL_openlibs(state);

#ifdef LUAAPI_HAS_CLIPBOARD
Expand Down Expand Up @@ -11612,12 +11610,6 @@ lua_State *ScriptInterface::createLuaVM()
return state;
}

void ScriptInterface::destroyLuaVM(lua_State *state)
{
if (state)
lua_close(state);
}

// Initial value for the instance-local table should be on the stack of
// the base Lua state; it will be popped when this function returns.
lua_State *ScriptInterface::createLuaThread(const std::string &file)
Expand Down Expand Up @@ -11721,8 +11713,16 @@ int ScriptInterface::gcGetStats()

void ScriptInterface::shutdown()
{
destroyLuaVM(baseState);
baseState = NULL;
if (baseState)
{
lua_close(baseState);
baseState = NULL;
}
if(_LA)
{
luaalloc_delete(_LA);
_LA = NULL;
}
}

Script *ScriptInterface::openScript(const std::string &file, bool ignoremissing /* = false */)
Expand Down
5 changes: 2 additions & 3 deletions Aquaria/ScriptInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define SCRIPTINTERFACE_H

#include "../BBGE/Base.h"
#include "../BBGE/MemoryAllocatorSmallBlock.h"

struct lua_State;
struct LuaAlloc;

class Entity;
class CollideEntity;
Expand Down Expand Up @@ -115,13 +115,12 @@ class ScriptInterface

protected:
lua_State *createLuaVM();
void destroyLuaVM(lua_State *state);
lua_State *createLuaThread(const std::string &file);
int destroyLuaThread(const std::string &file, lua_State *thread);
static void *the_alloc(void *ud, void *ptr, size_t osize, size_t nsize);

lua_State *baseState;
SmallBlockAllocator _sballoc;
LuaAlloc *_LA;
};

#endif
Loading

0 comments on commit db079a5

Please sign in to comment.