-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmymalloc.c
More file actions
116 lines (98 loc) · 2.33 KB
/
Copy pathmymalloc.c
File metadata and controls
116 lines (98 loc) · 2.33 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
#include <stdio.h>
#include <unistd.h>
#include "mymalloc.h"
// MACRO FOR SIZE OF HEADER STRUCTURE
#define HEADER_SIZE sizeof(struct Memory)
// ENDS OF LINKED LIST
struct Memory *head = NULL;
struct Memory *tail = NULL;
// CUSTOM MALLOC
void *my_lastfit_malloc(int size){
struct Memory *temp;
// INVALID INPUT TO MALLOC
if(size <=0){
return NULL;
}
temp = getMemory(size);
// SUFFICIENTLY LARGE FREE SPACE IS FOUND
if(temp){
temp->free = 0;
return temp + 1;
}
// MOVING BRK
void *space = sbrk(size + HEADER_SIZE);
// SBRK FAILED IF CASE
if(space == (void *) -1){
return NULL;
}
//ASSIGNING STUFF TO HEADER
temp = space;
temp->free = 0;
temp->next = NULL;
temp->size = size;
// IF CASE FOR FIRST MALLOC DATA
if(head == NULL){
temp->prev = NULL;
head = temp;
}
// TAIL ALREADY SET
if(tail != NULL){
temp->prev = NULL;
tail->next = temp;
}
else{
// ONLY 1 MALLOCED SPACE MEANING
//TAIL AND HEAD AT THE SAME SPOT
tail = head;
}
tail = temp;
//RETURN TEMP + 1 BECAUSE WE WANT
//TO ADD ONE BYTE WHICH WILL GO
//TO THE NEXT ADDRESS
return temp + 1;
}
// MY FREE FUNCTION
void my_free(void *ptr){
// INVALID FREE INPUT
if(ptr == NULL){
return;
}
// GETTING PTR-1 BECAUSE WE DID TEMP + 1 IN MALLOC
struct Memory *freeSpace = (struct Memory *)ptr - 1;
// IF ONE NODE OF MEMORY LEFT
if(head == tail){
head = NULL;
tail = NULL;
}
else{
struct Memory *memoryAt = head;
// GOING TO THE END THE LINKED LIST
while(memoryAt->next != tail){
// 2 FREES TOGTHER MERGING
if(memoryAt->free && memoryAt->next->free){
// SAVING THE SIZE
memoryAt->size += memoryAt->next->size + HEADER_SIZE;
memoryAt->next = memoryAt->next->next;
//DEALLOCATING
sbrk(0 - (memoryAt->size + HEADER_SIZE));
}
memoryAt = memoryAt->next;
}
tail = memoryAt;
tail->next = NULL;
}
sbrk(0 - (freeSpace->size + HEADER_SIZE));
}
// GO TO THE NEXT FREE MEMORY WTH ENOUGH SPACE
struct Memory *getMemory(int size){
//LOOP STARTS FROM TAIL AS WE ARE MOVING DOWNT THE STACK
struct Memory *memoryAt = tail;
// WHILE LOOP TILL THE END
while(memoryAt != NULL){
if (memoryAt->free && memoryAt->size >= size){
return memoryAt;
}
memoryAt = memoryAt->prev;
}
return NULL;
}