FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/src/misc/memory_model.cpp at master · throwthisaway/daScript · GitHub
throwthisaway
/
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
309 lines (283 loc) · 9.15 KB
Breadcrumbs
daScript
/
src
/
misc
/
memory_model.cpp
Copy path
File metadata and controls
309 lines (283 loc) · 9.15 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
#
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
MemoryModel::MemoryModel
() {
alignMask =
15
;
totalAllocated =
0
;
maxAllocated =
0
;
}
MemoryModel::~MemoryModel
() {
shoe.
clear
();
for
(
auto
& itb : bigStuff ) {
das_aligned_free16
(itb.
first
);
}
bigStuff.
clear
();
#
if
DAS_SANITIZER
for
(
auto
& itb : deletedBigStuff ) {
das_aligned_free16
(itb.
first
);
}
deletedBigStuff.
clear
();
#
endif
}
void
MemoryModel::setInitialSize
(
uint32_t
size ) {
initialSize = size;
}
uint32_t
MemoryModel::grow
(
uint32_t
si ) {
if
( shoe.
chunks
[si] ) {
uint32_t
size = shoe.
chunks
[si]->
total
;
if
( customGrow ) {
size =
customGrow
(size);
}
else
{
size = size *
2
;
}
return
size;
}
else
{
if
( !initialSize ) {
initialSize = default_initial_size;
}
return
initialSize / ((si+
1
)<<
4
);
//
fit in initial size
}
}
char
*
MemoryModel::allocate
(
uint32_t
size ) {
if
( !size )
return
nullptr
;
size = (size + alignMask) & ~alignMask;
totalAllocated += size;
maxAllocated =
das::max
(maxAllocated, totalAllocated);
#
if
!DAS_TRACK_ALLOCATIONS
if
( size >
DAS_MAX_SHOE_ALLOCATION
) {
#endif
char
* ptr = (
char
*)
das_aligned_alloc16
(size);
bigStuff[ptr] = size;
#
if
DAS_TRACK_ALLOCATIONS
if
( g_tracker==g_breakpoint )
os_debug_break
();
bigStuffId[ptr] = g_tracker ++;
#
endif
return
ptr;
#
if
!DAS_TRACK_ALLOCATIONS
}
else
{
if
(
char
* res = shoe.
allocate
(size) ) {
return
res;
}
size = (size +
15
) & ~
15
;
DAS_ASSERT
(size && size<=
DAS_MAX_SHOE_ALLOCATION
);
uint32_t
si = (size >>
4
) -
1
;
uint32_t
total =
grow
(si);
shoe.
chunks
[si] =
new
Deck
(total, size, shoe.
chunks
[si]);
return
shoe.
chunks
[si]->
allocate
();
}
#
endif
}
bool
MemoryModel::free
(
char
* ptr,
uint32_t
size ) {
if
( !size )
return
true
;
size = (size + alignMask) & ~alignMask;
#
if
DAS_SANITIZER
memset
(ptr,
0xcd
, size);
#
endif
#
if
!DAS_TRACK_ALLOCATIONS
if
( size <=
DAS_MAX_SHOE_ALLOCATION
) {
shoe.
free
(ptr, size);
totalAllocated -= size;
return
true
;
}
#
endif
#
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, %u allocated vs %u freed
"
, itb->
second
, size );
#
if
DAS_SANITIZER
deletedBigStuff[itb->
first
] = itb->
second
;
#
else
das_aligned_free16
(itb->
first
);
#
endif
bigStuff.
erase
(itb);
totalAllocated -= size;
#
if
DAS_TRACK_ALLOCATIONS
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,
uint32_t
size,
uint32_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
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
deletedBigStuff[itb.
first
] = itb.
second
;
#
else
das_aligned_free16
(itb.
first
);
#
endif
}
bigStuff.
clear
();
#
if
DAS_TRACK_ALLOCATIONS
bigStuffId.
clear
();
bigStuffAt.
clear
();
bigStuffComment.
clear
();
#
endif
shoe.
reset
();
}
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
;
#
if
!DAS_TRACK_ALLOCATIONS
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
();
uint32_t
utotal = ch->
total
/
32
;
for
(
uint32_t
i=
0
; i!=utotal; ++i ) {
uint32_t
b = ch->
bits
[i];
for
(
uint32_t
j=
0
; j!=
32
; ++j ) {
//
TODO: this is COUNTBITS * size
if
( b & (
1
<<j) ) {
totalAllocated += ch->
size
;
}
else
{
#
if
DAS_SANITIZER
memset
( ch->
data
+ (i*
32
+j)*ch->
size
,
0xcd
, ch->
size
);
#
endif
}
}
}
}
}
#
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
);
#
endif
das_aligned_free16
(it->
first
);
it = bigStuff.
erase
(it);
}
}
}
char
*
LinearChunkAllocator::reallocate
(
char
* ptr,
uint32_t
size,
uint32_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,
uint32_t
s ) {
s = (s + alignMask) & ~alignMask;
for
(
auto
ch=chunk; ch; ch=ch->
next
) {
if
( ch->
isOwnPtr
(ptr) ) {
ch->
free
(ptr,s);
break
;
}
}
}
uint32_t
LinearChunkAllocator::grow
(
uint32_t
size ) {
return
customGrow ?
customGrow
(size) : size *
2
;
}
char
*
LinearChunkAllocator::allocate
(
uint32_t
s ) {
if
( !s )
return
nullptr
;
s = (s + alignMask) & ~alignMask;
if
( !chunk ) {
if
( !initialSize ) {
initialSize = default_initial_size;
}
chunk =
new
HeapChunk
(
das::max
(initialSize, s),
nullptr
);
//
printf("[HC] %i\n", chunk->size);
}
for
( ;; ) {
if
(
char
* res = chunk->
allocate
(s) ) {
//
printf("[A] %i bytes, offs=%i\n", int(s), int(res-chunk->data));
return
res;
}
chunk =
new
HeapChunk
(
das::max
(
grow
(chunk->
size
), s), chunk);
//
printf("[HC] %i bytes\n", chunk->size);
}
}
void
LinearChunkAllocator::reset
() {
if
( chunk && chunk->
next
) {
auto
maxAllocated = (
uint32_t
(
bytesAllocated
())+
1023
) & ~
1023
;
initialSize =
das::max
(initialSize, maxAllocated);
delete
chunk;
chunk =
nullptr
;
}
else
if
( chunk ) {
chunk->
offset
=
0
;
}
}
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