-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmalloc.c
456 lines (378 loc) · 10.8 KB
/
malloc.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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
* Implements malloc library in C.
* This malloc implemtation has per thread bins.
* bins are according to four sizes:
* 8 bytes, 64 bytes, 512 bytes and greated than 512 bytes.
*
* memory for size > 512 bytes is allocated through mmap system call.
* memory blocks are initialized lazily and are appended on free list after
* free() call.
*
* Author: Savan Patel
* Email : [email protected]
*
*/
#include "malloc.h"
/*
Aligns pointer to 8 byte address.
params : pointer to align.
returns: a pointer (aligned to 8 bytes)
*/
void *align8(void *x)
{
unsigned long p = (unsigned long)x;
p = (((p - 1) >> 3) << 3) + 8;
return (void *)p;
}
/*
* returns the pointer to elated bin based on the size.
* params: size of bin.
* returns: pointer to bin corresponding size.
*/
block_info** get_bin(size_t size)
{
switch(size)
{
case 8 : return &bin_8;
case 64 : return &bin_64;
case 512 : return &bin_512;
default : return &bin_large;
}
}
/*
* Creates a memory block from unused heap.
* params: requested memory size in bytes.
* returns: pointer to allocated memory chunk. NULL on failure.
*/
void * block_from_unused_heap(size_t size)
{
/*If thread heap is not initialized or if available free size is less
than the block for requested size.*/
if(NULL == thread_unused_heap_start ||
(thread_heap_end - thread_unused_heap_start) <
(size + sizeof(block_info)))
{
/*If heap of process is not initialized or available free size of general
heap is less than the block for requested size.*/
if(NULL == heap_used_memory_end ||
(sbrk(0) - heap_used_memory_end) < (size + sizeof(block_info)))
{
/*If heap is not initialized.*/
if(NULL == heap_used_memory_end)
{
heap_used_memory_end = sbrk(0);
if(heap_used_memory_end == (void*) -1)
{
errno = ENOMEM;
perror("\n sbrk(0) failed.");
return NULL;
}
}
align8(heap_used_memory_end);
}
// extend heap, return NULL on failure.
if(sbrk(sysconf(_SC_PAGESIZE) * 100) == (void *) -1)
{
errno = ENOMEM;
perror("\n sbrk failed to extend heap.");
return NULL;
}
/*If there is smaller chunk remaining, add to free list of a bin.
to minimize the wastage of memory.*/
/*if(NULL != thread_unused_heap_start)
{
// TODO: add_chunk_to_bin(); possible optimization.
}*/
/*create fresh heap of 1 page size. for a thread.*/
thread_unused_heap_start = heap_used_memory_end;
thread_heap_end =
heap_used_memory_end + (sysconf(_SC_PAGESIZE));
heap_used_memory_end = thread_heap_end;
}
block_info b;
b.size = size;
b.next = NULL;
memcpy(thread_unused_heap_start, &b, sizeof(block_info));
thread_unused_heap_start += (sizeof(block_info) + size);
// update stats variables.
pthread_mutex_lock(&stats_mutex);
total_number_of_blocks++;
total_arena_size_allocated += size;
pthread_mutex_unlock(&stats_mutex);
return (thread_unused_heap_start - size);
}
/*
* Allocate memory from heap area. For memory request of sizes < 512, chunks are
* allocated from heap.
* params : size.
* returns: pointer to allocated area.
*/
void *heap_allocate(size_t size)
{
block_info **bin = get_bin(size);
void * ret = NULL;
/* reuse memory block from heap bins if available*/
if(NULL != *bin)
{
block_info *p = *bin;
*bin = p->next;
p->next = NULL;
pthread_mutex_lock(&stats_mutex);
total_free_blocks--;
pthread_mutex_unlock(&stats_mutex);
ret = (void *)((char*)p + sizeof(block_info));
}
else //request new memory or slice out remaining unused memory.
{ pthread_mutex_lock(&global_heap_mutex);
ret = block_from_unused_heap(size);
pthread_mutex_unlock(&global_heap_mutex);
}
return ret;
}
/*
Finds best fitting block from large memory bin.
*/
void * find_best_fit_from_bin_large(size_t size)
{
block_info *b = bin_large;
block_info *best_fit = NULL;
int min_fit_size = INT_MAX;
void *ret = NULL;
while(b != NULL)
{
if(b->size >= size && b->size < min_fit_size)
{
best_fit = b;
min_fit_size = b->size;
}
b = b->next;
}
/*If best fit found, update list*/
if(NULL != best_fit)
{
// if best_fit is first block.
if (best_fit == bin_large)
{
bin_large = bin_large->next;
best_fit->next = NULL;
ret = (void *)((void *)best_fit + sizeof(block_info));
}
else
{
b = bin_large;
while(b != NULL && b->next != best_fit)
{
b = b->next;
}
if(b != NULL)
{
b->next = best_fit->next;
}
best_fit->next = NULL;
ret = (void *)((void *)best_fit + sizeof(block_info));
}
}
return ret;
}
/*
* maps new memory address using mmap system call for size
* request > 512 bytes.
* Requests kernel to map new memory at some place decided by kernel.
* params: requested size in bytes.
* returns: pointer to block allocated., NULL on failure.
*/
void * mmap_new_memory(size_t size)
{
int num_pages =
((size + sizeof(block_info) - 1)/sysconf(_SC_PAGESIZE)) + 1;
int required_page_size = sysconf(_SC_PAGESIZE) * num_pages;
void *ret = mmap(NULL, // let kernel decide.
required_page_size,
PROT_READ | PROT_WRITE,
MAP_ANONYMOUS| MAP_PRIVATE,
-1, //no file descriptor
0); //offset.
block_info b;
b.size = (required_page_size - sizeof(block_info));
b.next = NULL;
ret = memcpy(ret, &b, sizeof(block_info));
ret = ((char*)ret + sizeof(block_info));
// update stats variables.
pthread_mutex_lock(&stats_mutex);
total_mmap_size_allocated += size;
pthread_mutex_unlock(&stats_mutex);
return ret;
}
/*
* Performs allocation for request > 512 bytes.
* params : requested memory size.
* returns: pointer to allocated memory. NULL on failure.
*/
void *alloc_large(size_t size)
{
void * ret = NULL;
if(NULL != bin_large)
{
//pthread_mutex_lock(&global_heap_mutex);
ret = find_best_fit_from_bin_large(size);
// pthread_mutex_unlock(&global_heap_mutex);
}
/*either bin_large is empty or no best fit was found.*/
if(ret == NULL)
{
//pthread_mutex_lock(&global_heap_mutex);
ret = mmap_new_memory(size);
// pthread_mutex_unlock(&global_heap_mutex);
}
return ret;
}
/*
* Allocates the memory.
*/
void* malloc(size_t size)
{
pthread_mutex_lock(&stats_mutex);
total_allocation_request++;
pthread_mutex_unlock(&stats_mutex);
void * ret = NULL;
if(size < 0)
{
perror("\n Invalid memory request.");
return NULL;
}
// allocate from either large bin or mmap.
if(size > 512)
{ //printf("Alloc large\n");
ret = alloc_large(size);
}
else
{
size = (size <= 8)? 8 : ((size<=64)? 64: 512);
ret = heap_allocate(size);
}
return ret;
}
/*
* Free up the memory allocated at pointer p. It appends the block into free
* list.
* params: address to pointer to be freed.
* returns: NONE.
*/
void free(void *p)
{
//update stats variables.
pthread_mutex_lock(&stats_mutex);
total_free_request++;
total_free_blocks++;
pthread_mutex_unlock(&stats_mutex);
if(NULL != p)
{
block_info *block = (block_info *)(p - sizeof(block_info));
memset(p, '\0', block->size);
block_info **bin = get_bin(block->size);
block_info *check_bin = *bin;
// already freed?
while(check_bin != NULL)
{
if(check_bin == block)
{
return;
}
check_bin = check_bin->next;
}
// attach as head to free list of corresponding bin.
block->next = *bin;
*bin = block;
}
}
/*similar to calloc of glibc */
void *calloc(size_t nmemb, size_t size)
{
void *p = malloc(nmemb * size);
block_info *b = (block_info *)(p - sizeof(block_info));
memset(p, '\0', b->size);
return p;
}
/*
* Allocate size of size bytes for nmemb. Initialize with null bytes.
* params: total number of elements of size 'size' to be allocated.
* and size to allocate.
* returns: pointer to allocated memory on success. NULL on failure.
*/void *realloc(void *ptr, size_t size)
{
if(NULL == ptr)
{
return malloc(size);
}
void *newptr = malloc(size);
if(NULL == newptr)
{
return NULL;
}
block_info *old_block =
(block_info *)((char*)ptr - sizeof(block_info));
memcpy(newptr, ptr, old_block->size);
free(ptr);
return newptr;
}
/*
* Fork hook. This will be called before fork happens.
* The method holds lock so as to make sure none of the active threads
* are holding lock and thus making sure, none of the threads are
* in middle of malloc process.
* later child hooks can reset the mutex to initial value.
*/
void prep_fork(void)
{
// take lock before fork so as to make sure no other thread is
// holding lock.
pthread_mutex_lock(&global_heap_mutex);
}
/*
* Since mutex is held by prep_fork method, it can safely be reset
* in parent process.
*/
void parent_fork_handle(void)
{
pthread_mutex_init(&global_heap_mutex, NULL);
}
/*
* Since mutex is held by prep_fork method, it can safely be reset
* in child process.
*/
void child_fork_handle(void)
{
pthread_mutex_init(&global_heap_mutex, NULL);
}
/*
* Global constructor to Initialize the fork hooks
*/
__attribute__((constructor)) void sharedLibConstructor(void)
{
int ret =
pthread_atfork(&prep_fork,
&parent_fork_handle,
&child_fork_handle);
if (ret != 0)
{
perror("pthread_atfork() error [Call #1]. Malloc is now not fork safe.");
}
/*if(mcheck(NULL) != 0)
{ TODO: mcheck implemtation.
perror("\n mcheck failed");
}*/
}
void *memalign(size_t alignment, size_t s)
{
return heap_used_memory_end;
}
void malloc_stats()
{
printf("\n -- malloc stats--\n");
printf("\n total_arena_size_allocated : %lu", total_arena_size_allocated);
printf("\n total_mmap_size_allocated : %lu", total_mmap_size_allocated);
printf("\n total_number_of_blocks : %lu", total_number_of_blocks);
printf("\n total_allocation_request : %lu", total_allocation_request);
printf("\n total_free_request : %lu", total_free_request);
printf("\n total_free_blocks : %lu\n", total_free_blocks);
}