-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyth_malloc_wrapper.c
More file actions
372 lines (348 loc) · 9.77 KB
/
Copy pathmyth_malloc_wrapper.c
File metadata and controls
372 lines (348 loc) · 9.77 KB
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
#include <dlfcn.h>
#include <stdlib.h>
#include "myth_config.h"
#include "myth_sched.h"
#include "myth_sched_func.h"
#ifdef MYTH_WRAP_MALLOC
int g_alloc_hook_ok=0;
myth_freelist_t **g_myth_malloc_wrapper_fl;
typedef union malloc_wrapper_header{
struct{
uint64_t fl_index;
void *org_ptr;
}s;
uint8_t c[16];
}malloc_wrapper_header,*malloc_wrapper_header_t;
void myth_malloc_wrapper_init(int nthreads)
{
#ifdef MYTH_WRAP_MALLOC_RUNTIME
/* check if the user wants to wrap malloc.
if not return without doing anything */
if (!g_wrap_malloc) return;
#endif
assert(real_malloc);
g_myth_malloc_wrapper_fl=real_malloc(sizeof(myth_freelist_t*)*nthreads);
}
void myth_malloc_wrapper_init_worker(int rank)
{
#ifdef MYTH_WRAP_MALLOC_RUNTIME
/* is it possible to come here before myth_malloc_wrapper_init is called? */
if (!g_wrap_malloc) return;
#endif
int i;
//allocate freelist
assert(real_malloc);
g_myth_malloc_wrapper_fl[rank]=real_malloc(sizeof(myth_freelist_t)*FREE_LIST_NUM);
//initialize
for (i=0;i<FREE_LIST_NUM;i++){myth_freelist_init(g_myth_malloc_wrapper_fl[rank][i]);}
__sync_fetch_and_add(&g_alloc_hook_ok,1);
}
void myth_malloc_wrapper_fini()
{
#ifdef MYTH_WRAP_MALLOC_RUNTIME
/* is it possible to come here before myth_malloc_wrapper_init is called? */
if (!g_wrap_malloc) return;
#endif
real_free(g_myth_malloc_wrapper_fl);
}
void myth_malloc_wrapper_fini_worker(int rank)
{
#ifdef MYTH_WRAP_MALLOC_RUNTIME
/* is it possible to come here before myth_malloc_wrapper_init is called? */
if (!g_wrap_malloc) return;
#endif
//Release freelist contents
/*for (i=0;i<FREE_LIST_NUM;i++){
}*/
//Release the array
real_free(g_myth_malloc_wrapper_fl[rank]);
__sync_fetch_and_sub(&g_alloc_hook_ok,1);
}
#ifdef MYTH_WRAP_MALLOC_RUNTIME
/* the fall-back region used by malloc/calloc/realloc/valloc/posix_memalign
when called before wrappers are in place. in particular they are used
when called by dlsym.
if we run out of memory for them, we are simply out of luck.
below we allocate 16KB, but it may be too much.
(in one experiment, it only allocated 32 bytes before wrapping
is complete).
TODO: we should make it dynamically adjustable.
*/
#define SYS_ALLOC_REGION_SIZE (1 << 14)
char g_sys_alloc_region[SYS_ALLOC_REGION_SIZE];
char * volatile g_sys_alloc_region_ptr = g_sys_alloc_region;
char * g_sys_alloc_region_end = g_sys_alloc_region + SYS_ALLOC_REGION_SIZE;
/* return true if ptr is in the g_sys_alloc_region array. */
int sys_alloc_region(void * ptr) {
if ((char *)ptr < g_sys_alloc_region) return 0;
if ((char *)ptr >= g_sys_alloc_region + SYS_ALLOC_REGION_SIZE) return 0;
return 1;
}
/* a generic, fall-back allocator used until malloc wrapping is incomplete.
it is the simplest pointer bumping allocator, which cannot free.
NOTE: you should not use any function that may in turn call any memory
allocator (malloc etc.). doing so may result in infinite recursions
(and stack overflow). */
void * sys_alloc_align(size_t alignment, size_t size) {
while (1) {
char * p = g_sys_alloc_region_ptr;
char * q = p + alignment - 1;
q = q - (long)q % alignment;
char * r = q + size;
if (r > g_sys_alloc_region_end) {
/* Ah, out of luck!
should increase SYS_ALLOC_REGION_SIZE */
return NULL;
}
char * s = __sync_val_compare_and_swap(&g_sys_alloc_region_ptr, p, r);
if (p == s) {
return q;
}
}
}
#endif
void *calloc(size_t nmemb,size_t size)
{
#ifdef MYTH_WRAP_MALLOC_RUNTIME
/* fall back to the bump allocator before wrapping completed */
if (!g_wrap_malloc_completed) {
void *ptr = sys_alloc_align(16, nmemb * size);
memset(ptr, 0, nmemb * size);
return ptr;
}
/* no wrap. call the real one */
if (!g_wrap_malloc) {
return real_calloc(nmemb, size);
}
#endif
void *ptr;
ptr=malloc(nmemb*size);
if (!ptr)return NULL;
memset(ptr,0,nmemb*size);
return ptr;
}
void *malloc(size_t size)
{
#ifdef MYTH_WRAP_MALLOC_RUNTIME
/* fall back to the bump allocator before wrapping completed */
if (!g_wrap_malloc_completed) {
void *ptr = sys_alloc_align(16, size);
return ptr;
}
/* no wrap. call the real one */
if (!g_wrap_malloc) {
return real_malloc(size);
}
#endif
//fprintf(stderr,"malloc %d\n",size);
malloc_wrapper_header_t ptr;
size_t realsize;
int idx;
if (size<16)size=16;
if (!real_malloc){
static int load_malloc_protect=0;
if (load_malloc_protect==0){
load_malloc_protect=1;
real_malloc=dlsym(RTLD_NEXT,"malloc");
}
else return NULL;
assert(real_malloc);
}
if ((!g_worker_thread_num) || (g_alloc_hook_ok!=g_worker_thread_num) || (size>MYTH_MALLOC_FLSIZE_MAX)){
ptr=real_malloc(size+sizeof(malloc_wrapper_header));
if (!ptr){
fprintf(stderr,"size=%llu\n",(unsigned long long)size);
}
assert(ptr);
ptr->s.fl_index=FREE_LIST_NUM;
ptr->s.org_ptr=ptr;
//fprintf(stderr,"malloc A,%p,%d\n",ptr,FREE_LIST_NUM);
return (void*)(ptr+1);
}
idx=MYTH_MALLOC_SIZE_TO_INDEX(size);
realsize=MYTH_MALLOC_INDEX_TO_RSIZE(idx);
void **fl_ptr;
myth_running_env_t env;
env=myth_get_current_env();
int rank=env->rank;
myth_freelist_pop(g_myth_malloc_wrapper_fl[rank][idx],fl_ptr);
if (!fl_ptr){
//Freelist is empty, allocate
ptr=real_malloc(realsize+sizeof(malloc_wrapper_header));
//fprintf(stderr,"malloc B,%p,%d\n",ptr,idx);
assert(ptr);
}
else{
ptr=(malloc_wrapper_header_t)fl_ptr;
}
ptr->s.fl_index=idx;
ptr->s.org_ptr=ptr;
return (void*)(ptr+1);
}
int posix_memalign(void **memptr,size_t alignment,size_t size)
{
#ifdef MYTH_WRAP_MALLOC_RUNTIME
/* fall back to the bump allocator before wrapping completed */
if (!g_wrap_malloc_completed) {
void *ptr = sys_alloc_align(alignment, size);
if (ptr) {
*memptr = ptr;
return 0;
} else {
return ENOMEM;
}
}
/* no wrap. call the real one */
if (!g_wrap_malloc) {
return real_posix_memalign(memptr, alignment, size);
}
#endif
if (size==0){*memptr=NULL;return 0;}
malloc_wrapper_header_t ptr;
if (size<16)size=16;
if (!real_malloc){
static int load_malloc_protect=0;
if (load_malloc_protect==0){
load_malloc_protect=1;
real_malloc=dlsym(RTLD_NEXT,"malloc");
}
else {*memptr=NULL;return 0;}
assert(real_malloc);
}
uintptr_t n0,n;
n0=(uintptr_t)real_malloc(size+alignment+sizeof(malloc_wrapper_header));
if (!n0){
fprintf(stderr,"size=%llu\n",(unsigned long long)size);
return ENOMEM;
}
//align
n=n0+alignment-1;
n/=alignment;n*=alignment;
ptr=(malloc_wrapper_header_t)n;ptr--;
ptr->s.fl_index=FREE_LIST_NUM;
ptr->s.org_ptr=(void*)n0;
//fprintf(stderr,"memalign A,%p,%p,%p,%d\n",(void*)n0,ptr,(void*)n,FREE_LIST_NUM);
*memptr=(void*)n;
return 0;
}
void *aligned_alloc(size_t alignment, size_t size)
{
void *ret;
errno=posix_memalign(&ret,alignment,size);
return ret;
}
void *valloc(size_t size)
{
#ifdef MYTH_WRAP_MALLOC_RUNTIME
/* fall back to the bump allocator before wrapping completed */
if (!g_wrap_malloc_completed) {
void *ptr = sys_alloc_align(PAGE_SIZE, size);
return ptr;
}
/* no wrap. call the real one */
if (!g_wrap_malloc) {
return real_valloc(size);
}
#endif
void *ret;
errno=posix_memalign(&ret,PAGE_SIZE,size);
return ret;
}
void free(void *ptr)
{
#ifdef MYTH_WRAP_MALLOC_RUNTIME
/* before wrapping completed, we simply forget about it.
(real_free not available yet, so we cannot call it.
the problem may be deeper. the ptr may have been allocated
by yet another function (not the original system malloc),
so even passing it to real_free may not be the right action */
if (!g_wrap_malloc_completed) {
/* leak */
return;
}
if (!g_wrap_malloc) {
/* we call real_free, except for region we have allocated
before wrapping is complete */
if (!sys_alloc_region(ptr)) {
return real_free(ptr);
}
}
#endif
if (!ptr)return;
if (!real_free){
real_free=dlsym(RTLD_NEXT,"free");
assert(real_free);
}
#ifdef MYTH_WRAP_MALLOC_DLSYM_ENABLED
//do nothing if in dlsym region
intptr_t s,e;
s=(intptr_t)s_malloc_dlsym_region;
e=s+MYTH_WRAP_MALLOC_DLSYM_SIZE;
if (s<=((intptr_t)ptr) && ((intptr_t)ptr)<e)return;
#endif
malloc_wrapper_header_t rptr=(malloc_wrapper_header_t)ptr;
rptr--;
uint64_t idx=rptr->s.fl_index;
if (idx>=FREE_LIST_NUM){
//fprintf(stderr,"free A,%p,%d\n",rptr->s.org_ptr,(int)idx);
real_free(rptr->s.org_ptr);
return;
}
if (g_worker_thread_num && (g_alloc_hook_ok==g_worker_thread_num)){
myth_running_env_t env;
env=myth_get_current_env();
int rank=env->rank;
myth_freelist_push(g_myth_malloc_wrapper_fl[rank][idx],(void**)rptr);
return ;
}
//fprintf(stderr,"free B,%p,%d\n",rptr->s.org_ptr,(int)idx);
real_free(rptr->s.org_ptr);
}
void *realloc(void *ptr,size_t size)
{
#ifdef MYTH_WRAP_MALLOC_RUNTIME
/* fall back to the bump allocator before wrapping completed */
if (!g_wrap_malloc_completed) {
/* leak old ptr */
void *new_ptr = sys_alloc_align(16, size);
memcpy(new_ptr, ptr, size);
return new_ptr;
}
/* no wrap. call the real one */
if (!g_wrap_malloc) {
return real_realloc(ptr, size);
}
#endif
if (size==0){free(ptr);return NULL;}
if (!ptr)return malloc(size);
uint64_t *rptr=(uint64_t*)ptr;rptr-=16/8;
MAY_BE_UNUSED size_t nrsize;
size_t orsize;
int oidx,nidx;
oidx=*rptr;
if (size<16)size=16;
nidx=MYTH_MALLOC_SIZE_TO_INDEX(size);
if (oidx==nidx)return ptr;
nrsize=MYTH_MALLOC_INDEX_TO_RSIZE(nidx);
orsize=MYTH_MALLOC_INDEX_TO_RSIZE(*rptr);
void *nptr=malloc(size);
if (!nptr)return NULL;
size_t btc=(size<orsize)?size:orsize;
memcpy(nptr,ptr,btc);
free(ptr);
return nptr;
}
#else
void myth_malloc_wrapper_init(int nthreads)
{
}
void myth_malloc_wrapper_init_worker(int rank)
{
}
void myth_malloc_wrapper_fini()
{
}
void myth_malloc_wrapper_fini_worker(int rank)
{
}
#endif