-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhw07_malloc.c
212 lines (195 loc) · 3.99 KB
/
hw07_malloc.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
#include <stdlib.h>
#include <sys/mman.h>
#include <stdio.h>
#include <pthread.h>
#include "xmalloc.h"
#include <pthread.h>
#include <string.h>
typedef struct memblock {
size_t size;
union {
struct memblock* next;
char data[1];
};
} memblock,free_list;
size_t const PAGE_SIZE=4096;
size_t const MIN_ALLOC_SIZE=2*sizeof(size_t);
static free_list list={0,{0}};
static pthread_mutex_t list_mutex=PTHREAD_MUTEX_INITIALIZER;
static size_t div_up(size_t xx,size_t yy)
{
return (xx+yy-1)/yy;
}
static memblock* map_block(size_t size)
{
memblock* ret=mmap(0,size,PROT_READ|PROT_WRITE,MAP_ANONYMOUS|MAP_PRIVATE,-1,0);
ret->size=size;
return ret;
}
static memblock* next_block(memblock const* bl)
{
return (memblock*)(((char*)bl)+bl->size);
}
static int coelescable(memblock const* a,memblock const* b)
{
return (next_block(a)==b);
//&&((size_t)a/PAGE_SIZE==(size_t)b/PAGE_SIZE);
}
static void insert_block_nonempty(memblock* block)
{
/*puts(__FUNCTION__);
printf("%x %x %ld\n",block,(void*)block->size,block->size);
for(free_list* head=list.next;head;head=head->next)
{
printf("%x %x %ld %x\n",head,(void*)head->size,head->size,head->next);
}*/
free_list* prev=&list;
free_list* head=list.next;
for(;head;prev=head,head=head->next)
{
if(block<head)
{
if(coelescable(block,head))
{
size_t const combined_size=block->size+head->size;
//printf("combined size %ld\n",combined_size);
if(coelescable(prev,block))
{
prev->size+=combined_size;
prev->next=head->next;
}
else
{
block->next=head->next;
block->size=combined_size;
prev->next=block;
}
}
else if(coelescable(prev,block))
{
prev->size+=block->size;
}
else
{
prev->next=block;
block->next=head;
}
return;
}
}
if(coelescable(prev,block))
{
prev->size+=block->size;
}
else
{
prev->next=block;
block->next=head;
}
}
static size_t fix_size(size_t size)
{
if(size<MIN_ALLOC_SIZE-sizeof(size_t))
{
return MIN_ALLOC_SIZE;
}
return div_up(size+sizeof(size_t),sizeof(size_t))*sizeof(size_t);
}
void* xmalloc(size_t _size)
{
/*puts(__FUNCTION__);
printf("Requested: %ld\n",_size);
for(free_list* head=list.next;head;head=head->next)
{
printf("%x %x %ld\n",head,(void*)head->size,head->size);
}*/
size_t const size=fix_size(_size);
if(size>=PAGE_SIZE)
{
//printf("big %ld\n",size);
memblock* block=map_block(size);
return block->data;
}
else
{
pthread_mutex_lock(&list_mutex);
free_list* prev=&list;
for(free_list* head=list.next;head;prev=head,head=head->next)
{
size_t const block_size=head->size;
if(block_size>=size)
{
void* ret=head->data;
size_t const remaining_size=block_size-size;
if(remaining_size<MIN_ALLOC_SIZE)
{
prev->next=head->next;
}
else
{
free_list* next=head->next;
head->size=size;
head=next_block(head);
head->size=remaining_size;
head->next=next;
prev->next=head;
}
pthread_mutex_unlock(&list_mutex);
return ret;
}
}
memblock* block=map_block(size);
size_t const remaining_size=PAGE_SIZE-size;
if(remaining_size>=MIN_ALLOC_SIZE)
{
memblock* rest=next_block(block);
rest->size=remaining_size;
insert_block_nonempty(rest);
}
else
{
block->size=PAGE_SIZE;
}
pthread_mutex_unlock(&list_mutex);
return block->data;
}
}
void
xfree(void* item)
{
memblock* block=(memblock*)((char*)item-sizeof(size_t));
size_t const size=block->size;
//printf("size free %ld\n",size);
if(size>PAGE_SIZE)
{
munmap(block,size);
}
else
{
pthread_mutex_lock(&list_mutex);
insert_block_nonempty(block);
pthread_mutex_unlock(&list_mutex);
}
}
void*
xrealloc(void* item,size_t _size)
{
if(item==0)
{
return xmalloc(_size);
}
size_t const size=fix_size(_size);
memblock* block=(memblock*)((char*)item-sizeof(size_t));
size_t const block_size=block->size;
if(block_size>=size)
{
return item;
}
else
{
void* data=xmalloc(_size);
memcpy(data,item,block_size-sizeof(size_t));
xfree(item);
return data;
}
}