forked from liuhaotian/kma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkma_p2fl.c
279 lines (252 loc) · 8.15 KB
/
kma_p2fl.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/***************************************************************************
* Title: Kernel Memory Allocator
* -------------------------------------------------------------------------
* Purpose: Kernel memory allocator based on the power-of-two free list
* algorithm
* Author: Stefan Birrer
* Version: $Revision: 1.2 $
* Last Modification: $Date: 2009/10/31 21:28:52 $
* File: $RCSfile: kma_p2fl.c,v $
* Copyright: 2004 Northwestern University
***************************************************************************/
/***************************************************************************
* ChangeLog:
* -------------------------------------------------------------------------
* $Log: kma_p2fl.c,v $
* Revision 1.2 2009/10/31 21:28:52 jot836
* This is the current version of KMA project 3.
* It includes:
* - the most up-to-date handout (F'09)
* - updated skeleton including
* file-driven test harness,
* trace generator script,
* support for evaluating efficiency of algorithm (wasted memory),
* gnuplot support for plotting allocation and waste,
* set of traces for all students to use (including a makefile and README of the settings),
* - different version of the testsuite for use on the submission site, including:
* scoreboard Python scripts, which posts the top 5 scores on the course webpage
*
* Revision 1.1 2005/10/24 16:07:09 sbirrer
* - skeleton
*
* Revision 1.2 2004/11/05 15:45:56 sbirrer
* - added size as a parameter to kma_free
*
* Revision 1.1 2004/11/03 23:04:03 sbirrer
* - initial version for the kernel memory allocator project
*
***************************************************************************/
#ifdef KMA_P2FL
#define __KMA_IMPL__
/************System include***********************************************/
#include <assert.h>
#include <stdlib.h>
/************Private include**********************************************/
#include "kpage.h"
#include "kma.h"
/************Defines and Typedefs*****************************************/
/* #defines and typedefs should have their names in all caps.
* Global variables begin with g. Global constants with k. Local
* variables should be in all lower case. When initializing
* structures and arrays, line everything up in neat columns.
*/
typedef struct
{
void* nextbuffer;
} kbuffer_t;
typedef struct
{
int size;
kbuffer_t* buffer;
} kfreelist_t;
typedef struct
{
void* itself;
int numpages;//from 0 to max, the 1st one is 0
int numalloc;// 0 means nothing//each page hold one , if 0 then free
void* freememory;
kfreelist_t p2fl[10];
} klistheader_t;
/************Global Variables*********************************************/
kpage_t* gentryptr=0;
/************Function Prototypes******************************************/
kma_size_t roundup(kma_size_t size);
int chkfreespace(kma_size_t size);// 0:not enough, >1: enough space
int chkfreelist(kma_size_t size);// find it in list
kpage_t* initial(kpage_t* page);
void* buffermalloc(klistheader_t* page,kma_size_t size);
/************External Declaration*****************************************/
/**************Implementation***********************************************/
void*
kma_malloc(kma_size_t size)
{
if ((size + sizeof(void*)) > PAGESIZE){ // requested size too large
return NULL;
}
int i;
void* ret;
klistheader_t* mainpage;
klistheader_t* temppage;
if(!gentryptr){// initialized the entry
gentryptr=initial(get_page());
}
mainpage=(klistheader_t*)(gentryptr->ptr);
if((i=chkfreelist(size)))
{
i--;
int listindex=4;
while((1<<listindex)!=roundup(size))
{
listindex++;
}
listindex=listindex-4;
temppage=(klistheader_t*)((long int)mainpage + i * PAGESIZE);
ret=(void*)(((*temppage).p2fl[listindex]).buffer);
((*temppage).p2fl[i]).buffer=(kbuffer_t*)((*((kbuffer_t*)ret)).nextbuffer);
(*temppage).numalloc++;// current page's number of malloc
(*mainpage).numalloc++;// global number of malloc
return ret;
}
else if((i=chkfreespace(size)))
{
i--;
temppage=(klistheader_t*)((long int)mainpage + i * PAGESIZE);
ret=buffermalloc(temppage,size);// already incease the (*temppage).numalloc++;
return ret;
}
else{
initial(get_page());
(*mainpage).numpages++;
i=(*mainpage).numpages;
temppage=(klistheader_t*)((long int)mainpage + i * PAGESIZE);
ret=buffermalloc(temppage,size);// already incease the (*temppage).numalloc++;
return ret;
}
// find in list
// find for free space
// create new pages
/*
i=roundup(size);
i=chkfreespace(size);
ret=buffermalloc(page,size);
return ret;
*/
return NULL;
}
void
kma_free(void* ptr, kma_size_t size)// when delete, we add the buffer to the gentrylist
{
kbuffer_t* buffer;
void* nextbuffer;
kfreelist_t* freelist;
buffer = (kbuffer_t*)((long int)ptr-sizeof(kbuffer_t*));
freelist = (kfreelist_t*)((*buffer).nextbuffer);
nextbuffer=((*freelist).buffer);
(*buffer).nextbuffer=nextbuffer;
klistheader_t* mainpage=(klistheader_t*)(gentryptr->ptr);
klistheader_t* temppage;
int i;
i=((long int)ptr-(long int)mainpage)/PAGESIZE;
temppage=(klistheader_t*)((long int)mainpage + i * PAGESIZE);
(*temppage).numalloc--;
(*mainpage).numalloc--;
if((*temppage).numalloc==0){
kpage_t* page0;
/* for(; (*mainpage).numpages >=0; (*mainpage).numpages--)
{
temppage = *((kpage_t**)((long int)mainpage + (*mainpage).numpages * PAGESIZE));
free_page(temppage);
}*/
page0 = *(kpage_t**)(temppage);
initial(page0);// it is no use, but we must keep all the pages sequential, so just initialize it
//free_page(page0);
//(*mainpage).numpages is -1 no
//gentryptr=0;//all pages are free
}
if((*mainpage).numalloc==0){// there is nothing left, we must free all
kpage_t* page1;
page1 = *(kpage_t**)(mainpage);
for(; (*mainpage).numpages >0; (*mainpage).numpages--)
{
page1 = *((kpage_t**)((long int)mainpage + (*mainpage).numpages * PAGESIZE));
free_page(page1);
}
page1 = *((kpage_t**)((long int)mainpage + (*mainpage).numpages * PAGESIZE));
free_page(page1);
//(*mainpage).numpages is -1 no
gentryptr=0;//all pages are free
}
//free_page(page);
//we need to clean the freelist when there is nothing left
}
kma_size_t roundup(kma_size_t size){
kma_size_t ret=16;
while(size>ret){
ret=ret<<1;
}
return ret;
}
kpage_t* initial(kpage_t* page){
klistheader_t* listheader;
*((kpage_t**)page->ptr) = page;
int i;
listheader=(klistheader_t*)(page->ptr);
for(i = 0; i < 10; ++i)
{
((*listheader).p2fl[i]).buffer=0;
((*listheader).p2fl[i]).size=1<<(i+4);
}
(*listheader).numpages=0;
(*listheader).numalloc=0;
(*listheader).freememory=(void*)((long int)listheader+sizeof(klistheader_t));// the point to indicate that from this point, we have free space
return page;
}
int chkfreespace(kma_size_t size){
klistheader_t* mainpage=(klistheader_t*)(gentryptr->ptr);
klistheader_t* temppage;
int i;
for(i=0; i<=(*mainpage).numpages; i++)
{
temppage = (klistheader_t*)((long int)mainpage + i * PAGESIZE);
if((PAGESIZE+(long int)temppage-(long int)((*temppage).freememory))/(size+sizeof(kbuffer_t)))return i+1;// if there is free space left
}
return 0;
}
int chkfreelist(kma_size_t size){// if there is a free buffer in the free list, we just use it
klistheader_t* mainpage=(klistheader_t*)(gentryptr->ptr);
klistheader_t* temppage;
int i;
int listindex=4;
while((1<<listindex)!=roundup(size))
{
listindex++;
}
listindex=listindex-4;
for(i = 0; i <= (*mainpage).numpages; ++i)
{
temppage=(klistheader_t*)((long int)mainpage+i*PAGESIZE);
if(((*temppage).p2fl[listindex]).buffer!=0)return i+1;
}
return 0;
}
void* buffermalloc(klistheader_t* page,kma_size_t size){//point to the main list
int i=4;
void* ret;
klistheader_t* mainpage=(klistheader_t*)(gentryptr->ptr);
kbuffer_t* buffer;
kfreelist_t* freelist;
while((1<<i)!=roundup(size))
{
i++;
}
i=i-4;
buffer=(kbuffer_t*)((*page).freememory);
freelist=(kfreelist_t*)&((*mainpage).p2fl[i]);
ret=(void*)buffer+sizeof(kbuffer_t);
(*buffer).nextbuffer=(void*)freelist;//point the list it belonging to
(*page).freememory=(void*)buffer+size+sizeof(kbuffer_t);
(*page).numalloc++;
(*mainpage).numalloc++;
return ret;
}
#endif // KMA_P2FL