mymalloc.c
5.05 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
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "mymalloc.h"
typedef struct _blkdata
{
unsigned long size; /* rozmiar bloku wlaczajac naglowek */
unsigned int flags;
}
blkdata;
#define MM_OCCUPIED 1
#define MM_LAST 2
static void *begin;
int
mymalloc_init (unsigned long size)
{
blkdata *b;
assert (size > 0);
begin = sbrk (size);
if (!begin)
return 0;
b = (blkdata *) begin;
b->size = size;
b->flags = MM_LAST;
return 1;
};
void *
mymalloc (unsigned long size)
{
blkdata *b = (blkdata *) begin;
printf ("Request to allocate %lu bytes\n", size);
while (1)
{
if ((b->size > size + sizeof (blkdata)) && !(b->flags & MM_OCCUPIED))
{
unsigned long after = b->size - size - sizeof (blkdata);
if (after > sizeof (blkdata))
{
blkdata *na =
(blkdata *) ((void *) b + size + sizeof (blkdata));
na->flags = 0;
if (b->flags & MM_LAST)
na->flags |= MM_LAST;
na->size = after;
b->size = size + sizeof (blkdata);
b->flags = MM_OCCUPIED;
return ((void *) b) + sizeof (blkdata);
}
else /* wez caly blok */
{
b->flags |= MM_OCCUPIED;
return ((void *) b) + sizeof (blkdata);
}
};
if (b->flags & MM_LAST)
break;
b = (blkdata *) ((void *) b + b->size);
};
return NULL;
}
void
myfree (void *ptr)
{
blkdata *b;
blkdata *nextb = NULL;
blkdata *prevb = NULL;
if (ptr == NULL)
return;
b = (blkdata *) (ptr - sizeof (blkdata));
printf ("Request to free %lu bytes at the address %p\n", b->size - sizeof (blkdata), b);
if ((void *) b != begin)
{
/* znajdz poprzedni blok */
prevb = (blkdata *) begin;
while (1)
{
blkdata *next;
assert (!(prevb->flags & MM_LAST));
next = (blkdata *) ((void *) prevb + prevb->size);
if (next == b)
break;
prevb = next;
};
};
/* znajdz nastepny blok */
if (!(b->flags & MM_LAST))
nextb = (blkdata *) ((void *) b + b->size);
/* zaznacz blok jako wolny */
b->flags &= ~MM_OCCUPIED;
/* skonsoliduj z nastepnym */
if (nextb && !(nextb->flags & MM_OCCUPIED))
{
if (nextb->flags & MM_LAST)
b->flags = MM_LAST;
b->size += nextb->size;
}
/* skonsoliduj z poprzednim */
if (prevb && !(prevb->flags & MM_OCCUPIED))
{
if (b->flags & MM_LAST)
prevb->flags |= MM_LAST;
prevb->size += b->size;
};
}
static void *
copyrealloc (void *ptr, unsigned long size, unsigned long oldsize)
{
void *blk = mymalloc (size);
if (blk == NULL)
return NULL;
memcpy (blk, ptr, oldsize);
myfree (ptr);
return blk;
}
void *
myrealloc (void *ptr, unsigned long size)
{
blkdata *b;
if (ptr == NULL)
return mymalloc (size);
b = (blkdata *) (ptr - sizeof (blkdata));
printf ("Request to reallocate %lu bytes to %lu bytes, old address=%p\n",
b->size - sizeof (blkdata), size, b);
if (size == 0)
{
myfree (ptr);
return NULL;
};
if (size <= b->size - sizeof (blkdata)) /* zmniejszenie bloku */
{
unsigned long after = b->size - size - sizeof (blkdata);
blkdata *nextb = NULL;
if (!(b->flags & MM_LAST))
nextb = (blkdata *) ((void *) b + b->size);
blkdata *na = ((void *) b) + size + sizeof (blkdata);
if (nextb && !(nextb->flags & MM_OCCUPIED))
/* skonsoliduj z nastepnym */
{
unsigned int flags = nextb->flags;
unsigned long size = after + nextb->size;
na->flags = flags;
na->size = size;
b->size = size + sizeof (blkdata);
}
else if (after > sizeof (blkdata))
{
na->flags = 0;
if (b->flags & MM_LAST)
na->flags |= MM_LAST;
na->size = after;
b->size = size + sizeof (blkdata);
b->flags = MM_OCCUPIED;
}
return ptr;
}
else /* zwiekszenie bloku */
{
blkdata *next = (blkdata *) ((void *) b + b->size);
if (!(b->flags & MM_LAST) && !(next->flags & MM_OCCUPIED))
{
unsigned long totalsize = b->size + next->size;
unsigned long after = totalsize - size - sizeof (blkdata);
if (size + sizeof (blkdata) > totalsize) /* brak miejsca w nastepnym bloku */
return copyrealloc (ptr, size, b->size - sizeof (blkdata));
if (next->flags & MM_LAST)
b->flags |= MM_LAST;
if (after > sizeof (blkdata))
{
blkdata *na =
(blkdata *) ((void *) b + size + sizeof (blkdata));
na->flags = 0;
if (b->flags & MM_LAST)
na->flags |= MM_LAST;
na->size = after;
b->size = size + sizeof (blkdata);
b->flags = MM_OCCUPIED;
}
else
b->size += next->size;
return ptr;
}
else /* nastepny zajety albo biezacy ostatni */
return copyrealloc (ptr, size, b->size - sizeof (blkdata));
}
}
void
mymalloc_dump ()
{
blkdata *b = (blkdata *) begin;
unsigned long totalsize = 0;
printf ("Mymalloc block list dump\n");
while (1)
{
printf ("Block at the address %p flags %c%c size %lu\n", b,
(b->flags & MM_OCCUPIED) ? 'O' : 'F',
(b->flags & MM_LAST) ? 'L' : ' ', b->size);
totalsize += b->size;
if (b->flags & MM_LAST)
break;
b = (blkdata *) ((void *) b + b->size);
};
printf ("Total size:%lu\n", totalsize);
}