diff --git a/src/scripting/vm/jit.cpp b/src/scripting/vm/jit.cpp index 3f6b400791..04d8b867fd 100644 --- a/src/scripting/vm/jit.cpp +++ b/src/scripting/vm/jit.cpp @@ -35,7 +35,26 @@ public: } }; -static asmjit::JitRuntime jit; +static asmjit::JitRuntime *jit; +static int jitRefCount = 0; + +asmjit::JitRuntime *JitGetRuntime() +{ + if (!jit) + jit = new asmjit::JitRuntime; + jitRefCount++; + return jit; +} + +void JitCleanUp(VMScriptFunction *func) +{ + jitRefCount--; + if (jitRefCount == 0) + { + delete jit; + jit = nullptr; + } +} #define A (pc[0].a) #define B (pc[0].b) @@ -286,10 +305,12 @@ JitFuncPtr JitCompile(VMScriptFunction *sfunc) using namespace asmjit; try { + auto *jit = JitGetRuntime(); + ThrowingErrorHandler errorHandler; //FileLogger logger(stdout); CodeHolder code; - code.init(jit.getCodeInfo()); + code.init(jit->getCodeInfo()); code.setErrorHandler(&errorHandler); //code.setLogger(&logger); @@ -1723,7 +1744,7 @@ JitFuncPtr JitCompile(VMScriptFunction *sfunc) cc.finalize(); JitFuncPtr fn = nullptr; - Error err = jit.add(&fn, &code); + Error err = jit->add(&fn, &code); if (err) I_FatalError("JitRuntime::add failed: %d", err); return fn; diff --git a/src/scripting/vm/jit.h b/src/scripting/vm/jit.h index a2509cb013..923e02df31 100644 --- a/src/scripting/vm/jit.h +++ b/src/scripting/vm/jit.h @@ -4,3 +4,4 @@ #include "vmintern.h" JitFuncPtr JitCompile(VMScriptFunction *func); +void JitCleanUp(VMScriptFunction *func); diff --git a/src/scripting/vm/vmframe.cpp b/src/scripting/vm/vmframe.cpp index 69a6febbf4..a537c46374 100644 --- a/src/scripting/vm/vmframe.cpp +++ b/src/scripting/vm/vmframe.cpp @@ -40,6 +40,7 @@ #include "templates.h" #include "vmintern.h" #include "types.h" +#include "jit.h" cycle_t VMCycles[10]; int VMCalls[10]; @@ -77,6 +78,12 @@ VMScriptFunction::VMScriptFunction(FName name) VMScriptFunction::~VMScriptFunction() { + if (JitFunc) + { + JitCleanUp(this); + JitFunc = nullptr; + } + if (Code != NULL) { if (KonstS != NULL)