-
Notifications
You must be signed in to change notification settings - Fork 0
/
half_fit.c
275 lines (234 loc) · 8.41 KB
/
half_fit.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
#include <stdio.h>
#include <stdint.h>
#include <math.h>
const uint32_t sizeArray = 8192;//8192x4=32768
//Declare Bucket With Size: 32768 Bytes
uint32_t memory[8192] = {2048, 8192+10, 8192+10}; //0b100000 = 32
//Declare an array to contain all the head pointers for each bucket
static int buckets[11] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0};
//buckets[0] 32 to 64 Bytes
//buckets[1] 64 to 127 Bytes
//buckets[2] 128 to 255 Bytes
//buckets[3] 256 to 511 Bytes
//buckets[4] 512 to 1023 Bytes
//buckets[5] 1024 to 2047 Bytes
//buckets[6] 2048 to 4095 Bytes
//buckets[7] 4096 to 8191 Bytes
//buckets[8] 8192 to 16383 Bytes
//buckets[9] 16384 to 32787 Bytes
//buckets[10] 32768 to whatever Bytes
//defines for reading bitfields
#define previousRead(ptr) ((4290772992 & ptr) >> 19) //0b11111111110000000000000000000000 = 4290772992, shift left by 22 and right by 17, 4 Byte addressable
#define nextRead(ptr) ((4190208 & ptr) >> 9) //0b1111111111000000000000 = 4190208, shift left by 12 and right by 5, 4 Byte addressable
#define sizeBlockRead(ptr) ((4094 & ptr) << 4) //0b111111111110 = 4094
#define allocatedRead(ptr) (ptr & 1)
//defines for righting bitfields
#define previousWrite(ptr, value) ptr = (value << 19) + (4194303 & ptr) //0b1111111111111111111111 = 4194303
#define nextWrite(ptr, value) ptr = (value << 9) + (4290777087 & ptr) //0b11111111110000000000111111111111 = 4290777087
#define sizeBlockWrite(ptr, value) ptr = (value >> 4) + (4294963201 & ptr) //0b11111111111111111111000000000001 = 4294963201
//#define allocate(ptr) ptr = ptr & 4294967295 //0b11111111111111111111111111111111 = 4294967295
#define allocate(ptr) ptr = ptr | 1 //0b11111111111111111111111111111111 = 4294967295
#define unallocate(ptr) ptr = ptr & 4294967294 //0b11111111111111111111111111111110 = 4294967294
void half_init(){
int i;
//Declare Bucket With Size: 32768 Bytes
memory[0] = 2048;
memory[1] = 8202;
memory[2] = 8202; //0b100000 = 32
//Declare an array to contain all the head pointers for each bucket
for (i = 0; i<10; i++)
buckets[i] = -1;
buckets[10] = 0;
return;
}
void removeFromBucket(uint32_t location){
if (memory[location+1] >= sizeArray && memory[location+2] >= sizeArray){
buckets[memory[location+1]-sizeArray] = -1; //sizeArray + 10
}
else if (memory[location+1] >= sizeArray){
buckets[memory[location+1]-sizeArray] = memory[location+2];
memory[memory[location+2]+1] = memory[location+1];
}
else if (memory[location+2] >= sizeArray){
memory[memory[location+1]+2] = memory[location+2];
}
else{
memory[memory[location+1]+2] = memory[location+2];
memory[memory[location+2]+1] = memory[location+1];
}
}
void addToBucket(uint32_t size, uint32_t location) {
uint32_t i = 0;
size = size >> 5;
while(size > 0){
i++;
size = size >> 1;
}
//printf("i: %u\n", i );
i--;
//printf("i: %u\n", i );
if (buckets[i] < 0){
memory[location+2] = sizeArray+i;
}
else{
memory[location+2] = buckets[i];
memory[buckets[i]+1] = location;
}
memory[location+1] = sizeArray+i;
buckets[i] = location;
}
//Declare Half_Alloc Function
void *half_alloc(unsigned int n){
uint32_t i = 0;
uint32_t differnce;
uint32_t size = n+4;
uint32_t temp = size;
uint32_t GP;
uint32_t mod = 0;
//printf("Test n: %u\n", n);
if (n > 32763)
return NULL;
//smallest bucket size is 32 so start there
temp = temp >> 5;
//find the power of two (or bucket size in which size is less then
//printf("testpoint\n");
while(temp > 0){
//If all the buckets are empty return null
if (i == 10)
return NULL;
i++;
temp = temp >> 1;
}
//printf("testpoint\n");
//printf("i: %u\n", i);
//for (int j=0; j<11; j++)
// printf("j: %d buckets[i]: %d size: %u\n",j, buckets[j], sizeBlockRead(memory[buckets[j]]));
//printf("after for loop\n");
//If larger bucket is empty keep going larger till you find a non empty bucket
while(buckets[i] == -1){
if (i == 10)
return NULL;
i++;
}
//printf("testpoint\n");
//Take the first block of the top of the bucket larger the the size to be allocated
// Check to see if amount of unallocated memory left after allocation has occurred is greater then 8 bytes (64 bits) (smallest appropriate block size)
temp = sizeBlockRead(memory[buckets[i]]);
mod = size%32;
if (mod != 0){
differnce = 32-mod;
size += differnce;
}
//printf("size: %u temp: %u\n", size, temp);
if(temp - size == 0){
temp = buckets[i];
removeFromBucket(temp);
return (void *)&memory[temp+1];
}
//printf("Allocating %u\n", size);
GP = buckets[i] + size/4;
//Update size of old header
sizeBlockWrite(memory[buckets[i]], size);
//Create new header
mod = nextRead(memory[buckets[i]]);
if (mod == buckets[i]){
nextWrite(memory[GP], GP);
}
else{
nextWrite(memory[GP], nextRead(memory[buckets[i]]));
}
previousWrite(memory[GP], buckets[i]);
size = temp - size;
sizeBlockWrite(memory[GP], size);
//Update the next pointer of the old header
nextWrite(memory[buckets[i]], GP);
unallocate(memory[GP]);
//Update the linked list, removing block to allocate
temp = buckets[i];
//Allocate the block
allocate(memory[temp]);
//Add the new header to linked list
//printf("before adds and removes\n");
removeFromBucket(temp);
//printf("after remove\n");
addToBucket(size, GP);
//printf("after removes and adds\n");
//printf("Memory Address %u\n",&memory[temp+1] );
return &memory[temp+1];
}
// Declare Half_Free Function
void half_free(void *mem_free){
uint32_t *mem_f = (uint32_t *) mem_free;
uint32_t next, previous, newSize, size, location;
uint8_t nextA, previousA;
if (mem_free == NULL)
return;
//printf("%u", *mem_f);
--mem_f;
//printf("%u", *mem_f);
size = sizeBlockRead(*mem_f);
previous = previousRead(*mem_f);
next = nextRead(*mem_f);
if (&memory[previous] == mem_f && &memory[next] == mem_f){
location = 0;
nextA = 1;
previousA = 1;
}
else if (&memory[previous] == mem_f){
location = previousRead(memory[next]);
nextA = allocatedRead(memory[next]);
previousA = 1;
}
else if (&memory[next] == mem_f){
location = nextRead(memory[previous]);
nextA = 1;
previousA = allocatedRead(memory[previous]);
}
else{
location = nextRead(memory[previous]);
nextA = allocatedRead(memory[next]);
previousA = allocatedRead(memory[previous]);
}
//location = previousRead(memory[next]);
//check for coalesce with next and previous
if (!nextA && !previousA){
//printf("coalesce both\n");
newSize = sizeBlockRead(memory[previous])+size+sizeBlockRead(memory[next]);
previousWrite(memory[nextRead(memory[next])], previous);
nextWrite(memory[previous], nextRead(memory[next]));
sizeBlockWrite(memory[previous], newSize);
//removeFromBucket(location);
removeFromBucket(previous);
removeFromBucket(next);
unallocate(memory[previous]);
addToBucket(newSize, previous);
}
//check for coalesce with next only
else if (!nextA){
//printf("coalesce next\n");
newSize = size+sizeBlockRead(memory[next]);
previousWrite(memory[nextRead(memory[next])], location);
nextWrite(*mem_f, nextRead(memory[next]));
sizeBlockWrite(*mem_f, newSize);
//removeFromBucket(location);
removeFromBucket(next);
unallocate(*mem_f);
addToBucket(newSize, location);
}
else if (!previousA){
//printf("Coalesce Previous\n");
newSize = size+sizeBlockRead(memory[previous]);
previousWrite(memory[next], previous);
nextWrite(memory[previous], next);
sizeBlockWrite(memory[previous], newSize);
//removeFromBucket(location);
removeFromBucket(previous);
unallocate(memory[previous]);
addToBucket(newSize, previous);
}
else{
//printf("Coalesce Nothing\n");
unallocate(*mem_f);
addToBucket(size, location);
}
}