FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/src/misc/memory_model.cpp at revert_fence · 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
/
memory_model.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
431 lines (393 loc) · 14 KB
Breadcrumbs
daScript
/
src
/
misc
/
memory_model.cpp
Copy path
File metadata and controls
431 lines (393 loc) · 14 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#
include
"
daScript/misc/platform.h
"
#
include
"
daScript/misc/memory_model.h
"
#
include
"
daScript/misc/debug_break.h
"
namespace
das
{
#
if
DAS_TRACK_ALLOCATIONS
uint64_t
g_tracker =
0
;
uint64_t
g_breakpoint= -
1ul
;
void
das_track_breakpoint
(
uint64_t
id ) {
g_breakpoint = id;
}
#
endif
#
if
DAS_TRACK_INSANE_POINTERS
void
* das_insane_pointer =
nullptr
;
das_hash_map<
void
*,
void
*> g_insane_pointers;
//
pointer -> who allocated it
mutex g_insane_mutex;
void
das_track_insane_pointer
(
void
* ptr ) {
lock_guard<mutex>
lock
(g_insane_mutex);
das_insane_pointer = ptr;
}
void
inscribeInsanePointer
(
void
* ptr,
void
* who ) {
lock_guard<mutex>
lock
(g_insane_mutex);
if
( ptr == das_insane_pointer ) {
os_debug_break
();
}
auto
it = g_insane_pointers.
find
(ptr);
if
( it == g_insane_pointers.
end
() ) {
g_insane_pointers[ptr] = who;
}
else
{
DAS_FATAL_ERROR
(
"
insane pointer %p, already allocated by %p (and not by %p)
"
, ptr, it->
second
, who);
}
}
void
freeInsanePointer
(
void
* ptr ) {
lock_guard<mutex>
lock
(g_insane_mutex);
auto
it = g_insane_pointers.
find
(ptr);
if
( it != g_insane_pointers.
end
() ) {
g_insane_pointers.
erase
(it);
}
else
{
DAS_FATAL_ERROR
(
"
freeing insane pointer %p, which was not allocated
"
, ptr);
}
}
#
define
INSCRIBE_INSANE_POINTER
(
ptr,model
) inscribeInsanePointer(ptr,model)
#
define
FREE_INSANE_POINTER
(
ptr
) freeInsanePointer(ptr)
#
else
#
define
INSCRIBE_INSANE_POINTER
(
ptr,model
)
#
define
FREE_INSANE_POINTER
(
ptr
)
#
endif
MemoryModel::MemoryModel
() {
alignMask =
15
;
totalAllocated =
0
;
maxAllocated =
0
;
}
MemoryModel::~MemoryModel
() {
shoe.
clear
();
for
(
auto
& itb : bigStuff ) {
FREE_INSANE_POINTER
(itb.
first
);
das_aligned_free16
(itb.
first
);
}
bigStuff.
clear
();
#
if
DAS_SANITIZER
for
(
auto
& itb : deletedBigStuff ) {
FREE_INSANE_POINTER
(itb.
first
);
das_aligned_free16
(itb.
first
);
}
deletedBigStuff.
clear
();
#
endif
}
void
MemoryModel::setInitialSize
(
uint64_t
size ) {
initialSize = size;
}
void
MemoryModel::setTrackAllocations
(
bool
on ) {
#
if
DAS_TRACK_ALLOCATIONS
DAS_ASSERTF
(totalAllocated ==
0
&& bigStuff.
empty
() && shoe.
depth
() ==
0
,
"
setTrackAllocations must be called before any allocation
"
);
trackAllocations = on;
maxShoeAllocation = on ?
0u
:
uint32_t
(
DAS_MAX_SHOE_ALLOCATION
);
#
else
(
void
)on;
DAS_ASSERTF
(!on,
"
DAS_TRACK_ALLOCATIONS=0 at compile time, cannot enable at runtime
"
);
#
endif
}
uint64_t
MemoryModel::grow
(
uint32_t
si ) {
if
( shoe.
chunks
[si] ) {
uint64_t
size = shoe.
chunks
[si]->
total
;
if
( customGrow ) {
size =
customGrow
(size);
}
else
{
size = size *
2
;
}
return
size;
}
else
{
if
( !initialSize ) {
initialSize = default_initial_size;
}
//
Entry count for the first chunk of this size-class. 64-bit throughout
//
(Deck::total and totalBytes are 64-bit), so no UINT32 clamp is needed.
return
initialSize / ((
uint64_t
(si)+
1
)<<
4
);
}
}
char
*
MemoryModel::allocate
(
uint64_t
size ) {
if
( !size )
return
nullptr
;
size = (size + alignMask) & ~alignMask;
totalAllocated += size;
maxAllocated =
das::max
(maxAllocated, totalAllocated);
if
( size > maxShoeAllocation ) {
char
* ptr = (
char
*)
das_aligned_alloc16
(size);
bigStuff[ptr] = size;
#
if
DAS_TRACK_ALLOCATIONS
if
( trackAllocations ) {
INSCRIBE_INSANE_POINTER
(ptr,
this
);
if
( g_tracker==g_breakpoint )
os_debug_break
();
bigStuffId[ptr] = g_tracker ++;
}
#
endif
return
ptr;
}
else
{
if
(
char
* res = shoe.
allocate
(
uint32_t
(size)) ) {
return
res;
}
size = (size +
15
) & ~
15
;
DAS_ASSERT
(size && size<=
DAS_MAX_SHOE_ALLOCATION
);
uint32_t
si =
uint32_t
((size >>
4
) -
1
);
uint64_t
total =
grow
(si);
shoe.
chunks
[si] =
new
Deck
(total,
uint32_t
(size), shoe.
chunks
[si]);
return
shoe.
chunks
[si]->
allocate
();
}
}
bool
MemoryModel::free
(
char
* ptr,
uint64_t
size ) {
if
( !size )
return
true
;
size = (size + alignMask) & ~alignMask;
#
if
DAS_SANITIZER
memset
(ptr,
0xcd
, size);
#
endif
if
( size <= maxShoeAllocation ) {
shoe.
free
(ptr,
uint32_t
(size));
totalAllocated -= size;
return
true
;
}
#
if
DAS_SANITIZER
auto
itd = deletedBigStuff.
find
(ptr);
if
( itd!= deletedBigStuff.
end
() ) {
os_debug_break
();
}
#
endif
auto
itb = bigStuff.
find
(ptr);
if
( itb!=bigStuff.
end
() ) {
DAS_ASSERTF
(itb->
second
==size,
"
free size mismatch, %llu allocated vs %llu freed
"
, (
unsigned
long
long
)itb->
second
, (
unsigned
long
long
)size );
#
if
DAS_SANITIZER
memset
(ptr,
0xcd
, size);
deletedBigStuff[itb->
first
] = itb->
second
;
#
else
FREE_INSANE_POINTER
(itb->
first
);
das_aligned_free16
(itb->
first
);
#
endif
bigStuff.
erase
(itb);
totalAllocated -= size;
#
if
DAS_TRACK_ALLOCATIONS
if
( trackAllocations ) {
bigStuffId.
erase
(ptr);
bigStuffAt.
erase
(ptr);
bigStuffComment.
erase
(ptr);
}
#
endif
return
true
;
}
DAS_ASSERTF
(
0
,
"
we are trying to delete pointer, which we did not allocate
"
);
return
false
;
}
char
*
MemoryModel::reallocate
(
char
* ptr,
uint64_t
size,
uint64_t
nsize ) {
if
( !ptr )
return
allocate
(nsize);
size = (size + alignMask) & ~alignMask;
nsize = (nsize + alignMask) & ~alignMask;
char
* nptr =
allocate
(nsize);
DAS_VERIFYF
(nptr,
"
out of memory?
"
);
memcpy
( nptr, ptr,
das::min
(size,nsize) );
#
if
DAS_TRACK_ALLOCATIONS
if
( trackAllocations ) {
auto
pAt = bigStuffAt.
find
(ptr);
if
( pAt != bigStuffAt.
end
() ) {
bigStuffAt[nptr] = pAt->
second
;
}
auto
pCm = bigStuffComment.
find
(ptr);
if
( pCm != bigStuffComment.
end
() ) {
bigStuffComment[nptr] = pCm->
second
;
}
}
#
endif
free
(ptr, size);
return
nptr;
}
void
MemoryModel::reset
() {
for
(
auto
& itb : bigStuff ) {
#
if
DAS_SANITIZER
memset
(itb.
first
,
0xcd
, itb.
second
);
deletedBigStuff[itb.
first
] = itb.
second
;
#
else
FREE_INSANE_POINTER
(itb.
first
);
das_aligned_free16
(itb.
first
);
#
endif
}
bigStuff.
clear
();
#
if
DAS_TRACK_ALLOCATIONS
if
( trackAllocations ) {
bigStuffId.
clear
();
bigStuffAt.
clear
();
bigStuffComment.
clear
();
}
#
endif
shoe.
reset
();
}
void
MemoryModel::shrink
() {
if
constexpr
(has_shrink_to_fit<
decltype
(bigStuff)>::value) {
bigStuff.
shrink_to_fit
();
}
#
if
DAS_TRACK_ALLOCATIONS
if
( trackAllocations ) {
if
constexpr
(has_shrink_to_fit<
decltype
(bigStuffId)>::value) {
bigStuffId.
shrink_to_fit
();
}
if
constexpr
(has_shrink_to_fit<
decltype
(bigStuffAt)>::value) {
bigStuffAt.
shrink_to_fit
();
}
if
constexpr
(has_shrink_to_fit<
decltype
(bigStuffComment)>::value) {
bigStuffComment.
shrink_to_fit
();
}
}
#
endif
}
uint64_t
MemoryModel::totalAlignedMemoryAllocated
()
const
{
uint64_t
mem = shoe.
totalBytesAllocated
();
for
(
const
auto
& it : bigStuff) {
mem += it.
second
;
}
return
mem;
}
void
MemoryModel::sweep
() {
totalAllocated =
0
;
//
When trackAllocations is on, maxShoeAllocation==0, so no shoe chunks exist
//
and this loop is naturally a no-op (chunks[si] is always nullptr).
for
(
uint32_t
si=
0
; si!=
DAS_MAX_SHOE_CUNKS
; ++si ) {
//
we re-track all small allocations
for
(
auto
ch=shoe.
chunks
[si]; ch; ch=ch->
next
) {
ch->
afterGC
();
uint64_t
utotal = ch->
total
/
32
;
#
if
DAS_SANITIZER
for
(
uint64_t
i=
0
; i!=utotal; ++i ) {
uint32_t
b = ch->
bits
[i];
for
(
uint32_t
j=
0
; j!=
32
; ++j ) {
if
( b & (
1
<<j) ) {
totalAllocated += ch->
size
;
}
else
{
memset
( ch->
data
+ (i*
32
+j)*ch->
size
,
0xcd
, ch->
size
);
}
}
}
#
else
uint64_t
live =
0
;
//
popcount instead of per-bit scan
for
(
uint64_t
i=
0
; i!=utotal; ++i ) {
live +=
das_popcount
(ch->
bits
[i]);
}
totalAllocated += live * ch->
size
;
#
endif
}
}
for
(
auto
it = bigStuff.
begin
(); it!=bigStuff.
end
() ; ) {
if
( it->
second
&
DAS_PAGE_GC_MASK
) {
it->
second
&= ~
DAS_PAGE_GC_MASK
;
totalAllocated += it->
second
;
++ it;
}
else
{
#
if
DAS_SANITIZER
memset
( it->
first
,
0xcd
, it->
second
);
deletedBigStuff[it->
first
] = it->
second
;
#
else
FREE_INSANE_POINTER
(it->
first
);
das_aligned_free16
(it->
first
);
#
endif
it = bigStuff.
erase
(it);
}
}
}
char
*
LinearChunkAllocator::reallocate
(
char
* ptr,
uint64_t
size,
uint64_t
nsize ) {
if
( !ptr )
return
allocate
(nsize);
size = (size + alignMask) & ~alignMask;
nsize = (nsize + alignMask) & ~alignMask;
//
TODO: we can 'expand' in certain cases
char
* nptr =
allocate
(nsize);
memcpy
( nptr, ptr,
das::min
(size,nsize) );
free
(ptr, size);
return
nptr;
}
void
LinearChunkAllocator::free
(
char
* ptr,
uint64_t
s ) {
s = (s + alignMask) & ~alignMask;
for
(
auto
ch=chunk; ch; ch=ch->
next
) {
if
( ch->
isOwnPtr
(ptr) ) {
ch->
free
(ptr,s);
break
;
}
}
}
uint64_t
LinearChunkAllocator::grow
(
uint64_t
size ) {
return
customGrow ?
customGrow
(size) : size *
2
;
}
char
*
LinearChunkAllocator::allocate
(
uint64_t
s ) {
if
( !s )
return
nullptr
;
s = (s + alignMask) & ~alignMask;
#
if
SIZE_MAX < UINT64_MAX
//
32-bit: no size_t allocation can back a >SIZE_MAX request. Fail it as OOM here and cap
//
chunk sizes below — otherwise HeapChunk's das_aligned_alloc16(size_t) truncates the malloc
//
while HeapChunk::size records the full uint64, handing out unbacked memory. The cap is
//
16-aligned so HeapChunk's own round-up can't push it past SIZE_MAX.
const
uint64_t
chunkCap =
uint64_t
(
SIZE_MAX
) &
~uint64_t
(
15
);
if
( s > chunkCap )
return
nullptr
;
auto
capChunk = [&](
uint64_t
sz ) {
return
das::min
(chunkCap, sz); };
#
else
auto
capChunk = [](
uint64_t
sz ) {
return
sz; };
#
endif
if
( !chunk ) {
if
( !initialSize ) {
initialSize = default_initial_size;
}
chunk =
new
HeapChunk
(
capChunk
(
das::max
(initialSize, s)),
nullptr
);
if
( !chunk->
data
) {
//
backing malloc failed: fail as OOM, don't install a dead chunk
delete
chunk;
chunk =
nullptr
;
return
nullptr
;
}
}
for
( ;; ) {
if
(
char
* res = chunk->
allocate
(s) ) {
return
res;
}
auto
nc =
new
HeapChunk
(
capChunk
(
das::max
(
grow
(chunk->
size
), s)), chunk);
if
( !nc->
data
) {
//
backing malloc failed: OOM, not an excuse to spin the grow loop
nc->
next
=
nullptr
;
//
detach — ~HeapChunk would otherwise free the whole live chain
delete
nc;
return
nullptr
;
}
chunk = nc;
}
}
void
LinearChunkAllocator::reset
() {
if
( chunk && chunk->
next
) {
auto
maxAllocated = (
bytesAllocated
()+
1023
) &
~uint64_t
(
1023
);
initialSize =
das::max
(initialSize, maxAllocated);
delete
chunk;
chunk =
nullptr
;
}
else
if
( chunk ) {
chunk->
offset
=
0
;
}
}
void
LinearChunkAllocator::shrink
() {
initialSize = unadjustedInitialSize;
if
( chunk && !chunk->
next
&& chunk->
offset
==
0
) {
delete
chunk;
chunk =
nullptr
;
}
}
char
*
LinearChunkAllocator::allocateName
(
const
string & name ) {
if
(!name.
empty
()) {
auto
length =
uint32_t
(name.
length
());
if
(
auto
str = (
char
*)
allocate
(length +
1
)) {
memcpy
(str, name.
c_str
(), length);
str[length] =
0
;
return
str;
}
}
return
nullptr
;
}
void
LinearChunkAllocator::getStats
(
uint32_t
& depth,
uint64_t
& bytes,
uint64_t
& total )
const
{
depth =
0
;
bytes =
0
;
total =
0
;
for
(
auto
ch=chunk; ch; ch=ch->
next
) {
depth ++;
bytes += ch->
offset
;
total += ch->
size
;
}
}
uint32_t
LinearChunkAllocator::depth
()
const
{
uint32_t
d;
uint64_t
b, t;
getStats
(d, b, t);
return
d;
}
uint64_t
LinearChunkAllocator::bytesAllocated
()
const
{
uint32_t
d;
uint64_t
b, t;
getStats
(d, b, t);
return
b;
}
uint64_t
LinearChunkAllocator::totalAlignedMemoryAllocated
()
const
{
uint32_t
d;
uint64_t
b, t;
getStats
(d, b, t);
return
t;
}
}
Back
|
FazBrowse Home
|
New Git URL