-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc_3.cpp
More file actions
819 lines (736 loc) · 23.4 KB
/
malloc_3.cpp
File metadata and controls
819 lines (736 loc) · 23.4 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
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
#include <unistd.h>
#include <cstring>
#include <sys/mman.h>
#include <math.h>
#define MAX_SIZE 100000000 //1e8B
#define MMAP_MIN_SIZE 131072 //128KB
#define MIN_SPLIT_SIZE 128 //128B
struct MallocMetaData
{
int cookie;
size_t size; //size of block without size of metadata
bool is_free;
void* block_start_address;
MallocMetaData* next;
MallocMetaData* prev;
};
MallocMetaData* MetaHead = nullptr; //global pointer to fist node of metadata (start of heap)
int cookie = rand();
size_t mmap_allocated_bytes;
size_t mmap_allocated_blocks;
/******************************* FUNCTIONS DECLARATIONS *********************************/
size_t _num_free_bytes();
size_t _num_allocated_blocks();
size_t _num_allocated_bytes();
size_t _num_free_blocks();
size_t _size_meta_data();
size_t _num_meta_data_bytes();
void* smalloc(size_t size);
void* scalloc(size_t num, size_t size);
void sfree(void* p);
void* srealloc(void* oldp, size_t size);
/****************************** OUR HELPER FUNCTIONS DECLARATIONS ***************************/
MallocMetaData* find_meta_from_block_address(void* block_address);
/* Helper function for smalloc:
* if there is a free block: this function returns the smallest one that can fit the new size (its metadata)
* if there is no free block that fits the new size, this function returns NULL
* */
MallocMetaData* find_suitable_meta(size_t size);
//helper function for adding a node. (containing metadata)
void add_in_between(MallocMetaData* prev, MallocMetaData* next, MallocMetaData* new_meta);
void add_to_sorted_meta_list(MallocMetaData* new_meta);
/*
* we remove this meta node only in order to add its memory space to another adjacent meta block!
--> so there will not be any "wasted meta".. --> can use arithmetic operations with meta sizes and block start addresses..
*/
void detach_meta_node(MallocMetaData* to_remove);
/*
* returns the block start address of the new block (could be newly allocated or reused)
assumes that allocation candidate is already free (doesn't check that)
* */
void* reuse_and_split_if_needed(MallocMetaData* allocation_candidate, size_t size);
/*
* this functions allocates a new block in the HEAP and returns the new metaData
* DOESN'T add to sorted list of metas!
*/
MallocMetaData* allocate_new_block(size_t size);
MallocMetaData* find_topmost_allocated_chunk();
/*
* if the wilderness chunk is free this function enlarges it and returns its meta node
* else: it returns null
* new_size : is the requested size for the enlarged wilderness chunk.
* IT IS NOT THE DIFFERENCE BETWEEN THE WANTED SIZE AND THE CURRENT WILDERNESS SIZE!
*/
MallocMetaData* enlarge_wilderness_chunk(size_t new_size);
/*
* returns upper meta (in terms of addresses and not sizes!)
* maybe change everything to while loops and not arithmetic operations? ..
* */
MallocMetaData* find_upper_meta(MallocMetaData* current_meta);
MallocMetaData* find_lower_meta(MallocMetaData* current_meta);
/*
* merges with upper block
* -- There's only one meta at the end of this function:
* Current meta "eats" the upper meta
* returns the MERGED meta
* works only if upper block is free! adds the new merged meta to sorted list
* */
MallocMetaData* merge_with_upper_block(MallocMetaData* current_meta);
/*
* merges with lower block
* -- There's only one meta at the end of this function:
* Lower meta "eats" the upper meta
* works only if lower block is free!
* */
MallocMetaData* merge_with_lower_block(MallocMetaData* current_meta);
/*
* lower meta eats current and upper meta
* works only if upper and lower blocks are free!
* */
MallocMetaData* merge_with_two_adjacent_blocks(MallocMetaData* current_meta);
void combine_adjacent_free_blocks(MallocMetaData* current_meta);
bool node_is_in_heap(MallocMetaData* meta);
void check_cookie(MallocMetaData* meta);
/*
* if lower meta is free, and the size of current meta and lower one are enough to use for allocation return true
* else return false
* */
bool check_size_combined_with_lower(MallocMetaData* current_meta, size_t size_to_allocate);
/*
* if upper meta is free, and the size of current meta and upper one are enough to use for allocation return true
* else return false
* */
bool check_size_combined_with_upper(MallocMetaData* current_meta, size_t size_to_allocate);
/*
* if upper and lower meta are free, and the size of current meta, upper one and lower one are enough to use for allocation return true
* else return false
* */
bool check_size_combined_with_upper_and_lower(MallocMetaData* current_meta, size_t size_to_allocate);
/*
* Returns true if current meta is the wilderness, false otherwise
* */
bool am_wilderness(MallocMetaData* current_meta);
bool lower_is_free(MallocMetaData* current_meta);
/****************************** HELPER FUNCTIONS IMPLEMENTATIONS ************************/
MallocMetaData* find_meta_from_block_address(void* block_address)
{
size_t base = (size_t)block_address - _size_meta_data();
MallocMetaData* meta = (MallocMetaData*)((void*)(base));
return meta;
}
MallocMetaData* find_suitable_meta(size_t size)
{
if(!MetaHead) return nullptr;
MallocMetaData* current = MetaHead;
check_cookie(current);
if(current->is_free && current->size >= size) return current;
while(current)
{
check_cookie(current);
if(!current->next) return NULL;
if(current->next->is_free && current->next->size >= size)
return current->next;
current = current->next;
}
return current;
}
void add_in_between(MallocMetaData* prev, MallocMetaData* next, MallocMetaData* new_meta)
{
check_cookie(new_meta);
if(prev != nullptr)
{
check_cookie(prev);
prev->next = new_meta;
}
new_meta->prev = prev;
new_meta->next = next;
if(next != nullptr)
{
check_cookie(next);
next->prev = new_meta;
}
}
void add_to_sorted_meta_list(MallocMetaData* new_meta)
{
//if(!MetaHead) return; //sure?
if(!new_meta) return;
check_cookie(new_meta);
check_cookie(MetaHead);
if(!MetaHead)
{
MetaHead = new_meta;
}
MallocMetaData* current = MetaHead;
if(new_meta->size < current->size) //I want to be the first node
{
MetaHead = new_meta;
new_meta->next = current;
current->prev = new_meta;
new_meta->prev = nullptr;
}
else //I'm not the first node
{
check_cookie(current->next);
while(current->next && current->next->size < new_meta->size)
{
check_cookie(current);
check_cookie(current->next);
current = current->next;
}
check_cookie(current);
check_cookie(current->next);
if(current->next == nullptr) //reached the end of the list
{
add_in_between(current, current->next, new_meta);
}
else if(current->next && current->next->size > new_meta->size)
{
add_in_between(current, current->next, new_meta);
}
else if(current->next->size == new_meta->size)
{
while(current->next && current->next->size == new_meta->size && current->next->block_start_address < new_meta->block_start_address)
{
check_cookie(current);
check_cookie(current->next);
current = current->next;
}
add_in_between(current, current->next, new_meta);
}
}
}
void detach_meta_node(MallocMetaData* to_remove)
{
if(!to_remove) return;
check_cookie(to_remove);
check_cookie(MetaHead);
//if we want to remove the head:
if(to_remove == MetaHead)
{
MetaHead = MetaHead->next;
if(MetaHead)
{
MetaHead->prev = nullptr;
}
to_remove->prev = nullptr;
to_remove->next = nullptr;
return;
}
MallocMetaData* prev = to_remove->prev;
MallocMetaData* next = to_remove->next;
if(prev!=nullptr)
{
check_cookie(prev);
prev->next = to_remove->next;
}
if(next != nullptr)
{
check_cookie(next);
next->prev = prev;
}
to_remove->next = nullptr;
to_remove->prev = nullptr;
}
void* reuse_and_split_if_needed(MallocMetaData* allocation_candidate, size_t size)
{
void* address_to_return;
check_cookie(allocation_candidate);
if(allocation_candidate->size - size >= MIN_SPLIT_SIZE + _size_meta_data()) //if large enough ->> split!
{
size_t old_size = allocation_candidate->size;
allocation_candidate->size = size;
allocation_candidate->is_free = false;
void* new_meta_address = (void*)((size_t)(allocation_candidate->block_start_address) + size);
MallocMetaData* new_meta = (MallocMetaData*) new_meta_address;
new_meta->is_free = true;
new_meta->size = old_size - size - _size_meta_data();
new_meta->block_start_address = (void*) ((size_t)new_meta_address + _size_meta_data());
new_meta->cookie = cookie;
add_to_sorted_meta_list(new_meta);
//combine_adjacent_free_blocks(new_meta); //don't merge after splitting!
address_to_return = allocation_candidate->block_start_address;
}
else //reuse, no need to split
{
allocation_candidate->is_free = false;
address_to_return = allocation_candidate->block_start_address;
//don't change size!
}
return address_to_return;
}
MallocMetaData* allocate_new_block(size_t size)
{
void* sbrk_res = sbrk((intptr_t)(_size_meta_data() + size));
if(sbrk_res == (void*)(-1))
{
return NULL;
}
MallocMetaData* new_meta = (MallocMetaData*) sbrk_res;
new_meta->size = size;
new_meta->block_start_address = (void*) (size_t(sbrk_res) + _size_meta_data());
new_meta->is_free = false;
new_meta->cookie = cookie;
//add_to_sorted_meta_list(new_meta);
return new_meta;
}
MallocMetaData* find_topmost_allocated_chunk()
{
if(!MetaHead) return nullptr;
check_cookie(MetaHead);
MallocMetaData* current = MetaHead;
MallocMetaData* wilderness = current;
while(current)
{
check_cookie(current);
size_t curr_address = (size_t)current->block_start_address;
size_t max_address = (size_t)wilderness->block_start_address;
if(curr_address > max_address)
{
wilderness = current;
}
current = current->next;
}
return wilderness;
}
MallocMetaData* enlarge_wilderness_chunk(size_t new_size)
{
MallocMetaData* wilderness_meta = find_topmost_allocated_chunk();
if(!wilderness_meta) return nullptr;
check_cookie(wilderness_meta);
if(!wilderness_meta->is_free) return nullptr;
size_t size_to_add = new_size - wilderness_meta->size;
void* sbrk_res = sbrk((intptr_t)size_to_add);
if(sbrk_res == (void*)(-1))
{
return nullptr;
}
wilderness_meta->size = new_size;
detach_meta_node(wilderness_meta);
add_to_sorted_meta_list(wilderness_meta);
return wilderness_meta;
}
MallocMetaData* find_upper_meta(MallocMetaData* current_meta)
{
check_cookie(current_meta);
MallocMetaData* upper_meta;
void* upper_address = (void*)((size_t)(current_meta->block_start_address) + current_meta->size);
void* program_break = sbrk(0);
if(upper_address == program_break) //if this is the last meta
{
upper_meta = nullptr;
}
else
{
upper_meta = (MallocMetaData*)upper_address;
}
return upper_meta;
}
MallocMetaData* find_lower_meta(MallocMetaData* current_meta)
{
if(!MetaHead) return nullptr;
MallocMetaData* current = MetaHead;
check_cookie(MetaHead);
check_cookie(current_meta);
while(current)
{
if(find_upper_meta(current) == current_meta) // == operand for this struct?? if not, compare by block addresses! CHECK DONE ! :) WORKS WELL.
{
return current;
}
current = current->next;
check_cookie(current);
}
return nullptr;
}
MallocMetaData* merge_with_upper_block(MallocMetaData* current_meta)
{
check_cookie(current_meta);
MallocMetaData* upper_meta = find_upper_meta(current_meta);
check_cookie(upper_meta);
if(!upper_meta || !upper_meta->is_free) return nullptr;
current_meta->size += upper_meta->size + _size_meta_data();
detach_meta_node(upper_meta);
detach_meta_node(current_meta);
add_to_sorted_meta_list(current_meta);
return current_meta;
}
MallocMetaData* merge_with_lower_block(MallocMetaData* current_meta)
{
check_cookie(current_meta);
MallocMetaData* lower_meta = find_lower_meta(current_meta);
check_cookie(lower_meta);
if(!lower_meta || !lower_meta->is_free) return nullptr;
lower_meta->size += current_meta->size + _size_meta_data();
detach_meta_node(current_meta);
detach_meta_node(lower_meta);
add_to_sorted_meta_list(lower_meta);
return lower_meta;
}
MallocMetaData* merge_with_two_adjacent_blocks(MallocMetaData* current_meta)
{
check_cookie(current_meta);
MallocMetaData* lower_meta = find_lower_meta(current_meta);
MallocMetaData* upper_meta = find_upper_meta(current_meta);
if(!lower_meta || !upper_meta || !lower_meta->is_free || !upper_meta->is_free) return nullptr;
check_cookie(lower_meta);
check_cookie(upper_meta);
lower_meta->size += current_meta->size + upper_meta->size + 2*_size_meta_data();
detach_meta_node(current_meta);
detach_meta_node(upper_meta);
detach_meta_node(lower_meta);
add_to_sorted_meta_list(lower_meta);
return lower_meta;
}
void combine_adjacent_free_blocks(MallocMetaData* current_meta)
{
check_cookie(current_meta);
MallocMetaData* res = merge_with_two_adjacent_blocks(current_meta);
if(res) return;
res = merge_with_upper_block(current_meta);
if(res) return;
merge_with_lower_block(current_meta);
}
bool node_is_in_heap(MallocMetaData* meta)
{
MallocMetaData* curr = MetaHead;
check_cookie(MetaHead);
while(curr)
{
check_cookie(curr);
if(curr == meta)
{
return true;
}
curr = curr->next;
}
return false;
}
void check_cookie(MallocMetaData* meta)
{
if(!meta) return;
if(meta->cookie != cookie)
{
exit(0xdeadbeef);
}
}
bool check_size_combined_with_lower(MallocMetaData* current_meta, size_t size_to_allocate)
{
check_cookie(current_meta);
MallocMetaData* lower_meta = find_lower_meta(current_meta);
check_cookie(lower_meta);
if(lower_meta && lower_meta->is_free && (current_meta->size + lower_meta->size + _size_meta_data() >= size_to_allocate))
{
return true;
}
return false;
}
bool check_size_combined_with_upper(MallocMetaData* current_meta, size_t size_to_allocate)
{
check_cookie(current_meta);
MallocMetaData* upper_meta = find_upper_meta(current_meta);
check_cookie(upper_meta);
if(upper_meta && upper_meta->is_free && (current_meta->size + upper_meta->size + _size_meta_data() >= size_to_allocate))
{
return true;
}
return false;
}
bool check_size_combined_with_upper_and_lower(MallocMetaData* current_meta, size_t size_to_allocate)
{
check_cookie(current_meta);
MallocMetaData* lower_meta = find_lower_meta(current_meta);
MallocMetaData* upper_meta = find_upper_meta(current_meta);
check_cookie(lower_meta);
check_cookie(upper_meta);
size_t combined_size = current_meta->size + upper_meta->size + lower_meta->size + 2 * _size_meta_data();
if(lower_meta && upper_meta && lower_meta->is_free && upper_meta->is_free &&
(combined_size >= size_to_allocate))
{
return true;
}
return false;
}
bool am_wilderness(MallocMetaData* current_meta)
{
if(!current_meta) return false;
check_cookie(current_meta);
MallocMetaData* wilderness = find_topmost_allocated_chunk();
check_cookie(wilderness);
if(current_meta == wilderness) return true; //if we reached this point, lower is free for sure
return false;
}
bool lower_is_free(MallocMetaData* current_meta)
{
MallocMetaData* lower = find_lower_meta(current_meta);
if(!lower || !lower->is_free) return false;
return true;
}
/*******************************************************************************************/
size_t _num_free_bytes()
{
size_t count = 0;
MallocMetaData* tmp = MetaHead;
while(tmp)
{
check_cookie(tmp);
if(tmp->is_free) count += tmp->size;
tmp = tmp->next;
}
return count;
}
size_t _num_allocated_blocks()
{
size_t count = 0;
MallocMetaData* tmp = MetaHead;
while(tmp)
{
check_cookie(tmp);
count++;
tmp = tmp->next;
}
count += mmap_allocated_blocks;
return count;
}
size_t _num_allocated_bytes()
{
size_t count = 0;
MallocMetaData* tmp = MetaHead;
while(tmp)
{
check_cookie(tmp);
count += tmp->size;
tmp = tmp->next;
}
count += mmap_allocated_bytes;
return count;
}
size_t _num_free_blocks()
{
size_t count = 0;
MallocMetaData* tmp = MetaHead;
while(tmp)
{
check_cookie(tmp);
if(tmp->is_free) count ++;
tmp = tmp->next;
}
return count;
}
size_t _size_meta_data()
{
return sizeof(MallocMetaData);
}
size_t _num_meta_data_bytes()
{
return _size_meta_data() * _num_allocated_blocks();
}
void* smalloc(size_t size)
{
if(size == 0 ||size > MAX_SIZE)
{
return nullptr;
}
if(size >= MMAP_MIN_SIZE) //big enough to add to map
{
void* mmap_res = mmap(NULL, size + _size_meta_data(), PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(mmap_res == (void*)-1)
{
return nullptr;
}
MallocMetaData* new_meta = (MallocMetaData*) mmap_res;
new_meta->is_free = false;
new_meta->size = size;
new_meta->block_start_address = (void*)((size_t)mmap_res + _size_meta_data());
new_meta->prev = nullptr;
new_meta->next = nullptr;
mmap_allocated_bytes += size;
mmap_allocated_blocks++;
new_meta->cookie = cookie;
return new_meta->block_start_address;
}
//work in heap
if(MetaHead == NULL) //first allocation
{
MetaHead = allocate_new_block(size);
MetaHead->next = NULL;
MetaHead->prev = NULL;
return MetaHead->block_start_address;
}
//not the first allocation
void* address_to_return;
MallocMetaData* allocation_candidate = find_suitable_meta(size);
if(allocation_candidate != NULL) //found a space to reuse!
{
address_to_return = reuse_and_split_if_needed(allocation_candidate, size);
}
else //need to allocate a new block!
{
MallocMetaData* free_wilderness_meta = enlarge_wilderness_chunk(size);
if(free_wilderness_meta != nullptr)
{
free_wilderness_meta->is_free = false;
address_to_return = free_wilderness_meta->block_start_address;
}
else
{
MallocMetaData* new_meta = allocate_new_block(size);
add_to_sorted_meta_list(new_meta);
address_to_return = new_meta->block_start_address;
}
}
return address_to_return;
}
void* scalloc(size_t num, size_t size)
{
size_t new_size = num*size;
void* smalloc_res = smalloc(new_size);
if(smalloc_res == NULL)
{
return NULL;
}
void* memset_res = std::memset(smalloc_res, 0, num*size);
if(memset_res == nullptr)
{
return nullptr;
}
return smalloc_res;
}
void sfree(void* p)
{
if(!p) return;
MallocMetaData* meta = find_meta_from_block_address(p);
if(!(meta && !meta->is_free)) return;
//check if memory mapped region or heap:
if(!node_is_in_heap(meta)) //mmapped
{
size_t meta_size = meta->size;
void* meta_address = (void*)((size_t)meta->block_start_address - _size_meta_data());
munmap(meta_address, meta_size + _size_meta_data());
mmap_allocated_bytes -= meta_size;
mmap_allocated_blocks--;
}
else
{
if(!MetaHead) return;
meta->is_free = true;
combine_adjacent_free_blocks(meta);
}
}
void* srealloc(void* oldp, size_t size)
{
if(size > MAX_SIZE) return nullptr;
if(!oldp)
{
return smalloc(size);
}
MallocMetaData* meta = find_meta_from_block_address(oldp);
check_cookie(meta);
bool in_heap = node_is_in_heap(meta);
meta->is_free = true;
if(!in_heap) //mmapped region
{
if(meta->size == size)
{
meta->is_free = false;
return oldp;
}
void* meta_address = (void*)((size_t)oldp - _size_meta_data());
void* mmap_res = mmap(NULL, size + _size_meta_data(), PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(mmap_res == (void*)-1)
{
return nullptr;
}
MallocMetaData* new_meta = (MallocMetaData*) mmap_res;
new_meta->is_free = false;
new_meta->size = size;
new_meta->cookie = cookie;
new_meta->next = nullptr;
new_meta->prev = nullptr;
new_meta->block_start_address = (void*)((size_t)mmap_res + _size_meta_data());
mmap_allocated_bytes += size;
size_t size_to_move = meta->size;
if(size < meta->size)
{
size_to_move = size;
}
std::memmove(new_meta->block_start_address, meta->block_start_address, size_to_move); //check size - done
mmap_allocated_bytes -= meta->size;
munmap(meta_address, meta->size + _size_meta_data());
return new_meta->block_start_address;
}
else //in heap
{
bool merged_or_enlarged = false;
if(size <= meta->size) //reuse same block
{
reuse_and_split_if_needed(meta, size);
meta->is_free = false;
return oldp;
}
MallocMetaData* meta_to_allocate; //here will be the new meta to use/ allocate in
if(check_size_combined_with_lower(meta, size)) //check if lower block is enough
{
meta_to_allocate = merge_with_lower_block(meta);
/* we did merge meta with lower one but technically, the bytes in meta didn't change and its struct
is still available (same address) only detached. So we can move from meta to lower (combined one)
* */
merged_or_enlarged = true;
}
else if(am_wilderness(meta) && lower_is_free(meta))
{
meta_to_allocate = merge_with_lower_block(meta);
meta_to_allocate = enlarge_wilderness_chunk(size);
merged_or_enlarged = true;
}
else if(am_wilderness(meta))
{
meta_to_allocate = enlarge_wilderness_chunk(size);
merged_or_enlarged = true;
}
else if(check_size_combined_with_upper(meta, size))
{
meta_to_allocate = merge_with_upper_block(meta); //should return (current) meta
merged_or_enlarged = true;
}
else if(check_size_combined_with_upper_and_lower(meta, size))
{
meta_to_allocate = merge_with_two_adjacent_blocks(meta);
merged_or_enlarged = true;
}
else if(am_wilderness(find_upper_meta(meta)) && find_upper_meta(meta)->is_free)
{
if(find_lower_meta(meta) && find_lower_meta(meta)->is_free)
{
meta_to_allocate = merge_with_two_adjacent_blocks(meta);
meta_to_allocate = enlarge_wilderness_chunk(size);
}
else //can't merge with lower, so will only merge with upper (wilderness) and enlarge it
{
meta_to_allocate = merge_with_upper_block(meta);
meta_to_allocate = enlarge_wilderness_chunk(size);
}
merged_or_enlarged = true;
}
if(merged_or_enlarged)
{
reuse_and_split_if_needed(meta_to_allocate, size);
meta_to_allocate->is_free = false;
std::memmove(meta_to_allocate->block_start_address, oldp, size);
return meta_to_allocate->block_start_address;
}
else
{
//else: allocating new block/ reusing one without merging
void* smalloc_res = smalloc(size);
if(!smalloc_res)
{
return NULL;
}
std::memmove(smalloc_res, oldp, meta->size);
sfree(oldp);
return smalloc_res;
}
}
}