-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc.c
More file actions
executable file
·156 lines (127 loc) · 3.32 KB
/
malloc.c
File metadata and controls
executable file
·156 lines (127 loc) · 3.32 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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* A very basic malloc implementation.
* Does not do block merging, or decrease the program break like
* actual malloc, but will reuse freed memory if there's a block that is the right size.
*/
extern char etext, edata, end;
void describe_break() {
char *cbrk = sbrk(0);
printf("======================\n");
printf("ETEXT:\t%10p \n", &etext);
printf("EDATA:\t%10p \n", &edata);
printf("END:\t%10p \n", &end);
printf("BRK:\t%10p \n", cbrk);
printf("HSZ:\t%d \n", cbrk - &end);
printf("======================\n");
}
void error(char *err) {
printf(err);
exit(EXIT_FAILURE);
}
static int needinit = 1;
static struct fblock *hbase;
// Not sure if packed is actually needed here,
// but we rely on adjusting the pointer back from .data
// and assume this is .sz.
struct fblock {
int sz;
struct fblock *prev;
struct fblock *next;
} __attribute__((packed));
struct ublock {
int sz;
char data;
} __attribute__((packed));
int initmalloc() {
int req;
if((hbase = sbrk(0)) == (void*)-1)
return -1;
req = sizeof (struct fblock);
sbrk(req);
hbase->sz = 0;
hbase->prev = NULL;
hbase->next = NULL;
return 0;
}
struct ublock* new_ublock(int reqsz) {
struct ublock *block;
reqsz = sizeof (struct fblock) - sizeof (int) > reqsz ? sizeof (struct fblock) - sizeof (int) : reqsz;
if ((block = sbrk(reqsz)) == (void*)-1)
return NULL;
block->sz = reqsz ;
return block;
}
struct fblock* next_good_fblock(int reqsz) {
struct fblock *cblock;
if (reqsz == 0)
return NULL;
cblock = hbase;
while (1) {
if (cblock->sz >= reqsz) {
return cblock;
} else if (cblock->next == NULL) {
return NULL;
}
cblock = cblock->next;
}
}
void* my_malloc(int n) {
char *current_brk;
struct fblock *goodblock;
struct ublock *newblock;
if((current_brk = sbrk(0)) == (void*)-1)
return NULL;
// Initilize the heap for use with mymalloc
if (needinit) {
if (initmalloc() == -1)
return NULL;
else
needinit = 0;
}
goodblock = next_good_fblock(n);
if (goodblock != NULL) {
goodblock->prev->next = goodblock->next;
return (void*)&(goodblock->prev);
} else {
newblock = new_ublock(n);
if (newblock == NULL)
return NULL;
return (void*)&(newblock->data);
}
}
void my_free(void *mem) {
struct fblock *freed, *second;
freed = (struct fblock*)(mem - sizeof (int));
second = hbase->next;
hbase->next = freed;
freed->next = second;
freed->prev = hbase;
if (second != NULL) {
second->prev = freed;
}
}
int main(int argc, char **argv) {
char *p;
int i;
describe_break();
printf("mallocing!!\n");
p = my_malloc(20);
printf("requested with size 20 -> p:\t%10p \n", p);
memcpy(p, "lmao!", 5);
describe_break();
printf("freeing & mallocing again!!!\n");
my_free(p);
p = my_malloc(6);
printf("requested with size 6 -> p:\t%10p \n", p);
memcpy(p, "lmao!", 6);
printf("string: %s\n", p);
describe_break();
my_free(p);
p = my_malloc(18);
printf("requested with size 18 -> p:\t%10p \n", p);
exit(EXIT_SUCCESS);
}