-
Notifications
You must be signed in to change notification settings - Fork 6
/
memory.c
320 lines (270 loc) · 8.78 KB
/
memory.c
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
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* This Original Work is licensed under the European Union Public Licence
* (EUPL) v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
#include "memory.h"
#include "fiftyone.h"
#ifndef FIFTYONE_DEGREES_MEMORY_TRACKER_SHARDS
#ifndef FIFTYONE_DEGREES_NO_THREADING
#define FIFTYONE_DEGREES_MEMORY_TRACKER_SHARDS 20
#else
#define FIFTYONE_DEGREES_MEMORY_TRACKER_SHARDS 1
#endif
#else
#endif
static int shardDivider = sizeof(void*) * 3;
static bool initialised = false;
typedef struct memory_allocation_t {
fiftyoneDegreesTreeNode tree; /* Tree node data structure */
uint32_t size; /* The amount of memory allocated at pointer */
} allocation;
typedef struct memory_allocation_tree_t {
fiftyoneDegreesTreeRoot roots[FIFTYONE_DEGREES_MEMORY_TRACKER_SHARDS];
#ifndef FIFTYONE_DEGREES_NO_THREADING
fiftyoneDegreesMutex locks[FIFTYONE_DEGREES_MEMORY_TRACKER_SHARDS];
fiftyoneDegreesMutex lock;
#endif
size_t allocated;
size_t max;
} allocationTree;
static allocationTree state;
bool fiftyoneDegreesMemoryAdvance(
fiftyoneDegreesMemoryReader *reader,
size_t advanceBy) {
if (reader == NULL || reader->current == NULL) {
return false;
}
reader->current += advanceBy;
if (reader->current > reader->lastByte) {
return false;
}
return true;
}
void* fiftyoneDegreesMemoryStandardMalloc(size_t size) {
return malloc(size);
}
void* fiftyoneDegreesMemoryStandardMallocAligned(int alignment, size_t size) {
size += size % alignment;
#if defined( _MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
return _aligned_malloc(size, alignment);
#elif defined (__APPLE__)
void *pointer;
if (posix_memalign(&pointer, alignment, size) == 0) {
return pointer;
}
else {
return NULL;
}
#else
return aligned_alloc(alignment, size);
#endif
}
static int getShardFromPointer(void *pointer) {
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
#endif
return (((uint64_t)pointer) / shardDivider) %
FIFTYONE_DEGREES_MEMORY_TRACKER_SHARDS;
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
}
static void tryInit() {
if (initialised == false) {
MemoryTrackingReset();
}
}
static void trackAllocation(void* pointer, size_t size) {
// Create a new tree node to record the allocation.
allocation* record = (allocation*)malloc(sizeof(allocation));
int shard = getShardFromPointer(pointer);
assert(record != NULL);
assert(shard < FIFTYONE_DEGREES_MEMORY_TRACKER_SHARDS);
fiftyoneDegreesTreeNodeInit(&record->tree, &state.roots[shard]);
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
#endif
record->tree.key = (int64_t)pointer;
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
record->size = (uint32_t)size;
// Update the tracking tree with the new allocation.
#ifndef FIFTYONE_DEGREES_NO_THREADING
FIFTYONE_DEGREES_MUTEX_LOCK(&state.locks[shard]);
#endif
fiftyoneDegreesTreeInsert(&record->tree);
#ifndef FIFTYONE_DEGREES_NO_THREADING
FIFTYONE_DEGREES_MUTEX_UNLOCK(&state.locks[shard]);
#endif
// Update the allocated and max values.
#ifndef FIFTYONE_DEGREES_NO_THREADING
FIFTYONE_DEGREES_MUTEX_LOCK(&state.lock);
#endif
state.allocated += size;
if (state.allocated > state.max) {
state.max = state.allocated;
}
#ifndef FIFTYONE_DEGREES_NO_THREADING
FIFTYONE_DEGREES_MUTEX_UNLOCK(&state.lock);
#endif
}
static void untrackAllocation(void *pointer) {
uint32_t size;
int shard = getShardFromPointer(pointer);
// Get the size of the memory being freed and free the tracking memory.
#ifndef FIFTYONE_DEGREES_NO_THREADING
FIFTYONE_DEGREES_MUTEX_LOCK(&state.locks[shard]);
#endif
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
#endif
allocation* record = (allocation*)fiftyoneDegreesTreeFind(
&state.roots[shard],
(int64_t)pointer);
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
assert(record != NULL);
size = record->size;
fiftyoneDegreesTreeDelete(&record->tree);
#ifndef FIFTYONE_DEGREES_NO_THREADING
FIFTYONE_DEGREES_MUTEX_UNLOCK(&state.locks[shard]);
#endif
free(record);
#ifndef FIFTYONE_DEGREES_NO_THREADING
FIFTYONE_DEGREES_MUTEX_LOCK(&state.lock);
#endif
state.allocated -= size;
#ifndef FIFTYONE_DEGREES_NO_THREADING
FIFTYONE_DEGREES_MUTEX_UNLOCK(&state.lock);
#endif
}
void* fiftyoneDegreesMemoryTrackingMallocAligned(
int alignment,
size_t size) {
// Check that the tracker is initialised.
tryInit();
// Allocate the memory.
void* pointer = MemoryStandardMallocAligned(
alignment,
size);
// Track the allocation.
trackAllocation(pointer, size + size % alignment);
return pointer;
}
void* fiftyoneDegreesMemoryTrackingMalloc(size_t size) {
// Check that the tracker is initialised.
tryInit();
// Allocate the memory.
void* pointer = MemoryStandardMalloc(size);
// Track the allocation.
trackAllocation(pointer, size);
return pointer;
}
void fiftyoneDegreesMemoryStandardFree(void *pointer) {
free(pointer);
}
void fiftyoneDegreesMemoryStandardFreeAligned(void* pointer) {
#if defined( _MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
_aligned_free(pointer);
#else
free(pointer);
#endif
}
void fiftyoneDegreesMemoryTrackingFree(void *pointer) {
untrackAllocation(pointer);
// Finally free the pointer.
MemoryStandardFree(pointer);
}
void fiftyoneDegreesMemoryTrackingFreeAligned(void* pointer) {
untrackAllocation(pointer);
// Finally free the pointer.
MemoryStandardFreeAligned(pointer);
}
size_t fiftyoneDegreesMemoryTrackingGetMax() {
return state.max;
}
size_t fiftyoneDegreesMemoryTrackingGetAllocated() {
return state.allocated;
}
void fiftyoneDegreesMemoryTrackingReset() {
int i;
if (initialised == false) {
for (i = 0; i < FIFTYONE_DEGREES_MEMORY_TRACKER_SHARDS; i++) {
#ifndef FIFTYONE_DEGREES_NO_THREADING
FIFTYONE_DEGREES_MUTEX_CREATE(state.locks[i]);
#endif
fiftyoneDegreesTreeRootInit(&state.roots[i]);
}
#ifndef FIFTYONE_DEGREES_NO_THREADING
FIFTYONE_DEGREES_MUTEX_CREATE(state.lock);
#endif
}
state.allocated = 0;
state.max = 0;
initialised = true;
}
void fiftyoneDegreesSetUpMemoryTracking() {
fiftyoneDegreesMemoryTrackingReset();
fiftyoneDegreesMalloc = fiftyoneDegreesMemoryTrackingMalloc;
fiftyoneDegreesMallocAligned = fiftyoneDegreesMemoryTrackingMallocAligned;
fiftyoneDegreesFree = fiftyoneDegreesMemoryTrackingFree;
fiftyoneDegreesFreeAligned = fiftyoneDegreesMemoryTrackingFreeAligned;
}
size_t fiftyoneDegreesUnsetMemoryTracking() {
size_t memAlloced = MemoryTrackingGetAllocated();
fiftyoneDegreesMalloc = fiftyoneDegreesMemoryStandardMalloc;
fiftyoneDegreesMallocAligned = fiftyoneDegreesMemoryStandardMallocAligned;
fiftyoneDegreesFree = fiftyoneDegreesMemoryStandardFree;
fiftyoneDegreesFreeAligned = fiftyoneDegreesMemoryStandardFreeAligned;
fiftyoneDegreesMemoryTrackingReset();
return memAlloced;
}
#ifdef FIFTYONE_DEGREES_MEMORY_TRACK_ENABLED
/**
* Enable memory tracking.
*/
void *(FIFTYONE_DEGREES_CALL_CONV *fiftyoneDegreesMalloc)(size_t size) =
fiftyoneDegreesMemoryTrackingMalloc;
void* (FIFTYONE_DEGREES_CALL_CONV* fiftyoneDegreessMallocAligned)(
int alignment,
size_t size) = fiftyoneDegreesMemoryTrackingMallocAligned;
void (FIFTYONE_DEGREES_CALL_CONV *fiftyoneDegreesFree)(void* pointer) =
fiftyoneDegreesMemoryTrackingFree;
void (FIFTYONE_DEGREES_CALL_CONV *fiftyoneDegreesFreeAligned)(void* pointer) =
fiftyoneDegreesMemoryTrackingFreeAligned;
#else
/**
* Disable memory tracking.
*/
void* (FIFTYONE_DEGREES_CALL_CONV* fiftyoneDegreesMalloc)(size_t size) =
fiftyoneDegreesMemoryStandardMalloc;
void* (FIFTYONE_DEGREES_CALL_CONV* fiftyoneDegreesMallocAligned)(
int alignment,
size_t size) = fiftyoneDegreesMemoryStandardMallocAligned;
void (FIFTYONE_DEGREES_CALL_CONV *fiftyoneDegreesFree)(void *pointer) =
fiftyoneDegreesMemoryStandardFree;
void (FIFTYONE_DEGREES_CALL_CONV *fiftyoneDegreesFreeAligned)(void* pointer) =
fiftyoneDegreesMemoryStandardFreeAligned;
#endif