forked from sadiredd-sv/Malloc--Dynamic-Memory-Allocator
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mm.c
1133 lines (924 loc) · 32.8 KB
/
mm.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
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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* mm.c
* Name: Santosh Nikhil Kumar Adireddy
* AndrewId: sadiredd
DYNAMIC MEMORY ALLOCATOR
------------------------
This is a dynamic memory allocator that manages the computer memory
by creating an array of contiguous allocated and free blocks on the
heap.
When the user makes a malloc request, the memory allocator searches
through the free lists(segregated) to determine which one to start
looking at first based on the requested size and continuing onward
with lists that have bigger size restrictions(LIST_LIMIT). If a free
block is found, a block pointer pointing to the payload of that block
is returned. If no free block is found in the list, memory will be
extended(Extend_heap) increasing the current heap size. In case, the
assigned free block is larger than the minimum free block size and
if the remaining size after assigning this block is larger than
the minimum free block size(24 in my implementation), then splitting
occurs. The footer, previous(predecessor) and next(successor) pointers
are removed and header of the allocated block will be updated
accordingly.
Implementation with Segregated free list and LRU:
------------------------------------------------
Free blocks list: Implemented using segregated lists.The free blocks
are arranged in many linked lists that only contain blocks less than
fixed sizes.
Headers: Both allocated and free blocks have headers that indicate
the size of the blocks, current block's allocation status, and
previous block's allocation status.
Footers: Used only for coalescing of free blocks. Footers are
identical to headers. They are included at the end of each FREE BLOCK
for possible coalescing. Also, pointers to the previous(predecessor)
and next(successor) free block in the segregated list are included
in each free block's payload.
Coalescing: Coalescing is performed to possibly create a block of a
larger size to reduce external fragmentation. Calling "free" or heap
extention casues the addition of a free block to the free list. The
blocks to be coalesced will be first removed from the their list(one
of the segregated). Then, they are joined based on 4 cases to form
a bigger block and adding it to a free list(possibly different).
When there are free requests, the memory allocator determines the size
of the freed block and adds (links) it to the appropriate linked list
by updating the header, adding the footer, and previous and next
pointers to free blocks in the free list.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include "mm.h"
#include "memlib.h"
/* If you want debugging output, use the following macro. When you hand
* in, remove the #define DEBUG line. */
#define DEBUG
#ifdef DEBUG
# define dbg_printf(...) printf(__VA_ARGS__)
#else
# define dbg_printf(...)
#endif
/* do not change the following! */
#ifdef DRIVER
/* create aliases for driver tests */
#define malloc mm_malloc
#define free mm_free
#define realloc mm_realloc
#define calloc mm_calloc
#endif /* def DRIVER */
/* Constants */
#define WSIZE 4
#define DSIZE 8
#define CHUNKSIZE 168
#define OVERHEAD 8
#define MINBLOCK 24
/* For storing in lower 3 bits of header in allocated blocks
and header & footer in free blocks */
#define CURRENTALLOCATED 1
#define PREVIOUSALLOCATED 2
/* Variables used as offsets for
segregated lists headers */
#define SEGLIST1 0
#define SEGLIST2 8
#define SEGLIST3 16
#define SEGLIST4 24
#define SEGLIST5 32
#define SEGLIST6 40
#define SEGLIST7 48
#define SEGLIST8 56
#define SEGLIST9 64
#define SEGLIST10 72
#define SEGLIST11 80
#define SEGLIST12 88
#define SEGLIST13 96
#define SEGLIST14 104
/* Maximum size limit of each list */
#define LIST1_LIMIT 24
#define LIST2_LIMIT 48
#define LIST3_LIMIT 72
#define LIST4_LIMIT 96
#define LIST5_LIMIT 120
#define LIST6_LIMIT 480
#define LIST7_LIMIT 960
#define LIST8_LIMIT 1920
#define LIST9_LIMIT 3840
#define LIST10_LIMIT 7680
#define LIST11_LIMIT 15360
#define LIST12_LIMIT 30720
#define LIST13_LIMIT 61440
/* Total number of segregated lists */
#define TOTALLIST 14
/* Function macros */
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
/* Pack a size and allocated bit into a word */
#define PACK(size, alloc) ((size) | (alloc))
/* Read and write 8 bytes at address p */
#define GET(p) (*((size_t*) (p)))
#define PUT(p, val) (*((size_t*) (p)) = (val))
/* Read and write 4 bytes at address p */
#define GET4BYTES(p) (*((unsigned*) (p)))
#define PUT4BYTES(p, val)(*((unsigned*) (p)) = (val))
/* Read the size and allocated fields from address p */
#define GET_SIZE(p) (GET4BYTES(p) & ~0x7)
#define GET_ALLOC(p) (GET4BYTES(p) & 0x1)
/* Read whether previous block is allocated or not*/
#define GET_PREV_ALLOC(p) (GET4BYTES(p) & 0x2)
/* Given block ptr bp, compute address of its header and footer */
#define HDRP(bp) ((char*)(bp) - WSIZE)
#define FTRP(bp) ((char*)(bp) + GET_SIZE(HDRP(bp)) - DSIZE)
/* Given block ptr bp, compute address of next and prev blocks */
#define NEXT_BLKP(bp) ((char*)(bp) + GET_SIZE((char*)(bp) - WSIZE))
#define PREV_BLKP(bp) ((char*)(bp) - GET_SIZE((char*)(bp) - DSIZE))
/* Given free block addr bp, compute addresses in which the previous
and next free blocks' addresses are stored */
#define SUCCESSOR(bp) ((char*) ((char*)(bp)))
#define PREDECESSOR(bp) ((char*) ((char*)(bp) + DSIZE))
/* Helper functions */
static void *extend_heap(size_t words);
static void *coalesce(void *bp);
static void *find(size_t sizeatstart, size_t actual_size);
static void *find_fit(size_t asize);
static void place(void *bp, size_t asize);
static void addingtoseglist(char *bp, size_t size);
static void removefromseglist(char *bp, size_t size);
/* Global variables-- Base of the heap(heap_listp) */
static char *heap_listp;
char *h_start;
/*
* Function name: Addingtoseglist
* Description: Adds free block to the appropriate segregated list
* by adding it to the front of the list as head &
* and linking it to the previous head
*/
static void addingtoseglist(char *bp, size_t size)
{
/* Address of head of a particular list */
char *listhead;
/* Address pointing to the address of head of a particular list */
char *segstart;
if (size <= LIST1_LIMIT) {
segstart = heap_listp + SEGLIST1;
listhead = (char *) GET(segstart);
} else if (size <= LIST2_LIMIT) {
segstart = heap_listp + SEGLIST2;
listhead = (char *) GET(segstart);
} else if (size <= LIST3_LIMIT) {
segstart = heap_listp + SEGLIST3;
listhead = (char *) GET(segstart);
} else if (size <= LIST4_LIMIT) {
segstart = heap_listp + SEGLIST4;
listhead = (char *) GET(segstart);
} else if (size <= LIST5_LIMIT) {
segstart = heap_listp + SEGLIST5;
listhead = (char *) GET(segstart);
} else if (size <= LIST6_LIMIT) {
segstart = heap_listp + SEGLIST6;
listhead = (char *) GET(segstart);
} else if (size <= LIST7_LIMIT) {
segstart = heap_listp + SEGLIST7;
listhead = (char *) GET(segstart);
} else if (size <= LIST8_LIMIT) {
segstart = heap_listp + SEGLIST8;
listhead = (char *) GET(segstart);
} else if (size <= LIST9_LIMIT) {
segstart = heap_listp + SEGLIST9;
listhead = (char *) GET(segstart);
} else if (size <= LIST10_LIMIT) {
segstart = heap_listp + SEGLIST10;
listhead = (char *) GET(segstart);
} else if (size <= LIST11_LIMIT) {
segstart = heap_listp + SEGLIST11;
listhead = (char *) GET(segstart);
} else if (size <= LIST12_LIMIT) {
segstart = heap_listp + SEGLIST12;
listhead = (char *) GET(segstart);
} else if (size <= LIST13_LIMIT) {
segstart = heap_listp + SEGLIST13;
listhead = (char *) GET(segstart);
} else {
segstart = heap_listp + SEGLIST14;
listhead = (char *) GET(segstart);
}
/* If there is no block in that size list */
if (listhead != NULL) {
/* Set the current block as head */
PUT(segstart, (size_t) bp);
/* Set the current free block's previous pointer to NULL */
PUT(PREDECESSOR(bp), (size_t) NULL);
/* set current free block's next pointer to previous head */
PUT(SUCCESSOR(bp), (size_t) listhead);
/* Set the previous head's previous pointer to
current free block */
PUT(PREDECESSOR(listhead), (size_t) bp);
}
/* If there are blocks in the size list */
else {
/* Set the current block as head */
PUT(segstart, (size_t) bp);
/* Set the free block's next and prev free block
addresses to NULL */
PUT(SUCCESSOR(bp), (size_t) NULL);
PUT(PREDECESSOR(bp), (size_t) NULL);
}
}
/*
* Function: removefromseglist
* Description:
Removes free block from the appropriate segregated list
by linking blocks to the left or right of it. possibly
updating initial list pointer to head of list.
*/
static void removefromseglist(char *bp, size_t size)
{
/* Previous block address */
char *nextaddress = (char *) GET(SUCCESSOR(bp));
/* Next block address */
char *prevaddress = (char *) GET(PREDECESSOR(bp));
if (prevaddress == NULL && nextaddress != NULL) {
/* Update head pointer of segregated list */
if (size <= LIST1_LIMIT)
PUT(heap_listp + SEGLIST1, (size_t) nextaddress);
else if (size <= LIST2_LIMIT)
PUT(heap_listp + SEGLIST2, (size_t) nextaddress);
else if (size <= LIST3_LIMIT)
PUT(heap_listp + SEGLIST3, (size_t) nextaddress);
else if (size <= LIST4_LIMIT)
PUT(heap_listp + SEGLIST4, (size_t) nextaddress);
else if (size <= LIST5_LIMIT)
PUT(heap_listp + SEGLIST5, (size_t) nextaddress);
else if (size <= LIST6_LIMIT)
PUT(heap_listp + SEGLIST6, (size_t) nextaddress);
else if (size <= LIST7_LIMIT)
PUT(heap_listp + SEGLIST7, (size_t) nextaddress);
else if (size <= LIST8_LIMIT)
PUT(heap_listp + SEGLIST8, (size_t) nextaddress);
else if (size <= LIST9_LIMIT)
PUT(heap_listp + SEGLIST9, (size_t) nextaddress);
else if (size <= LIST10_LIMIT)
PUT(heap_listp + SEGLIST10, (size_t) nextaddress);
else if (size <= LIST11_LIMIT)
PUT(heap_listp + SEGLIST11, (size_t) nextaddress);
else if (size <= LIST12_LIMIT)
PUT(heap_listp + SEGLIST12, (size_t) nextaddress);
else if (size <= LIST13_LIMIT)
PUT(heap_listp + SEGLIST13, (size_t) nextaddress);
else
PUT(heap_listp + SEGLIST14, (size_t) nextaddress);
/* Update the new head block's previous to NULL */
PUT(PREDECESSOR(nextaddress), (size_t) NULL);
}
/* If only free block in list, update head pointer to NULL */
else if (prevaddress == NULL && nextaddress == NULL) {
if (size <= LIST1_LIMIT)
PUT(heap_listp + SEGLIST1, (size_t) NULL);
else if (size <= LIST2_LIMIT)
PUT(heap_listp + SEGLIST2, (size_t) NULL);
else if (size <= LIST3_LIMIT)
PUT(heap_listp + SEGLIST3, (size_t) NULL);
else if (size <= LIST4_LIMIT)
PUT(heap_listp + SEGLIST4, (size_t) NULL);
else if (size <= LIST5_LIMIT)
PUT(heap_listp + SEGLIST5, (size_t) NULL);
else if (size <= LIST6_LIMIT)
PUT(heap_listp + SEGLIST6, (size_t) NULL);
else if (size <= LIST7_LIMIT)
PUT(heap_listp + SEGLIST7, (size_t) NULL);
else if (size <= LIST8_LIMIT)
PUT(heap_listp + SEGLIST8, (size_t) NULL);
else if (size <= LIST9_LIMIT)
PUT(heap_listp + SEGLIST9, (size_t) NULL);
else if (size <= LIST10_LIMIT)
PUT(heap_listp + SEGLIST10, (size_t) NULL);
else if (size <= LIST11_LIMIT)
PUT(heap_listp + SEGLIST11, (size_t) NULL);
else if (size <= LIST12_LIMIT)
PUT(heap_listp + SEGLIST12, (size_t) NULL);
else if (size <= LIST13_LIMIT)
PUT(heap_listp + SEGLIST13, (size_t) NULL);
else
PUT(heap_listp + SEGLIST14, (size_t) NULL);
}
/* If head of list, update head pointer to next free block */
else if (prevaddress != NULL && nextaddress == NULL) {
/* Update new tail block's next to null */
PUT(SUCCESSOR(prevaddress), (size_t) NULL);
/* If in middle of a list, link blocks on either sides */
} else {
/* Link next block's previous to current's previous block */
PUT(PREDECESSOR(nextaddress), (size_t) prevaddress);
/* Link previous block's next to current's next block */
PUT(SUCCESSOR(prevaddress), (size_t) nextaddress);
}
}
/*
* Extend_heap - Extends the heap upward of size bytes
and adds free block to appropriate free list before
coalescing.
*/
static void *extend_heap(size_t words)
{
char *bp;
if ((long) (bp = mem_sbrk(words)) < 0)
return NULL;
/* Change epilogue header to new free block header */
PUT4BYTES(HDRP(bp), PACK(words, GET_PREV_ALLOC(HDRP(bp))));
/* Set new free block footer */
PUT4BYTES(FTRP(bp), GET4BYTES(HDRP(bp)));
/* Add to segregated list */
addingtoseglist(bp, words);
/* Set new epilogue header */
PUT4BYTES(HDRP(NEXT_BLKP(bp)), PACK(0, 1));
/* coalesce free blocks if needed */
return coalesce(bp);
}
/*
* coalesce - combines free blocks from left and right with current
* free block to form larger free block. removes current
* and/or left and/or right free blocks from their own lists
* and add to appropriate free list of new combined sizs.
* Called after each addition of a free block.
*/
static void *coalesce(void *bp)
{
/* store prev and next block's info */
size_t prev_alloc = GET_PREV_ALLOC(HDRP(bp));
size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
/* get size of current, prev, next free block (including header) */
size_t size = GET_SIZE(HDRP(bp));
size_t nsize = 0;
size_t psize = 0;
/* previous block's address and its header address */
char *prev_hd;
char *prev_blk;
char *next_blk;
/* 4 Cases */
/* Case 1: Prev and Next both allocated */
if (prev_alloc && next_alloc) {
/* return current pointer to free block */
return bp;
}
/* Case 2: Prev allocated, Next free */
else if (prev_alloc && !next_alloc) {
next_blk = NEXT_BLKP(bp);
/* remove current free block and next free block from lists */
removefromseglist(bp, size);
removefromseglist(next_blk, GET_SIZE(HDRP(next_blk)));
/* new block size is current free size plus next free size */
size += GET_SIZE(HDRP(next_blk));
/* change header to reflect new size */
PUT4BYTES(HDRP(bp), PACK(size, prev_alloc));
/* change footer to reflect new size */
PUT4BYTES(FTRP(bp), PACK(size, prev_alloc));
/* add new free block to segregated list */
addingtoseglist(bp, size);
/* return current pointer to free block
since block expanded to next */
return bp;
}
/* Case 3- Prev free, Next allocated */
else if (!prev_alloc && next_alloc) {
/* get previous block's location and header location */
prev_blk = PREV_BLKP(bp);
prev_hd = HDRP(prev_blk);
psize = GET_SIZE(prev_hd);
/* remove current free block and prev free block from lists */
removefromseglist(bp, size);
removefromseglist(prev_blk, psize);
/* size is current free size plus prev free size */
size += psize;
/* change header to reflect new size */
PUT4BYTES(prev_hd, PACK(size, GET_PREV_ALLOC(prev_hd)));
/* change footer to reflect new size */
PUT4BYTES(FTRP(prev_blk), GET4BYTES(prev_hd));
/* add new free block to segregated list */
addingtoseglist(prev_blk, size);
/* return prev pointer to prev block
since block expanded to prev */
return prev_blk;
}
/* Case 4- Prev free, Next free */
else {
/* Get previous block's location and header location */
prev_blk = PREV_BLKP(bp);
prev_hd = HDRP(prev_blk);
next_blk = NEXT_BLKP(bp);
psize = GET_SIZE(prev_hd);
nsize = GET_SIZE(HDRP(next_blk));
/* Remove current, prev, and next free block from lists */
removefromseglist(bp, size);
removefromseglist(prev_blk, psize);
removefromseglist(next_blk, nsize);
/* Size is current free plus prev free and next free size */
size += psize + nsize;
/* Change header to reflect new size */
PUT4BYTES(prev_hd, PACK(size, GET_PREV_ALLOC(prev_hd)));
/* Change footer to reflect new size */
PUT4BYTES(FTRP(prev_blk), GET4BYTES(prev_hd));
/* Add new free block to segregated list */
addingtoseglist(prev_blk, size);
/* Return prev pointer to free block
since block expanded to prev */
return prev_blk;
}
}
/*
* mm_init - initializes the heap by storing pointers to
* start of each free list at beginning of heap as well as
* creating the initial prologue and eplilogue blocks.
* The heap is also initially extended to CHUNKSIZE bytes.
*/
int mm_init(void)
{
/* Allocating segregated free list pointers on heap */
if ((heap_listp = mem_sbrk(TOTALLIST * DSIZE)) == NULL)
return -1;
/* Creating prologue and epilogue */
if ((h_start = mem_sbrk(4 * WSIZE)) == NULL)
return -1;
/* Alignment padding */
PUT4BYTES(h_start, 0);
/* Prologue header */
PUT4BYTES(h_start + WSIZE, PACK(DSIZE, 1));
/* Prologue footer */
PUT4BYTES(h_start + (2 * WSIZE), PACK(DSIZE, 1));
/* Epilogue header */
PUT4BYTES(h_start + (3 * WSIZE),
PACK(0, PREVIOUSALLOCATED | CURRENTALLOCATED));
/* Initializing the segregated list pointers on heap */
PUT(heap_listp + SEGLIST1, (size_t) NULL);
PUT(heap_listp + SEGLIST2, (size_t) NULL);
PUT(heap_listp + SEGLIST3, (size_t) NULL);
PUT(heap_listp + SEGLIST4, (size_t) NULL);
PUT(heap_listp + SEGLIST5, (size_t) NULL);
PUT(heap_listp + SEGLIST6, (size_t) NULL);
PUT(heap_listp + SEGLIST7, (size_t) NULL);
PUT(heap_listp + SEGLIST8, (size_t) NULL);
PUT(heap_listp + SEGLIST9, (size_t) NULL);
PUT(heap_listp + SEGLIST10, (size_t) NULL);
PUT(heap_listp + SEGLIST11, (size_t) NULL);
PUT(heap_listp + SEGLIST12, (size_t) NULL);
PUT(heap_listp + SEGLIST13, (size_t) NULL);
PUT(heap_listp + SEGLIST14, (size_t) NULL);
/* Create initial empty space of CHUNKSIZE bytes */
if (extend_heap(CHUNKSIZE) == NULL)
return -1;
return 0;
}
/*
* Function: find_fit
* Description: Searches through all free lists containing
blocks with size greater than size requested and returns
pointer to a satisfacotry free block or NULL if none
are found.
*/
static void *find_fit(size_t asize)
{
size_t sizeatstart;
char *bp = NULL;
/* Segregated lists- Size breakdown */
if (asize <= LIST1_LIMIT) {
for (sizeatstart = 0; sizeatstart < TOTALLIST; sizeatstart++) {
if ((bp = find(sizeatstart, asize)) != NULL)
return bp;
}
} else if (asize <= LIST2_LIMIT) {
for (sizeatstart = 1; sizeatstart < TOTALLIST; sizeatstart++) {
if ((bp = find(sizeatstart, asize)) != NULL)
return bp;
}
} else if (asize <= LIST3_LIMIT) {
for (sizeatstart = 2; sizeatstart < TOTALLIST; sizeatstart++) {
if ((bp = find(sizeatstart, asize)) != NULL)
return bp;
}
} else if (asize <= LIST4_LIMIT) {
for (sizeatstart = 3; sizeatstart < TOTALLIST; sizeatstart++) {
if ((bp = find(sizeatstart, asize)) != NULL)
return bp;
}
} else if (asize <= LIST5_LIMIT) {
for (sizeatstart = 4; sizeatstart < TOTALLIST; sizeatstart++) {
if ((bp = find(sizeatstart, asize)) != NULL)
return bp;
}
} else if (asize <= LIST6_LIMIT) {
for (sizeatstart = 5; sizeatstart < TOTALLIST; sizeatstart++) {
if ((bp = find(sizeatstart, asize)) != NULL)
return bp;
}
} else if (asize <= LIST7_LIMIT) {
for (sizeatstart = 6; sizeatstart < TOTALLIST; sizeatstart++) {
if ((bp = find(sizeatstart, asize)) != NULL)
return bp;
}
} else if (asize <= LIST8_LIMIT) {
for (sizeatstart = 7; sizeatstart < TOTALLIST; sizeatstart++) {
if ((bp = find(sizeatstart, asize)) != NULL)
return bp;
}
} else if (asize <= LIST9_LIMIT) {
for (sizeatstart = 8; sizeatstart < TOTALLIST; sizeatstart++) {
if ((bp = find(sizeatstart, asize)) != NULL)
return bp;
}
} else if (asize <= LIST10_LIMIT) {
for (sizeatstart = 9; sizeatstart < TOTALLIST; sizeatstart++) {
if ((bp = find(sizeatstart, asize)) != NULL)
return bp;
}
} else if (asize <= LIST11_LIMIT) {
for (sizeatstart = 10; sizeatstart < TOTALLIST; sizeatstart++) {
if ((bp = find(sizeatstart, asize)) != NULL)
return bp;
}
} else if (asize <= LIST12_LIMIT) {
for (sizeatstart = 11; sizeatstart < TOTALLIST; sizeatstart++) {
if ((bp = find(sizeatstart, asize)) != NULL)
return bp;
}
} else if (asize <= LIST13_LIMIT) {
for (sizeatstart = 12; sizeatstart < TOTALLIST; sizeatstart++) {
if ((bp = find(sizeatstart, asize)) != NULL)
return bp;
}
} else {
sizeatstart = 13;
if ((bp = find(sizeatstart, asize)) != NULL) {
return bp;
}
}
return bp;
}
/*
* Function: place
* Description:
Allocates block of memory at address bp. if remaining
memory is >= min free block size, allocate requested
amount and form new free block to add to segregated
list. If not, allocate entire block of memory at
address bp.
*/
static void place(void *bp, size_t asize)
{
/* Original free block size */
size_t csize = GET_SIZE(HDRP(bp));
/* Remaining free block size */
size_t remainsize = csize - asize;
/* Next adjacent block address */
char *nextaddress = NEXT_BLKP(bp);
/* Remove free block from the appropriate seg list */
removefromseglist(bp, csize);
/* If the remaining block is larger than min block size of
24 bytes, then splitting is done to form new free block
*/
if (remainsize >= MINBLOCK) {
/* Update new header information, store info bits */
PUT4BYTES(HDRP(bp),
PACK(asize,
GET_PREV_ALLOC(HDRP(bp)) | CURRENTALLOCATED));
/* Update next block's address to remaining free block's address */
nextaddress = NEXT_BLKP(bp);
/* Inform next adjacent free block that its previous block
is allocated */
PUT4BYTES(HDRP(nextaddress), remainsize | PREVIOUSALLOCATED);
PUT4BYTES(FTRP(nextaddress), remainsize | PREVIOUSALLOCATED);
/* Add remaining free block to appropriate segregated list */
addingtoseglist(nextaddress, remainsize);
/* If the remaining is not larger than min block, then assign
entire free block to allocated */
} else {
/* Update new header information, store info bits */
PUT4BYTES(HDRP(bp),
PACK(csize,
GET_PREV_ALLOC(HDRP(bp)) | CURRENTALLOCATED));
/* Inform next adjacent block that its previous block
is allocated */
PUT4BYTES(HDRP(nextaddress),
GET4BYTES(HDRP(nextaddress)) | PREVIOUSALLOCATED);
/* Update next adjacent block's footer only if free */
if (!GET_ALLOC(HDRP(nextaddress)))
PUT4BYTES(FTRP(nextaddress), GET4BYTES(HDRP(nextaddress)));
}
}
/*
* Function: find
* Description:
Find searches through a particular size free list
to find a possible free block >= size requested and returns
pointer to a possible free block or NULL if none are found.
*/
static void *find(size_t sizeatstart, size_t actual_size)
{
char *current = NULL;
/* Finding which list to look into */
if (sizeatstart == 0)
current = (char *) GET(heap_listp + SEGLIST1);
else if (sizeatstart == 1)
current = (char *) GET(heap_listp + SEGLIST2);
else if (sizeatstart == 2)
current = (char *) GET(heap_listp + SEGLIST3);
else if (sizeatstart == 3)
current = (char *) GET(heap_listp + SEGLIST4);
else if (sizeatstart == 4)
current = (char *) GET(heap_listp + SEGLIST5);
else if (sizeatstart == 5)
current = (char *) GET(heap_listp + SEGLIST6);
else if (sizeatstart == 6)
current = (char *) GET(heap_listp + SEGLIST7);
else if (sizeatstart == 7)
current = (char *) GET(heap_listp + SEGLIST8);
else if (sizeatstart == 8)
current = (char *) GET(heap_listp + SEGLIST9);
else if (sizeatstart == 9)
current = (char *) GET(heap_listp + SEGLIST10);
else if (sizeatstart == 10)
current = (char *) GET(heap_listp + SEGLIST11);
else if (sizeatstart == 11)
current = (char *) GET(heap_listp + SEGLIST12);
else if (sizeatstart == 12)
current = (char *) GET(heap_listp + SEGLIST13);
else if (sizeatstart == 13)
current = (char *) GET(heap_listp + SEGLIST14);
/* Finding available free block in list */
while (current != NULL) {
if (actual_size <= GET_SIZE(HDRP(current))) {
break;
}
current = (char *) GET(SUCCESSOR(current));
}
return current;
}
/*
* malloc - returns a pointer to an allocated block payload
* of at least size bytes. It first searches through different
* size free lists for a free block and if needed, extend the
* heap.
*/
void *malloc(size_t size)
{
size_t asize; /* adjusted block size */
size_t extendsize; /* size to be extended */
char *bp;
/* Ignore less size requests */
if (size <= 0)
return NULL;
/* Adjust block size to include header and alignment requests */
if (size <= 2 * DSIZE)
asize = MINBLOCK;
else
/* Rounds up to the nearest multiple of ALIGNMENT */
asize = (((size_t) (size + WSIZE) + 7) & ~0x7);
/* Search through heap for possible fit */
if ((bp = find_fit(asize)) != NULL) {
place(bp, asize); /* Actual assignment */
return bp;
}
/* If no fit, get more memory and allocate memory */
extendsize = MAX(asize, CHUNKSIZE);
if ((bp = extend_heap(extendsize)) == NULL)
return NULL;
place(bp, asize); /* Assignment */
return bp;
}
/*
* Function: free
* Description: It frees the block pointed to by ptr and returns
nothing. Then, it adds freed block to apprpriate size
free list.
*/
void free(void *ptr)
{
char *nxtblkheader;
size_t size;
if (ptr == NULL)
return;
size = GET_SIZE(HDRP(ptr));
nxtblkheader = HDRP(NEXT_BLKP(ptr));
/* Update header and footer to unallocated */
PUT4BYTES(HDRP(ptr), size | GET_PREV_ALLOC(HDRP(ptr)));
PUT4BYTES(FTRP(ptr), GET4BYTES(HDRP(ptr)));
/* Update next block, its previous is no longer allocated */
PUT4BYTES(nxtblkheader,
GET_SIZE(nxtblkheader) | GET_ALLOC(nxtblkheader));
/* Add free block to appropriate segregated list */
addingtoseglist(ptr, size);
coalesce(ptr);
}
/*
* Function: realloc
* Description: Returns a pointer to an allocated region
of at least size bytes with the contents of pointer
oldptr up to the size bytes. The default action is to
simply free current allocated block and copy size bytes
at oldptr to a new free block and free oldptr.
*/
void *realloc(void *oldptr, size_t size)
{
/* After malloc, new address with size "size" */
char *newaddress;
/* Size to be copied */
size_t copysize;
/* If ptr is NULL, call equivalent to malloc(size) */
if (oldptr == NULL)
return malloc(size);
/* If size = 0, call equivalanet to free(oldptr), returns null */
if (size == 0) {
free(oldptr);
return NULL;
}
/* Else, allocate new free block, copy old content over */
newaddress = malloc(size);
copysize = MIN(size, GET_SIZE(HDRP(oldptr)) - WSIZE);
/* Move from source to dest */
memmove(newaddress, oldptr, copysize);
/* Free the old block */
free(oldptr);
return newaddress;
}
/*
* calloc - Allocates memory for an array of nmemb elements
* of size bytes each and returns a pointer to the allocated
* memory. The memory is set to zero before returning.
*/
void *calloc(size_t nmemb, size_t size)
{
size_t i;
size_t tsize = nmemb * size;
char *ptr = malloc(tsize);
char *temp = ptr;
for (i = 0; i < nmemb; i++) {
*temp = 0;
temp = temp + size;
}
return ptr;
}
/*
* Return whether the pointer is in the heap.
* May be useful for debugging.
*/
static int in_heap(const void *p)
{
return p <= mem_heap_hi() && p >= mem_heap_lo();
}
/*
* Return whether the pointer is aligned.
* May be useful for debugging.
*/
static int aligned(const void *p)
{
return (size_t) (((size_t) (p) + 7) & ~0x7) == (size_t) p;
}
/*
* mm_checkheap:
This function scans the heap and checks it
for consistency.
* Invariants:
-> All blocks are aligned to 8 bytes (checked)
-> All blocks store info if previous
block is allocated
-> All free blocks' headers/footers
are the same (checked)
-> Each free list contains only blocks
within size ranges (checked)
-> Each block is within the heap (checked)
-> No consecutive free blocks (checked)
*/
void mm_checkheap(int verbose)
{
/* Variables for testing if block falls under appropriate size list */
char *listpointer = NULL;
unsigned minimumblocksize = 0;
unsigned maximumblocksize = 0;
unsigned sizeatstart = 0;
/* Pointer to the very first block */
char *ptr = heap_listp + 2 * DSIZE + TOTALLIST * DSIZE;
/* If block is free, check for:
1. Header/footer mismatch
2. Next/prev free pointer inconsistencies
3. Adjacent free blocks
*/
if (!GET_ALLOC(HDRP(ptr))) {
/*Header/footer mismatch*/
if ((GET4BYTES(HDRP(ptr)) != GET4BYTES(FTRP(ptr))))
printf("Free block pointer %p: \
header and footer mismatch\n", ptr);