-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathblocAlloc.cpp
182 lines (158 loc) · 4.83 KB
/
blocAlloc.cpp
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
/* This file is part of the uFLIP software. See www.uflip.org
uFLIP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
uFLIP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with uFLIP. If not, see <http://www.gnu.org/licenses/>.
uFLIP was initially developed based on SQLIO2 by Leonard Chung although almost all SQLIO2
code have disappeared. ([email protected] - see
http://research.microsoft.com/en-us/um/siliconvalley/projects/sequentialio/ )
uFLIP also includes some lines from the pseudo random generator from Agner Fog
(see http://www.agner.org/random/)
©Luc Bouganim - 2008-2009
*/
#include "blocAlloc.h"
#include "utility.h"
void printMem(item * list){
item* curr;
printf("===================\n");
curr = list;
while (curr!= NULL) {
printf("%d %d %d (%d %d %d)\n", curr->inf/100000, curr->sup/100000,curr->sup/100000 - curr->inf/100000, curr->inf, curr->sup, curr->sup - curr->inf);
curr = curr->next;
}
printf("===================\n\n");
}
void CheckMem(item * list){
item* curr;
//printMem (list);
curr = list;
while (curr!= NULL) {
if ((curr->inf % BLOCK) != 0){
printMem (list);
printf ("Memory is corrupted (not more aligned) \n");
exit(1);
}
curr = curr->next;
}
}
item* InitMemList(int32 size) {
item * curr;
curr = (item *)malloc(sizeof(item));
if (curr == NULL) HandleError("initList", "allocation pbm !", 0, ERR_ABORT);
curr->inf = 0;
curr->sup = size;
curr->prev = NULL;
curr->next = NULL;
return (curr);
}
int32 MemMinAddress(item* list) {
CheckMem(list);
return (list->inf);
}
int32 MemSearch(item* list, int32 size) {
item* curr;
item* min;
int32 minSize = INT32_MAX;
CheckMem(list);
if ((size % BLOCK) != 0) size = ((int32)(size/BLOCK)) * BLOCK + BLOCK;
// Find smallest space where size fits
curr = list;
while (curr!= NULL) {
if ((curr->sup - curr->inf >= size) && (curr->sup - curr->inf < minSize)) {
minSize = curr->sup - curr->inf;
min = curr;
}
curr = curr->next;
}
// allocate
if (minSize == INT32_MAX) return(INT32_MAX);
min->inf += size;
//printf("[%d]\n",min->inf - size );
CheckMem(list);
return (min->inf - size);
}
int32 MemAllocNearestAfterA(item *list, int32 A, int32 size) {
item* curr;
int32 I, S;
//printf("A: %d, size: %d\n", A,size);
CheckMem(list);
if ((size % BLOCK) != 0) size = ((int32)(size/BLOCK)) * BLOCK + BLOCK;
if ((A % BLOCK) != 0) A = ((int32)(A/BLOCK)) * BLOCK + BLOCK;
// Find the first item after A or containing A
curr = list;
while ((curr!= NULL) && (curr->inf <= A) && (curr->sup <A)) curr = curr->next;
// Find first space where size fits
I = curr->inf;
S = curr->sup;
while ((curr!= NULL) && !(((I<=A) && (S>A) && (S-A >= size))) && !(((I>A) && (S-I > size)))) {
curr = curr->next;
if (curr != NULL) {
I = curr->inf;
S = curr->sup;
}
}
if (curr == NULL) return(INT32_MAX);
if ((I<A) && (S>A) && (S-A >= size)) {
item * newI;
//scinder
newI = (item *)malloc(sizeof(item));
if (newI == NULL) HandleError("MemAllocNearest", "allocation pbm !", 0, ERR_ABORT);
newI->inf = A + size;
newI->sup = S;
curr->sup = A;
newI->next = curr->next;
newI->prev = curr;
curr->next = newI;
//printMem(list);
CheckMem(list);
return (A);
}
if ((I >= A) && (S-I > size)) {
curr->inf += size;
//printMem(list);
CheckMem(list);
return(I);
}
CheckMem(list);
return(INT32_MAX);
}
int32 MemAlloc(item *list, int32 A, int32 B) {
item* curr;
int32 I, S;
//printf("entering MemAlloc %d %d \n", A, B);
// Find the item containing A-B
CheckMem(list);
curr = list;
while ((curr!= NULL) && ((curr->inf >A) || (curr->sup < B))) curr = curr->next;
if (curr == NULL) return(INT32_MAX);
I = curr->inf;
S = curr->sup;
if ((I < A) && (B <= S)) {
item * newI;
//scinder
newI = (item *)malloc(sizeof(item));
if (newI == NULL) HandleError("MemAlloc", "allocation pbm !", 0, ERR_ABORT);
newI->inf = B;
newI->sup = S;
curr->sup = A;
newI->next = curr->next;
newI->prev = curr;
curr->next = newI;
//printMem(list);
CheckMem(list);
return (A);
}
else if (I==A) {
curr->inf = B;
CheckMem(list);
return A;
}
CheckMem(list);
return(INT32_MAX);
}