Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Y
ygopro-core
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
xiaoye
ygopro-core
Commits
eb324716
You need to sign in or sign up before continuing.
Commit
eb324716
authored
Jul 12, 2025
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add LuaMemTracker & limit Lua memory size
parent
c06a0867
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
127 additions
and
1 deletion
+127
-1
LuaMemTracker.cpp
LuaMemTracker.cpp
+80
-0
LuaMemTracker.h
LuaMemTracker.h
+38
-0
interpreter.cpp
interpreter.cpp
+3
-1
interpreter.h
interpreter.h
+6
-0
No files found.
LuaMemTracker.cpp
0 → 100644
View file @
eb324716
#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
);
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
time_t
now
=
time
(
nullptr
);
char
filename
[
64
];
std
::
snprintf
(
filename
,
sizeof
(
filename
),
"memtrace-%ld.log"
,
static_cast
<
long
>
(
now
));
log_file
=
std
::
fopen
(
filename
,
"a"
);
if
(
log_file
)
{
std
::
fprintf
(
log_file
,
"---- Lua memory tracking started ----
\n
"
);
}
#endif
}
LuaMemTracker
::~
LuaMemTracker
()
{
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
if
(
log_file
)
{
std
::
fprintf
(
log_file
,
"---- Lua memory tracking ended ----
\n
"
);
std
::
fclose
(
log_file
);
log_file
=
nullptr
;
}
#endif
}
void
*
LuaMemTracker
::
AllocThunk
(
void
*
ud
,
void
*
ptr
,
size_t
osize
,
size_t
nsize
)
{
return
static_cast
<
LuaMemTracker
*>
(
ud
)
->
Alloc
(
ptr
,
osize
,
nsize
);
}
void
*
LuaMemTracker
::
Alloc
(
void
*
ptr
,
size_t
osize
,
size_t
nsize
)
{
if
(
nsize
==
0
)
{
if
(
ptr
&&
osize
<=
total_allocated
)
{
total_allocated
-=
osize
;
}
return
real_alloc
(
real_ud
,
ptr
,
osize
,
nsize
);
}
else
{
size_t
projected
=
total_allocated
-
osize
+
nsize
;
if
(
limit
&&
projected
>
limit
)
{
return
nullptr
;
// over limit
}
void
*
newptr
=
real_alloc
(
real_ud
,
ptr
,
osize
,
nsize
);
if
(
newptr
)
{
total_allocated
=
projected
;
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
write_log
();
#endif
}
return
newptr
;
}
}
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
void
LuaMemTracker
::
write_log
()
{
if
(
!
log_file
)
return
;
time_t
now
=
time
(
nullptr
);
struct
tm
*
tm_info
=
localtime
(
&
now
);
char
time_buf
[
32
];
std
::
strftime
(
time_buf
,
sizeof
(
time_buf
),
"%Y-%m-%d %H:%M:%S"
,
tm_info
);
if
(
total_allocated
>
max_used
)
max_used
=
total_allocated
;
if
(
limit
)
std
::
fprintf
(
log_file
,
"%s | used = %zu bytes | max_used = %zu bytes | limit = %zu
\n
"
,
time_buf
,
total_allocated
,
max_used
,
limit
);
else
std
::
fprintf
(
log_file
,
"%s | used = %zu bytes | max_used = %zu bytes | limit = unlimited
\n
"
,
time_buf
,
total_allocated
,
max_used
);
std
::
fflush
(
log_file
);
// make it write instantly
}
#endif
LuaMemTracker.h
0 → 100644
View file @
eb324716
#ifndef LUA_MEM_TRACKER_H
#define LUA_MEM_TRACKER_H
#include <cstdlib>
#include <cstddef>
#include <lua.h>
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
#include <ctime> // time_t
#include <cstdio> // FILE*, fopen, fprintf
#endif
class
LuaMemTracker
{
public:
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
);
size_t
get_total
()
const
{
return
total_allocated
;
}
size_t
get_limit
()
const
{
return
limit
;
}
private:
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
FILE
*
log_file
=
nullptr
;
void
write_log
();
#endif
lua_Alloc
real_alloc
;
void
*
real_ud
;
size_t
total_allocated
=
0
;
size_t
limit
=
0
;
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
size_t
max_used
=
0
;
// for logging purposes, to track peak memory usage
#endif
};
#endif // LUA_MEM_TRACKER_H
interpreter.cpp
View file @
eb324716
...
...
@@ -15,7 +15,8 @@
#include "interpreter.h"
interpreter
::
interpreter
(
duel
*
pd
)
:
coroutines
(
256
)
{
lua_state
=
luaL_newstate
();
mem_tracker
=
new
LuaMemTracker
(
YGOPRO_LUA_MEMORY_SIZE
);
lua_state
=
lua_newstate
(
LuaMemTracker
::
AllocThunk
,
mem_tracker
);
current_state
=
lua_state
;
pduel
=
pd
;
std
::
memcpy
(
lua_getextraspace
(
lua_state
),
&
pd
,
LUA_EXTRASPACE
);
//set_duel_info
...
...
@@ -54,6 +55,7 @@ interpreter::interpreter(duel* pd): coroutines(256) {
}
interpreter
::~
interpreter
()
{
lua_close
(
lua_state
);
delete
mem_tracker
;
}
void
interpreter
::
register_card
(
card
*
pcard
)
{
if
(
!
pcard
)
...
...
interpreter.h
View file @
eb324716
...
...
@@ -16,6 +16,7 @@
#include <list>
#include <vector>
#include <cstdio>
#include "LuaMemTracker.h"
class
card
;
struct
card_data
;
...
...
@@ -47,6 +48,7 @@ public:
char
msgbuf
[
64
];
lua_State
*
lua_state
;
lua_State
*
current_state
;
LuaMemTracker
*
mem_tracker
;
param_list
params
;
param_list
resumes
;
coroutine_map
coroutines
;
...
...
@@ -97,4 +99,8 @@ public:
#define COROUTINE_YIELD 2
#define COROUTINE_ERROR 3
#ifndef YGOPRO_LUA_MEMORY_SIZE
#define YGOPRO_LUA_MEMORY_SIZE 67108864 // 64 MB
#endif
#endif
/* INTERPRETER_H_ */
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment