Commit d5a0b5b7 authored by nanahira's avatar nanahira

improve lua memtracker

parent 669f3410
#include "LuaMemTracker.h"
#include <lauxlib.h>
LuaMemTracker::LuaMemTracker(size_t mem_limit)
: limit(mem_limit) {
lua_State* tmp_L = luaL_newstate(); // get default alloc
real_alloc = lua_getallocf(tmp_L, &real_ud);
lua_close(tmp_L);
LuaMemTracker::LuaMemTracker(lua_Alloc alloc_func, void* ud, size_t mem_limit)
: real_alloc(alloc_func), real_ud(ud), limit(mem_limit) {
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
time_t now = time(nullptr);
char filename[64];
......
......@@ -12,7 +12,7 @@
class LuaMemTracker {
public:
LuaMemTracker(lua_Alloc real_alloc, void* real_ud, size_t mem_limit = 0);
LuaMemTracker(size_t mem_limit = 0);
~LuaMemTracker();
static void* AllocThunk(void* ud, void* ptr, size_t osize, size_t nsize);
void* Alloc(void* ptr, size_t osize, size_t nsize);
......
......@@ -15,14 +15,7 @@
#include "interpreter.h"
interpreter::interpreter(duel* pd): coroutines(256) {
lua_State* tmp_L = luaL_newstate(); // 只是为了拿默认 alloc
lua_Alloc raw_alloc;
void* raw_ud;
raw_alloc = lua_getallocf(tmp_L, &raw_ud);
lua_close(tmp_L);
mem_tracker = new LuaMemTracker(raw_alloc, raw_ud, YGOPRO_LUA_MEMORY_SIZE);
mem_tracker = new LuaMemTracker(YGOPRO_LUA_MEMORY_SIZE);
lua_state = lua_newstate(LuaMemTracker::AllocThunk, mem_tracker);
current_state = lua_state;
pduel = pd;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment