-
Notifications
You must be signed in to change notification settings - Fork 1
/
malloc_challenge_basic.c
698 lines (631 loc) · 23.5 KB
/
malloc_challenge_basic.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
////////////////////////////////////////////////////////////////////////////////
/* (๑>◡<๑) Malloc Challenge!! (◍>◡<◍) */
////////////////////////////////////////////////////////////////////////////////
//
// Welcome to Malloc Challenge!! Your job is to invent a smart malloc algorithm.
//
// Rules:
//
// 1. Your job is to implement my_malloc(), my_free() and my_initialize().
// * my_initialize() is called only once at the beginning of each challenge.
// You can initialize the memory allocator.
// * my_malloc(size) is called every time an object is allocated. In this
// challenge, |size| is guaranteed to be a multiple of 8 bytes and meets
// 8 <= size <= 4000.
// * my_free(ptr) is called every time an object is freed.
// 2. The only library functions you can use in my_malloc() and my_free() are
// mmap_from_system() and munmap_to_system().
// * mmap_from_system(size) allocates |size| bytes from the system. |size|
// needs to be a multiple of 4096 bytes. mmap_from_system(size) is a
// system call and heavy. You are expected to minimize the call of
// mmap_from_system(size) by reusing the returned
// memory region as much as possible.
// * munmap_to_system(ptr, size) frees the memory region [ptr, ptr + size)
// to the system. |ptr| and |size| need to be a multiple of 4096 bytes.
// You are expected to free memory regions that are unused.
// * You are NOT allowed to use any other library functions at all, including
// the default malloc() / free(), std:: libraries etc. This is because you
// are implementing malloc itself -- if you use something that may use
// malloc internally, it will result in an infinite recurion.
// 3. simple_malloc(), simple_free() and simple_initialize() are an example,
// straightforward implementation. Your job is to invent a smarter malloc
// algorithm than the simple malloc.
// 4. There are five challenges (Challenge 1, 2, 3, 4 and 5). Each challenge
// allocates and frees many objects with different patterns. Your malloc
// is evaluated by two criteria.
// * [Speed] How faster your malloc finishes the challange compared to
// the simple malloc.
// * [Memory utilization] How much your malloc is memory efficient.
// This is defined as (S1 / S2), where S1 is the total size of objects
// allocated at the end of the challange and S2 is the total size of
// mmap_from_system()ed regions at the end of the challenge. You can
// improve the memory utilization by decreasing memory fragmentation and
// reclaiming unused memory regions to the system with munmap_to_system().
// 5. This program works on Linux and Mac but not on Windows. If you don't have
// Linux or Mac, you can use Google Cloud Shell (See https://docs.google.com/document/d/1TNu8OfoQmiQKy9i2jPeGk1DOOzSVfbt4RoP_wcXgQSs/edit#).
// 6. You need to specify an '-lm' option to compile this program.
// * gcc malloc_challenge.c -lm
// * clang malloc_challenge.c -lm
//
// Enjoy! :D
//
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/time.h>
void* mmap_from_system(size_t size);
void munmap_to_system(void* ptr, size_t size);
////////////////////////////////////////////////////////////////////////////////
//
// [Simple malloc]
//
// This is an example, straightforward implementation of malloc. Your goal is
// to invent a smarter malloc algorithm in terms of both [Execution time] and
// [Memory utilization].
// Each object or free slot has metadata just prior to it:
//
// ... | m | object | m | free slot | m | free slot | m | object | ...
//
// where |m| indicates metadata. The metadata is needed for two purposes:
//
// 1) For an allocated object:
// * |size| indicates the size of the object. |size| does not include
// the size of the metadata.
// * |next| is unused and set to NULL.
// 2) For a free slot:
// * |size| indicates the size of the free slot. |size| does not include
// the size of the metadata.
// * The free slots are linked with a singly linked list (we call this a
// free list). |next| points to the next free slot.
typedef struct simple_metadata_t {
size_t size;
struct simple_metadata_t* next;
} simple_metadata_t;
// The global information of the simple malloc.
// * |free_head| points to the first free slot.
// * |dummy| is a dummy free slot (only used to make the free list
// implementation simpler).
typedef struct simple_heap_t {
simple_metadata_t* free_head;
simple_metadata_t dummy;
} simple_heap_t;
simple_heap_t simple_heap;
// Add a free slot to the beginning of the free list.
void simple_add_to_free_list(simple_metadata_t* metadata) {
assert(!metadata->next);
metadata->next = simple_heap.free_head;
simple_heap.free_head = metadata;
}
// Remove a free slot from the free list.
void simple_remove_from_free_list(simple_metadata_t* metadata,
simple_metadata_t* prev) {
if (prev) {
prev->next = metadata->next;
} else {
simple_heap.free_head = metadata->next;
}
metadata->next = NULL;
}
// This is called only once at the beginning of each challenge.
void simple_initialize() {
simple_heap.free_head = &simple_heap.dummy;
simple_heap.dummy.size = 0;
simple_heap.dummy.next = NULL;
}
// This is called every time an object is allocated. |size| is guaranteed
// to be a multiple of 8 bytes and meets 8 <= |size| <= 4000. You are not
// allowed to use any library functions other than mmap_from_system /
// munmap_to_system.
void* simple_malloc(size_t size) {
simple_metadata_t* metadata = simple_heap.free_head;
simple_metadata_t* prev = NULL;
// First-fit: Find the first free slot the object fits.
while (metadata && metadata->size < size) {
prev = metadata;
metadata = metadata->next;
}
if (!metadata) {
// There was no free slot available. We need to request a new memory region
// from the system by calling mmap_from_system().
//
// | metadata | free slot |
// ^
// metadata
// <---------------------->
// buffer_size
size_t buffer_size = 4096;
simple_metadata_t* metadata = (simple_metadata_t*)mmap_from_system(buffer_size);
metadata->size = buffer_size - sizeof(simple_metadata_t);
metadata->next = NULL;
// Add the memory region to the free list.
simple_add_to_free_list(metadata);
// Now, try simple_malloc() again. This should succeed.
return simple_malloc(size);
}
// |ptr| is the beginning of the allocated object.
//
// ... | metadata | object | ...
// ^ ^
// metadata ptr
void* ptr = metadata + 1;
size_t remaining_size = metadata->size - size;
metadata->size = size;
// Remove the free slot from the free list.
simple_remove_from_free_list(metadata, prev);
if (remaining_size > sizeof(simple_metadata_t)) {
// Create a new metadata for the remaining free slot.
//
// ... | metadata | object | metadata | free slot | ...
// ^ ^ ^
// metadata ptr new_metadata
// <------><---------------------->
// size remaining size
simple_metadata_t* new_metadata = (simple_metadata_t*)((char*)ptr + size);
new_metadata->size = remaining_size - sizeof(simple_metadata_t);
new_metadata->next = NULL;
// Add the remaining free slot to the free list.
simple_add_to_free_list(new_metadata);
}
return ptr;
}
// This is called every time an object is freed. You are not allowed to use
// any library functions other than mmap_from_system / munmap_to_system.
void simple_free(void* ptr) {
// Look up the metadata. The metadata is placed just prior to the object.
//
// ... | metadata | object | ...
// ^ ^
// metadata ptr
simple_metadata_t* metadata = (simple_metadata_t*)ptr - 1;
// Add the free slot to the free list.
simple_add_to_free_list(metadata);
}
////////////////////////////////////////////////////////////////////////////////
//
// [My malloc]
//
// Your job is to invent a smarter malloc algorithm here :)
// Each object or free slot has metadata just prior to it:
//
// ... | m | object | m | free slot | m | free slot | m | object | ...
//
// where |m| indicates metadata. The metadata is needed for two purposes:
//
// 1) For an allocated object:
// * |size| indicates the size of the object. |size| does not include
// the size of the metadata.
// * |next| is unused and set to NULL.
// 2) For a free slot:
// * |size| indicates the size of the free slot. |size| does not include
// the size of the metadata.
// * The free slots are linked with a singly linked list (we call this a
// free list). |next| points to the next free slot.
typedef struct my_metadata_t {
size_t size;
struct my_metadata_t* next;
} my_metadata_t;
// The global information of the my malloc.
// * |free_head| points to the first free slot.
// * |dummy| is a dummy free slot (only used to make the free list
// implementation simpler).
typedef struct my_heap_t {
my_metadata_t* free_head;
my_metadata_t dummy;
} my_heap_t;
// The size of the free list bin. The |i|th bin maintains a singly-linked list
// of the free slots whose size is [2^(i+3), 2^(i+4)) bytes. The allocation
// size is guaranteed to meet 8 <= |allocation size| <= 4000, so 0 <= |i| < 11.
#define FREE_LIST_BIN_MAX 11
my_heap_t my_heap[FREE_LIST_BIN_MAX];
// Return the bin index.
int get_bin(size_t size) {
int count = 0;
while (size) {
size /= 2;
count++;
}
int bin = count - 4;
assert(0 <= bin);
assert(bin < FREE_LIST_BIN_MAX);
return bin;
}
// Add a free slot to the beginning of the free list.
void my_add_to_free_list(my_metadata_t* metadata, int bin) {
assert(!metadata->next);
metadata->next = my_heap[bin].free_head;
my_heap[bin].free_head = metadata;
}
// Remove a free slot from the free list.
void my_remove_from_free_list(my_metadata_t* metadata,
my_metadata_t* prev, int bin) {
if (prev) {
prev->next = metadata->next;
} else {
my_heap[bin].free_head = metadata->next;
}
metadata->next = NULL;
}
// This is called only once at the beginning of each challenge.
void my_initialize() {
for (int bin = 0; bin < FREE_LIST_BIN_MAX; bin++) {
my_heap[bin].free_head = &my_heap[bin].dummy;
my_heap[bin].dummy.size = 0;
my_heap[bin].dummy.next = NULL;
}
}
// This is called every time an object is allocated. |size| is guaranteed
// to be a multiple of 8 bytes and meets 8 <= |size| <= 4000. You are not
// allowed to use any library functions other than mmap_from_system /
// munmap_to_system.
void* my_malloc(size_t size) {
int bin = get_bin(size);
// Best-fit: Find the best-fit free slot the object fits.
size_t best_diff = 4096;
my_metadata_t* best_metadata = NULL;
my_metadata_t* best_metadata_prev = NULL;
my_metadata_t* current_metadata = my_heap[bin].free_head;
my_metadata_t* current_metadata_prev = NULL;
while (current_metadata) {
if (current_metadata->size >= size) {
// Found a slot the object fits.
size_t diff = current_metadata->size - size;
if (diff <= best_diff) {
// Try to find the best-fit slot.
best_diff = diff;
best_metadata = current_metadata;
best_metadata_prev = current_metadata_prev;
}
}
current_metadata_prev = current_metadata;
current_metadata = current_metadata->next;
}
my_metadata_t* prev = best_metadata_prev;
my_metadata_t* metadata = best_metadata;
if (!metadata) {
// There was no free slot available. We need to request a new memory region
// from the system by calling mmap_from_system().
//
// | metadata | free slot |
// ^
// metadata
// <---------------------->
// buffer_size
size_t buffer_size = 4096;
my_metadata_t* metadata = (my_metadata_t*)mmap_from_system(buffer_size);
metadata->size = buffer_size - sizeof(my_metadata_t);
metadata->next = NULL;
// Add the memory region to the free list.
my_add_to_free_list(metadata, bin);
// Now, try my_malloc() again. This should succeed.
return my_malloc(size);
}
// |ptr| is the beginning of the allocated object.
//
// ... | metadata | object | ...
// ^ ^
// metadata ptr
void* ptr = metadata + 1;
size_t remaining_size = metadata->size - size;
metadata->size = size;
// Remove the free slot from the free list.
my_remove_from_free_list(metadata, prev, bin);
if (remaining_size > sizeof(my_metadata_t)) {
// Create a new metadata for the remaining free slot.
//
// ... | metadata | object | metadata | free slot | ...
// ^ ^ ^
// metadata ptr new_metadata
// <------><---------------------->
// size remaining size
my_metadata_t* new_metadata = (my_metadata_t*)((char*)ptr + size);
new_metadata->size = remaining_size - sizeof(my_metadata_t);
new_metadata->next = NULL;
// Add the remaining free slot to the free list.
my_add_to_free_list(new_metadata, bin);
}
return ptr;
}
// This is called every time an object is freed. You are not allowed to use
// any library functions other than mmap_from_system / munmap_to_system.
void my_free(void* ptr) {
// Look up the metadata. The metadata is placed just prior to the object.
//
// ... | metadata | object | ...
// ^ ^
// metadata ptr
my_metadata_t* metadata = (my_metadata_t*)ptr - 1;
// Add the free slot to the free list.
my_add_to_free_list(metadata, get_bin(metadata->size));
}
////////////////////////////////////////////////////////////////////////////////
//
// [Test]
//
// Add test cases in test(). test() is called at the beginning of the program.
void test() {
my_initialize();
for (int i = 0; i < 100; i++) {
void* ptr = my_malloc(96);
my_free(ptr);
}
void* ptrs[100];
for (int i = 0; i < 100; i++) {
ptrs[i] = my_malloc(96);
}
for (int i = 0; i < 100; i++) {
my_free(ptrs[i]);
}
}
////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO READ THE CODE BELOW //
////////////////////////////////////////////////////////////////////////////////
// This is code to run challenges. Please do NOT modify the code.
// Vector
typedef struct object_t {
void* ptr;
size_t size;
char tag; // A tag to check the object is not broken.
} object_t;
typedef struct vector_t {
size_t size;
size_t capacity;
object_t* buffer;
} vector_t;
vector_t* vector_create() {
vector_t* vector = (vector_t*)malloc(sizeof(vector_t));
vector->capacity = 0;
vector->size = 0;
vector->buffer = NULL;
return vector;
}
void vector_push(vector_t* vector, object_t object) {
if (vector->size >= vector->capacity) {
vector->capacity = vector->capacity * 2 + 128;
vector->buffer = (object_t*)realloc(
vector->buffer, vector->capacity * sizeof(object_t));
}
vector->buffer[vector->size] = object;
vector->size++;
}
size_t vector_size(vector_t* vector) {
return vector->size;
}
object_t vector_at(vector_t* vector, size_t i) {
assert(i < vector->size);
return vector->buffer[i];
}
void vector_clear(vector_t* vector) {
free(vector->buffer);
vector->capacity = 0;
vector->size = 0;
vector->buffer = NULL;
}
void vector_destroy(vector_t* vector) {
free(vector->buffer);
free(vector);
}
// Return the current time in seconds.
double get_time(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec * 1e-6;
}
// Return a random number in [0, 1).
double urand() {
return rand() / ((double)RAND_MAX + 1);
}
// Return an object size. The returned size is a random number in
// [min_size, max_size] that follows an exponential distribution.
// |min_size| needs to be a multiple of 8 bytes.
size_t get_object_size(size_t min_size, size_t max_size) {
const int alignment = 8;
assert(min_size <= max_size);
assert(min_size % alignment == 0);
const double lambda = 1;
const double threshold = 6;
double tau = -lambda * log(urand());
if (tau >= threshold) {
tau = threshold;
}
size_t result =
(size_t)((max_size - min_size) * tau / threshold) + min_size;
result = result / alignment * alignment;
assert(min_size <= result);
assert(result <= max_size);
return result;
}
// Return an object lifetime. The returned lifetime is a random number in
// [min_epoch, max_epoch] that follows an exponential distribution.
unsigned get_object_lifetime(unsigned min_epoch, unsigned max_epoch) {
const double lambda = 1;
const double threshold = 6;
double tau = -lambda * log(urand());
if (tau >= threshold) {
tau = threshold;
}
unsigned result =
(unsigned)((max_epoch - min_epoch) * tau / threshold + min_epoch);
assert(min_epoch <= result);
assert(result <= max_epoch);
return result;
}
typedef void (*initialize_func_t)();
typedef void* (*malloc_func_t)(size_t size);
typedef void (*free_func_t)(void* ptr);
// Record the statistics of each challenge.
typedef struct stats_t {
double begin_time;
double end_time;
size_t mmap_size;
size_t munmap_size;
size_t allocated_size;
size_t freed_size;
} stats_t;
stats_t stats;
// Run one challenge.
// |min_size|: The min size of an allocated object
// |max_size|: The max size of an allocated object
// |*_func|: Function pointers to initialize / malloc / free.
void run_challenge(size_t min_size,
size_t max_size,
initialize_func_t initialize_func,
malloc_func_t malloc_func,
free_func_t free_func) {
const int cycles = 10;
const int epochs_per_cycle = 100;
const int objects_per_epoch_small = 100;
const int objects_per_epoch_large = 2000;
char tag = 0;
// The last entry of the vector is used to store objects that are never freed.
vector_t* objects[epochs_per_cycle + 1];
for (int i = 0; i < epochs_per_cycle + 1; i++) {
objects[i] = vector_create();
}
initialize_func();
stats.mmap_size = stats.munmap_size = 0;
stats.allocated_size = stats.freed_size = 0;
stats.begin_time = get_time();
for (int cycle = 0; cycle < cycles; cycle++) {
for (int epoch = 0; epoch < epochs_per_cycle; epoch++) {
size_t allocated = 0;
size_t freed = 0;
// Allocate |objects_per_epoch| objects.
int objects_per_epoch = objects_per_epoch_small;
if (epoch == 0) {
// To simulate a peak memory usage, we allocate a larger number of objects
// from time to time.
objects_per_epoch = objects_per_epoch_large;
}
for (int i = 0; i < objects_per_epoch; i++) {
size_t size = get_object_size(min_size, max_size);
int lifetime = get_object_lifetime(1, epochs_per_cycle);
stats.allocated_size += size;
allocated += size;
void* ptr = malloc_func(size);
memset(ptr, tag, size);
object_t object = {ptr, size, tag};
tag++;
if (tag == 0) {
// Avoid 0 for tagging since it is not distinguishable from fresh
// mmaped memory.
tag++;
}
if (urand() < 0.04) {
// 4% of objects are set as never freed.
vector_push(objects[epochs_per_cycle], object);
} else {
vector_push(objects[(epoch + lifetime) % epochs_per_cycle], object);
}
}
// Free objects that are expected to be freed in this epoch.
vector_t* vector = objects[epoch];
for (size_t i = 0; i < vector_size(vector); i++) {
object_t object = vector_at(vector, i);
stats.freed_size += object.size;
freed += object.size;
// Check that the tag is not broken.
if (((char*)object.ptr)[0] != object.tag ||
((char*)object.ptr)[object.size - 1] != object.tag) {
printf("An allocated object is broken!");
assert(0);
}
free_func(object.ptr);
}
#if 0
// Debug print
printf("epoch = %d, allocated = %ld bytes, freed = %ld bytes\n",
cycle * epochs_per_cycle + epoch, allocated, freed);
printf("allocated = %.2f MB, freed = %.2f MB, mmap = %.2f MB, munmap = %.2f MB, utilization = %d%%\n",
stats.allocated_size / 1024.0 / 1024.0,
stats.freed_size / 1024.0 / 1024.0,
stats.mmap_size / 1024.0 / 1024.0,
stats.munmap_size / 1024.0 / 1024.0,
(int)(100.0 * (stats.allocated_size - stats.freed_size)
/ (stats.mmap_size - stats.munmap_size)));
#endif
vector_clear(vector);
}
}
stats.end_time = get_time();
for (int i = 0; i < epochs_per_cycle + 1; i++) {
vector_destroy(objects[i]);
}
}
// Print stats
void print_stats(char* challenge, stats_t simple_stats, stats_t my_stats) {
printf("%s: simple malloc => my malloc\n", challenge);
printf("Time: %.f ms => %.f ms\n",
(simple_stats.end_time - simple_stats.begin_time) * 1000,
(my_stats.end_time - my_stats.begin_time) * 1000);
printf("Utilization: %d%% => %d%%\n",
(int)(100.0 * (simple_stats.allocated_size - simple_stats.freed_size)
/ (simple_stats.mmap_size - simple_stats.munmap_size)),
(int)(100.0 * (my_stats.allocated_size - my_stats.freed_size)
/ (my_stats.mmap_size - my_stats.munmap_size)));
printf("==================================\n");
}
// Run challenges
void run_challenges() {
stats_t simple_stats, my_stats;
// Warm up run.
run_challenge(128, 128, simple_initialize, simple_malloc, simple_free);
// Challenge 1:
run_challenge(128, 128, simple_initialize, simple_malloc, simple_free);
simple_stats = stats;
run_challenge(128, 128, my_initialize, my_malloc, my_free);
my_stats = stats;
print_stats("Challenge 1", simple_stats, my_stats);
// Challenge 2:
run_challenge(16, 16, simple_initialize, simple_malloc, simple_free);
simple_stats = stats;
run_challenge(16, 16, my_initialize, my_malloc, my_free);
my_stats = stats;
print_stats("Challenge 2", simple_stats, my_stats);
// Challenge 3:
run_challenge(16, 128, simple_initialize, simple_malloc, simple_free);
simple_stats = stats;
run_challenge(16, 128, my_initialize, my_malloc, my_free);
my_stats = stats;
print_stats("Challenge 3", simple_stats, my_stats);
// Challenge 4:
run_challenge(256, 4000, simple_initialize, simple_malloc, simple_free);
simple_stats = stats;
run_challenge(256, 4000, my_initialize, my_malloc, my_free);
my_stats = stats;
print_stats("Challenge 4", simple_stats, my_stats);
// Challenge 5:
run_challenge(8, 4000, simple_initialize, simple_malloc, simple_free);
simple_stats = stats;
run_challenge(8, 4000, my_initialize, my_malloc, my_free);
my_stats = stats;
print_stats("Challenge 5", simple_stats, my_stats);
}
// Allocate a memory region from the system. |size| needs to be a multiple of
// 4096 bytes.
void* mmap_from_system(size_t size) {
assert(size % 4096 == 0);
stats.mmap_size += size;
void* ptr = mmap(NULL, size,
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
assert(ptr);
return ptr;
}
// Free a memory region [ptr, ptr + size) to the system. |ptr| and |size| needs to
// be a multiple of 4096 bytes.
void munmap_to_system(void* ptr, size_t size) {
assert(size % 4096 == 0);
assert((uintptr_t)(ptr) % 4096 == 0);
stats.munmap_size += size;
int ret = munmap(ptr, size);
assert(ret != -1);
}
int main(int argc, char** argv) {
srand(12); // Set the rand seed to make the challenges non-deterministic.
test();
run_challenges();
return 0;
}