FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/src/misc/alloc_tracker_overrides.cpp at master · WhyNot135/daScript · GitHub
WhyNot135
/
daScript
Public
forked from
GaijinEntertainment/daScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
daScript
/
src
/
misc
/
alloc_tracker_overrides.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
134 lines (118 loc) · 6.85 KB
Breadcrumbs
daScript
/
src
/
misc
/
alloc_tracker_overrides.cpp
Copy path
File metadata and controls
134 lines (118 loc) · 6.85 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
Global operator new/delete overrides for the daslang leak tracker.
//
//
Compiled into every target participating in the leak audit (daslang,
//
daslang_static, daslang-live, libDaScript_runtime, libDaScriptDyn_runtime);
//
see CMakeLists.txt target_sources lines. The TU body is #if DAS_TRACK_ALLOC-
//
gated, so it compiles to an empty object in Release/Debug. DAS_TRACK_ALLOC
//
itself is set by generator expression to $<CONFIG:RelWithDebInfo>, i.e. the
//
tracker is permanently on in RelWithDebInfo and off everywhere else -- no
//
user-facing CMake option.
//
//
The Windows DLL model resolves operator new per-module against each module's
//
CRT, so a single override living in libDaScriptDyn_runtime only covers its
//
own code; the exe and every shared module need their own copy.
//
//
Uses std::malloc/std::free (not _aligned_malloc) for the backing store so
//
cross-module mismatches (e.g. allocated here, freed through a module that
//
doesn't share our override) stay ABI-compatible with the default CRT
//
allocator and don't corrupt the heap. The tracker's own map records and
//
track_free_hook reports orphan frees for diagnostic visibility.
//
//
Full overload set: C++14 sized delete and C++17 aligned new/delete are
//
replaced alongside the classic unsized forms so every compiler-emitted
//
deallocation funnels through track_free_hook. A missing overload would let
//
the default runtime free the pointer directly, bypassing the hook and
//
leaving a stale entry that would be reported as a (false) leak.
#
include
"
daScript/misc/platform.h
"
#
if
DAS_TRACK_ALLOC
#
include
<
cstdlib
>
#
include
<
cstdint
>
#
include
<
new
>
//
-----------------------------------------------------------------------------
//
Aligned allocation helper. std::aligned_alloc is POSIX-only and requires size
//
to be a multiple of alignment. On MSVC we use _aligned_malloc / _aligned_free.
//
Aligned pointers must be freed through the matching aligned API (the CRT
//
stores bookkeeping before the returned pointer). We track the allocation in
//
the usual map so track_free_hook fires as for any other alloc.
//
-----------------------------------------------------------------------------
static
void
*
track_new_impl
(
size_t
size) {
void
*p =
std::malloc
(size ? size :
1
);
das::track_alloc_hook
(p, size);
return
p;
}
static
void
track_delete_impl
(
void
*p)
noexcept
{
if
(!p)
return
;
das::track_free_hook
(p);
std::free
(p);
}
#
if
defined(_MSC_VER)
#
include
<
malloc.h
>
static
void
*
track_new_aligned_impl
(
size_t
size,
size_t
alignment) {
if
(size ==
0
) size =
1
;
if
(alignment <
sizeof
(
void
*)) alignment =
sizeof
(
void
*);
void
*p =
_aligned_malloc
(size, alignment);
das::track_alloc_hook
(p, size);
return
p;
}
static
void
track_delete_aligned_impl
(
void
*p)
noexcept
{
if
(!p)
return
;
das::track_free_hook
(p);
_aligned_free
(p);
}
#
else
static
void
*
track_new_aligned_impl
(
size_t
size,
size_t
alignment) {
if
(size ==
0
) size = alignment;
//
posix_memalign / aligned_alloc both require alignment to be a power of
//
two and a multiple of sizeof(void*).
if
(alignment <
sizeof
(
void
*)) alignment =
sizeof
(
void
*);
//
aligned_alloc requires size to be a multiple of alignment.
size_t
rounded = (size + alignment -
1
) & ~(alignment -
1
);
void
*p =
std::aligned_alloc
(alignment, rounded);
das::track_alloc_hook
(p, size);
return
p;
}
static
void
track_delete_aligned_impl
(
void
*p)
noexcept
{
if
(!p)
return
;
das::track_free_hook
(p);
std::free
(p);
}
#
endif
//
-----------------------------------------------------------------------------
//
Plain new / delete (unsized)
//
-----------------------------------------------------------------------------
void
*
operator
new
(
size_t
size) {
return
track_new_impl
(size); }
void
*
operator
new
[](
size_t
size) {
return
track_new_impl
(size); }
void
*
operator
new
(
size_t
size, std::
nothrow_t
const
&)
noexcept
{
return
track_new_impl
(size); }
void
*
operator
new
[](
size_t
size, std::
nothrow_t
const
&)
noexcept
{
return
track_new_impl
(size); }
#
ifdef
__APPLE__
void
operator
delete
(
void
*p) _NOEXCEPT {
track_delete_impl
(p); }
void
operator
delete[]
(
void
*p) _NOEXCEPT {
track_delete_impl
(p); }
void
operator
delete
(
void
*p, std::
nothrow_t
const
&) _NOEXCEPT {
track_delete_impl
(p); }
void
operator
delete[]
(
void
*p, std::
nothrow_t
const
&) _NOEXCEPT {
track_delete_impl
(p); }
#
else
void
operator
delete
(
void
*p)
noexcept
{
track_delete_impl
(p); }
void
operator
delete[]
(
void
*p)
noexcept
{
track_delete_impl
(p); }
void
operator
delete
(
void
*p, std::
nothrow_t
const
&)
noexcept
{
track_delete_impl
(p); }
void
operator
delete[]
(
void
*p, std::
nothrow_t
const
&)
noexcept
{
track_delete_impl
(p); }
#
endif
//
Sized delete (C++14). MSVC emits calls to these whenever it knows the static
//
size of the type being deleted; without our replacement the default CRT
//
deallocator runs and bypasses track_free_hook.
void
operator
delete
(
void
*p,
size_t
/*
size
*/
)
noexcept
{
track_delete_impl
(p); }
void
operator
delete[]
(
void
*p,
size_t
/*
size
*/
)
noexcept
{
track_delete_impl
(p); }
//
-----------------------------------------------------------------------------
//
Aligned new / delete (C++17). Emitted for types whose alignof exceeds
//
__STDCPP_DEFAULT_NEW_ALIGNMENT__ (16 on x64). Separate allocator because
//
MSVC's _aligned_malloc stores bookkeeping before the returned pointer.
//
-----------------------------------------------------------------------------
void
*
operator
new
(
size_t
size, std::
align_val_t
al) {
return
track_new_aligned_impl
(size, (
size_t
)al); }
void
*
operator
new
[](
size_t
size, std::
align_val_t
al) {
return
track_new_aligned_impl
(size, (
size_t
)al); }
void
*
operator
new
(
size_t
size, std::
align_val_t
al, std::
nothrow_t
const
&)
noexcept
{
return
track_new_aligned_impl
(size, (
size_t
)al); }
void
*
operator
new
[](
size_t
size, std::
align_val_t
al, std::
nothrow_t
const
&)
noexcept
{
return
track_new_aligned_impl
(size, (
size_t
)al); }
void
operator
delete
(
void
*p, std::
align_val_t
)
noexcept
{
track_delete_aligned_impl
(p); }
void
operator
delete[]
(
void
*p, std::
align_val_t
)
noexcept
{
track_delete_aligned_impl
(p); }
void
operator
delete
(
void
*p, std::
align_val_t
, std::
nothrow_t
const
&)
noexcept
{
track_delete_aligned_impl
(p); }
void
operator
delete[]
(
void
*p, std::
align_val_t
, std::
nothrow_t
const
&)
noexcept
{
track_delete_aligned_impl
(p); }
void
operator
delete
(
void
*p,
size_t
/*
size
*/
, std::
align_val_t
)
noexcept
{
track_delete_aligned_impl
(p); }
void
operator
delete[]
(
void
*p,
size_t
/*
size
*/
, std::
align_val_t
)
noexcept
{
track_delete_aligned_impl
(p); }
#
endif
//
DAS_TRACK_ALLOC
Back
|
FazBrowse Home
|
New Git URL