Skip to content

Commit

Permalink
CScriptEngine: Use dynamic buffer instead of _alloca.
Browse files Browse the repository at this point in the history
  • Loading branch information
nitrocaster committed Jan 16, 2016
1 parent 2669d99 commit 28c2d19
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 30 deletions.
40 changes: 10 additions & 30 deletions src/xrScriptEngine/script_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ void CScriptEngine::reinit()
file_header = file_header_new;
else
file_header = file_header_old;
scriptBufferSize = 1024*1024;
scriptBuffer = xr_alloc<char>(scriptBufferSize);
}

int CScriptEngine::vscript_log(LuaMessageType luaMessageType, LPCSTR caFormat, va_list marker)
Expand Down Expand Up @@ -338,38 +340,14 @@ bool CScriptEngine::load_buffer(lua_State *L, LPCSTR caBuffer, size_t tSize, LPC
xr_sprintf(insert, header, caNameSpaceName, a, b);
u32 str_len = xr_strlen(insert);
u32 const total_size = str_len+tSize;
LPSTR script = nullptr;
bool dynamic_allocation = false;
__try
if (total_size>=scriptBufferSize)
{
if (total_size < 768 * 1024)
script = (LPSTR)_alloca(total_size);
else
{
#ifdef DEBUG
script = (LPSTR)Memory.mem_alloc(total_size, "lua script file");
#else
script = (LPSTR)Memory.mem_alloc(total_size);
#endif
dynamic_allocation = true;
}
}
__except (GetExceptionCode()==STATUS_STACK_OVERFLOW)
{
int errcode = _resetstkoflw();
R_ASSERT2(errcode, "Could not reset the stack after \"Stack overflow\" exception!");
#ifdef DEBUG
script = (LPSTR)Memory.mem_alloc(total_size, "lua script file (after exception)");
#else
script = (LPSTR)Memory.mem_alloc(total_size);
#endif
dynamic_allocation = true;
scriptBufferSize = total_size;
scriptBuffer = (char *)xr_realloc(scriptBuffer, scriptBufferSize);
}
xr_strcpy(script, total_size, insert);
CopyMemory(script+str_len, caBuffer, u32(tSize));
l_iErrorCode = luaL_loadbuffer(L, script, tSize+str_len, caScriptName);
if (dynamic_allocation)
xr_free(script);
xr_strcpy(scriptBuffer, total_size, insert);
CopyMemory(scriptBuffer+str_len, caBuffer, u32(tSize));
l_iErrorCode = luaL_loadbuffer(L, scriptBuffer, tSize+str_len, caScriptName);
}
else
l_iErrorCode = luaL_loadbuffer(L, caBuffer, tSize, caScriptName);
Expand Down Expand Up @@ -808,6 +786,8 @@ CScriptEngine::~CScriptEngine()
disconnect_from_debugger();
#endif
#endif
if (scriptBuffer)
xr_free(scriptBuffer);
}

void CScriptEngine::unload()
Expand Down
2 changes: 2 additions & 0 deletions src/xrScriptEngine/script_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class XRSCRIPTENGINE_API CScriptEngine
static string4096 g_ca_stdout;
bool logReenterability = false;
bool bindingsDumped = false;
char *scriptBuffer = nullptr;
size_t scriptBufferSize = 0;

protected:
CScriptProcessStorage m_script_processes;
Expand Down

0 comments on commit 28c2d19

Please sign in to comment.