Skip to content

Commit 28c2d19

Browse files
author
nitrocaster
committed
CScriptEngine: Use dynamic buffer instead of _alloca.
1 parent 2669d99 commit 28c2d19

File tree

2 files changed

+12
-30
lines changed

2 files changed

+12
-30
lines changed

src/xrScriptEngine/script_engine.cpp

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ void CScriptEngine::reinit()
189189
file_header = file_header_new;
190190
else
191191
file_header = file_header_old;
192+
scriptBufferSize = 1024*1024;
193+
scriptBuffer = xr_alloc<char>(scriptBufferSize);
192194
}
193195

194196
int CScriptEngine::vscript_log(LuaMessageType luaMessageType, LPCSTR caFormat, va_list marker)
@@ -338,38 +340,14 @@ bool CScriptEngine::load_buffer(lua_State *L, LPCSTR caBuffer, size_t tSize, LPC
338340
xr_sprintf(insert, header, caNameSpaceName, a, b);
339341
u32 str_len = xr_strlen(insert);
340342
u32 const total_size = str_len+tSize;
341-
LPSTR script = nullptr;
342-
bool dynamic_allocation = false;
343-
__try
343+
if (total_size>=scriptBufferSize)
344344
{
345-
if (total_size < 768 * 1024)
346-
script = (LPSTR)_alloca(total_size);
347-
else
348-
{
349-
#ifdef DEBUG
350-
script = (LPSTR)Memory.mem_alloc(total_size, "lua script file");
351-
#else
352-
script = (LPSTR)Memory.mem_alloc(total_size);
353-
#endif
354-
dynamic_allocation = true;
355-
}
356-
}
357-
__except (GetExceptionCode()==STATUS_STACK_OVERFLOW)
358-
{
359-
int errcode = _resetstkoflw();
360-
R_ASSERT2(errcode, "Could not reset the stack after \"Stack overflow\" exception!");
361-
#ifdef DEBUG
362-
script = (LPSTR)Memory.mem_alloc(total_size, "lua script file (after exception)");
363-
#else
364-
script = (LPSTR)Memory.mem_alloc(total_size);
365-
#endif
366-
dynamic_allocation = true;
345+
scriptBufferSize = total_size;
346+
scriptBuffer = (char *)xr_realloc(scriptBuffer, scriptBufferSize);
367347
}
368-
xr_strcpy(script, total_size, insert);
369-
CopyMemory(script+str_len, caBuffer, u32(tSize));
370-
l_iErrorCode = luaL_loadbuffer(L, script, tSize+str_len, caScriptName);
371-
if (dynamic_allocation)
372-
xr_free(script);
348+
xr_strcpy(scriptBuffer, total_size, insert);
349+
CopyMemory(scriptBuffer+str_len, caBuffer, u32(tSize));
350+
l_iErrorCode = luaL_loadbuffer(L, scriptBuffer, tSize+str_len, caScriptName);
373351
}
374352
else
375353
l_iErrorCode = luaL_loadbuffer(L, caBuffer, tSize, caScriptName);
@@ -808,6 +786,8 @@ CScriptEngine::~CScriptEngine()
808786
disconnect_from_debugger();
809787
#endif
810788
#endif
789+
if (scriptBuffer)
790+
xr_free(scriptBuffer);
811791
}
812792

813793
void CScriptEngine::unload()

src/xrScriptEngine/script_engine.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ class XRSCRIPTENGINE_API CScriptEngine
8282
static string4096 g_ca_stdout;
8383
bool logReenterability = false;
8484
bool bindingsDumped = false;
85+
char *scriptBuffer = nullptr;
86+
size_t scriptBufferSize = 0;
8587

8688
protected:
8789
CScriptProcessStorage m_script_processes;

0 commit comments

Comments
 (0)