-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmymalloc.c
143 lines (122 loc) · 3.79 KB
/
mymalloc.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
#include <stdio.h>
#include <stdlib.h>
#include "mymalloc.h"
struct mm_block_def
{
struct mm_block_def *next;
struct mm_block_def *prev;
int size;
unsigned char *buffer;
int isFree; // 1: free, 0: allocated
};
unsigned char malloc_size[MAX_MALLOC_SIZE];
static struct mm_block_def *head = NULL;
void initmyMalloc()
{
head = (struct mm_block_def*)malloc_size;
if (head == NULL) {
return;
}
head->prev = NULL;
head->next = NULL;
head->size = sizeof(malloc_size) - sizeof(struct mm_block_def);
head->buffer = (unsigned char *)(head + sizeof(struct mm_block_def));
head->isFree = 1;
}
void *mymalloc(int size)
{
if (size <= 0) {
return NULL;
}
struct mm_block_def *current = head;
while(current != NULL && current->size <= size){
current = current->next;
}
if(current == NULL){
return NULL;
}
//bos alani bol
if(current->size >= size + sizeof(struct mm_block_def)){
struct mm_block_def *newBlock = (struct mm_block_def *)(current->buffer + size);
newBlock->buffer = current->buffer + size + sizeof(struct mm_block_def);
newBlock->size = current->size - size - sizeof(struct mm_block_def);
newBlock->prev = current->prev;
newBlock->next = current->next;
newBlock->isFree = 1;
current->size = size;
current->isFree = 0;
if(current == head){
head = newBlock;
}
else if(current->prev!= NULL){
current->prev->next = newBlock;
}
if(current->next != NULL){
current->next->prev = newBlock;
}
current->next = newBlock;
}
return current;
}
void myfree(void *buffer)
{
if (buffer == NULL) {
return;
}
struct mm_block_def *block = (struct mm_block_def *)buffer;
block->buffer = (unsigned char *)buffer + sizeof(struct mm_block_def);
block->isFree = 1;
struct mm_block_def *current = head;
while (current != NULL && current->buffer <= block->buffer) {
current = current->next;
}
//buffer degerlerinde sorun var
//solda bos blok varsa mergele
if (current != NULL && current->prev != NULL && current->prev->buffer + current->prev->size + sizeof(struct mm_block_def) == block->buffer) { //current boş değil ve current head değil ve önceki boş blok merge edilebilir
current->prev->size += block->size + sizeof(struct mm_block_def);
current->prev->next = block->next;
}
//sagda bos blok varsa mergele
if (current != NULL && block->buffer + block->size + sizeof(struct mm_block_def) == current->buffer) { //current boş değil ve sonraki boş blok merge edilebilir
block->size += current->size + sizeof(struct mm_block_def);
block->next = current->next;
}
block->next = head;
block->prev = NULL;
head = block;
}
void printmyMallocFreeList() {
struct mm_block_def *current = head;
while (current != NULL) {
if (current->isFree) {
printf("block: %p\tsize: %d\n", current, current->size);
current = current->next;
}
}
printf("---------------\n");
}
int main()
{
initmyMalloc();
//head değerlerini printle
printf("head:\t%x\n", head);
printf("head->size:\t%d\n", head->size);
printf("head->next:\t%x\n", head->next);
printf("head->prev:\t%x\n", head->prev);
printf("head->buffer:\t%x\n", head->buffer);
printf("-------------------------------\n");
char *a = mymalloc(200);
printmyMallocFreeList();
myfree(a);
printmyMallocFreeList();
char *c = mymalloc(50);
mymalloc(80);
char *b = mymalloc(20);
printmyMallocFreeList();
myfree(b);
mymalloc(800);
mymalloc(300);
myfree(c);
printmyMallocFreeList();
return 0;
}