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
nanahira
ygopro-core
Commits
9e1acf78
Commit
9e1acf78
authored
Aug 20, 2025
by
nanahira
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'patch-memtrack' into develop
parents
ce8c6540
8a8434e8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
10 deletions
+50
-10
LuaMemTracker.cpp
LuaMemTracker.cpp
+50
-10
No files found.
LuaMemTracker.cpp
View file @
9e1acf78
...
@@ -34,24 +34,64 @@ void* LuaMemTracker::AllocThunk(void* ud, void* ptr, size_t osize, size_t nsize)
...
@@ -34,24 +34,64 @@ void* LuaMemTracker::AllocThunk(void* ud, void* ptr, size_t osize, size_t nsize)
}
}
void
*
LuaMemTracker
::
Alloc
(
void
*
ptr
,
size_t
osize
,
size_t
nsize
)
{
void
*
LuaMemTracker
::
Alloc
(
void
*
ptr
,
size_t
osize
,
size_t
nsize
)
{
if
(
nsize
==
0
)
{
auto
shrink
=
[
&
](
size_t
dec
)
{
// Clamp subtraction to avoid size_t underflow.
if
(
dec
>
total_allocated
)
{
total_allocated
=
0
;
}
else
{
total_allocated
-=
dec
;
}
};
if
(
nsize
==
0
)
{
// free
if
(
ptr
)
{
if
(
ptr
)
{
total_allocated
-=
osize
;
shrink
(
osize
)
;
}
}
return
real_alloc
(
real_ud
,
ptr
,
osize
,
nsize
);
return
real_alloc
(
real_ud
,
ptr
,
osize
,
nsize
);
}
else
{
}
size_t
projected
=
total_allocated
-
osize
+
nsize
;
if
(
limit
&&
projected
>
limit
)
{
// Unified path:
return
nullptr
;
// over limit
// - grow > 0: check limit using safe inequality, then alloc; on success add 'grow'
// - grow = 0: just alloc; accounting unchanged
// - shrink > 0: shrink first, alloc; on failure, rollback the shrink
auto
do_alloc
=
[
&
](
size_t
grow_amount
,
size_t
shrink_amount
)
->
void
*
{
if
(
grow_amount
>
0
&&
limit
)
{
if
(
total_allocated
>=
limit
||
grow_amount
>
(
limit
-
total_allocated
))
{
return
nullptr
;
}
}
}
void
*
newptr
=
real_alloc
(
real_ud
,
ptr
,
osize
,
nsize
);
if
(
newptr
)
{
auto
result
=
real_alloc
(
real_ud
,
ptr
,
osize
,
nsize
);
total_allocated
=
projected
;
if
(
result
)
{
if
(
shrink_amount
>
0
)
shrink
(
shrink_amount
);
if
(
grow_amount
>
0
)
total_allocated
+=
grow_amount
;
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
write_log
();
write_log
();
#endif
#endif
}
}
return
newptr
;
return
result
;
};
if
(
ptr
)
{
// realloc path: osize is the real old size
if
(
nsize
>
osize
)
{
// growth
size_t
grow
=
nsize
-
osize
;
return
do_alloc
(
grow
,
0
);
}
else
if
(
nsize
<
osize
)
{
// shrink
size_t
dec
=
osize
-
nsize
;
return
do_alloc
(
0
,
dec
);
}
else
{
// no net change
return
do_alloc
(
0
,
0
);
}
}
else
{
// malloc path: osize is a type tag; only count nsize
return
do_alloc
(
nsize
,
0
);
}
}
}
}
...
...
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