-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkma_dummy.c
108 lines (91 loc) · 3.59 KB
/
kma_dummy.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
/***************************************************************************
* Title: Kernel Memory Allocator
* -------------------------------------------------------------------------
* Purpose: Dummy kernel memory allocator
* Author: Stefan Birrer
* Version: $Revision: 1.2 $
* Last Modification: $Date: 2009/10/31 21:28:52 $
* File: $RCSfile: kma_dummy.c,v $
* Copyright: 2004 Northwestern University
***************************************************************************/
/***************************************************************************
* ChangeLog:
* -------------------------------------------------------------------------
* $Log: kma_dummy.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.5 2004/11/16 19:12:15 sbirrer
* - added base address macro
*
* Revision 1.4 2004/11/11 23:22:54 sbirrer
* - updated the dummy algorithm to use the page as the storage for the pointer
*
* Revision 1.3 2004/11/05 15:45:56 sbirrer
* - added size as a parameter to kma_free
*
* Revision 1.2 2004/11/05 13:56:01 sbirrer
* - compare request size to page size
*
* Revision 1.1 2004/11/03 23:04:03 sbirrer
* - initial version for the kernel memory allocator project
*
***************************************************************************/
#ifdef KMA_DUMMY
#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.
*/
/************Global Variables*********************************************/
/************Function Prototypes******************************************/
/************External Declaration*****************************************/
/**************Implementation***********************************************/
void* kma_malloc(kma_size_t size)
{
kpage_t* page;
// get one page
page = get_page();
// add a pointer to the page structure at the beginning of the page
*((kpage_t**)page->ptr) = page;
if ((size + sizeof(kpage_t*)) > page->size)
{ // requested size too large
free_page(page);
return NULL;
}
// check whether the BASEADDR macro works
//for (i = 0; i < page->size; i++)
//{
// assert(BASEADDR(page->ptr + i) == page->ptr);
//}
// oh yea, it worked
return page->ptr + sizeof(kpage_t*);
}
void kma_free(void* ptr, kma_size_t size)
{
kpage_t* page;
page = *((kpage_t**)(ptr - sizeof(kpage_t*)));
free_page(page);
}
#endif // KMA_DUMMY