-
Notifications
You must be signed in to change notification settings - Fork 0
/
kma_page.c
181 lines (147 loc) · 4.66 KB
/
kma_page.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
/***************************************************************************
* Title: Kernel Page Allocator
* -------------------------------------------------------------------------
* Purpose: Implementation of the kernel page allocator
* Author: Stefan Birrer
* Copyright: 2004 Northwestern University
***************************************************************************/
/***************************************************************************
* ChangeLog:
* -------------------------------------------------------------------------
* 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.3 2004/11/16 21:08:40 sbirrer
* - support continous pages
*
* Revision 1.2 2004/11/15 04:23:40 sbirrer
* - use constant alignment for pages
*
* Revision 1.1 2004/11/03 23:04:03 sbirrer
* - initial version for the kernel memory allocator project
*
* Revision 1.1 2004/11/03 18:34:52 sbirrer
* - initial version of the kernel memory project
*
***************************************************************************/
#define __KPAGE_IMPL__
/************System include***********************************************/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
/************Private include**********************************************/
#include "kma_page.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.
*/
/************Global Variables*********************************************/
static kma_page_stat_t kma_page_stats = { 0, 0, 0, PAGESIZE };
static void* pool = NULL;
static void* next_free_page = NULL;
/************Function Prototypes******************************************/
void* allocPage();
void freePage(void*);
void initPages();
/************External Declaration*****************************************/
/**************Implementation***********************************************/
// Returns address to a kma_page_t
kma_page_t*
get_page()
{
static int id = 0;
kma_page_t* res;
kma_page_stats.num_requested++;
kma_page_stats.num_in_use++;
res = (kma_page_t*) malloc(sizeof(kma_page_t));
res->id = id++;
res->size = kma_page_stats.page_size;
res->ptr = allocPage();
assert(res->ptr != NULL);
return res;
}
void
free_page(kma_page_t* ptr)
{
assert(ptr != NULL);
assert(ptr->ptr != NULL);
assert(kma_page_stats.num_in_use > 0);
kma_page_stats.num_freed++;
kma_page_stats.num_in_use--;
freePage(ptr->ptr);
free(ptr);
}
kma_page_stat_t*
page_stats()
{
static kma_page_stat_t stats;
return memcpy(&stats, &kma_page_stats, sizeof(kma_page_stat_t));
}
void*
allocPage()
{
void* res;
if (pool == NULL)
{
initPages();
}
res = next_free_page;
if (res == NULL)
{
error("error: all pages already allocated", "");
}
next_free_page = *((void**)next_free_page);
assert(res != NULL);
return res;
}
void
freePage(void* ptr)
{
assert(ptr != NULL);
*((void**)ptr) = next_free_page;
next_free_page = ptr;
if (kma_page_stats.num_in_use == 0)
{
free(pool);
pool = NULL;
next_free_page = NULL;
}
}
void
initPages()
{
int i;
assert(next_free_page == NULL);
assert(pool == NULL);
//pool = calloc(MAXPAGES, PAGESIZE);
int result = posix_memalign(&pool, PAGESIZE, MAXPAGES * PAGESIZE);
if(result)
error("Error using posix_memalign to allocate memory", "");
next_free_page = pool;
// use ptr to point to the next free page struct
for (i = 0; i < (MAXPAGES - 1); i++)
{
void* ptr = (pool + i * PAGESIZE);
void* next = ptr + PAGESIZE;
*((void**) ptr) = next;
}
*((void**)(pool + (MAXPAGES - 1) * PAGESIZE)) = NULL;
}