-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllocator.c
More file actions
197 lines (176 loc) · 3.58 KB
/
Allocator.c
File metadata and controls
197 lines (176 loc) · 3.58 KB
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
/*
CSE 109: Spring 2018
Dylan Spector
C file for Allocator Object
Program #4
*/
#include"Allocator.h"
#include"Allocation.h"
#include<stdio.h>
#include<stdlib.h>
void makeAllocator(struct Allocator_t* it, size_t capacity)
{
// Rounds capacity up to the closest multiple of 16
if(capacity > 16)
{
capacity += 16 - (capacity % 16);
}
else if(capacity != 0)
{
capacity = 16;
}
capacity -= 16;
it->memory = (void*)malloc(capacity);
it->capacity = capacity;
it->allocationList = NULL;
it->listSize = 0;
it->listCapacity = 0;
}
void freeAllocator(struct Allocator_t* it)
{
free(it->memory);
for(size_t i = 0; i < numAllocations(it); i++)
{
free(it->allocationList[i]);
}
free(it);
return;
}
void* allocate(struct Allocator_t* it, size_t amt)
{
// If allocationList is full, expand it.
if(it->listSize == it->listCapacity)
{
// Create a newList with an expanded capacity
size_t newCapacity = (it->listCapacity)*2 + 1;
struct Allocation_t** expandedAllocationList = (struct Allocation_t**)malloc(newCapacity * sizeof(struct Allocation_t*));
for(size_t i = 0; i < it->listCapacity; i++)
{
expandedAllocationList[i] = it->allocationList[i];
}
for(size_t i = 0; i < numAllocations(it); i++)
{
free(it->allocationList[i]);
}
free(it->allocationList);
it->allocationList = expandedAllocationList;
it->listCapacity = newCapacity;
}
// Find starting location of allocation.
if(amt > 16)
{
amt += 16 - (amt % 16);
}
else if(amt != 0)
{
amt = 16;
}
if(amt > it->capacity)
{
return NULL;
}
int overlapFound = 0;
int assignedStart = -1;
for(size_t start = 0; start <= getCapacity(it)-16; start+=16)
{
for(size_t i = 0; i < it->listSize; i++)
{
if(!doesOverlap(getAllocation(it, i), start, amt))
{
continue;
}
else
{
overlapFound = 1;
break;
}
}
if(overlapFound)
{
overlapFound = 0;
}
else
{
assignedStart = start;
break;
}
}
// If space no was found, add Allocation_t to List
if(assignedStart == -1)
{
return NULL;
}
else
{
struct Allocation_t* newAllocation = (struct Allocation_t*)malloc(1* sizeof(struct Allocation_t));
makeAllocation(newAllocation, assignedStart, amt);
it->allocationList[numAllocations(it)] = newAllocation;
(it->listSize)++;
return getBase(it) + assignedStart;
}
}
void deallocate(struct Allocator_t* it, void* ptr)
{
if(ptr == NULL)
{
return;
}
for(size_t i = 0; i < it->listSize; i++)
{
struct Allocation_t* current = getAllocation(it, i);
if(ptr == getBase(it) + getStart(current))
{
freeAllocation(current);
for(size_t j = i; j < (it->listSize) - 1; j++)
{
it->allocationList[j] = it->allocationList[j+1];
}
(it->listSize)--;
return;
}
}
// If ptr was not found, throw an error
fprintf(stderr, "Corruption in Free");
exit(1);
}
void* getBase(struct Allocator_t* it)
{
return it->memory;
}
size_t getUsed(struct Allocator_t* it)
{
size_t sum = 0;
for(size_t i = 0; i < it->listSize; i++)
{
sum += getSize(getAllocation(it, i));
}
return sum;
}
size_t getCapacity(struct Allocator_t* it)
{
return it->capacity;
}
struct Allocation_t* getAllocation(struct Allocator_t* it, size_t index)
{
if(index >= 0 && index < numAllocations(it))
{
return it->allocationList[index];
}
return NULL;
}
size_t numAllocations(struct Allocator_t* it)
{
return it->listSize;
}
void* riskyAlloc(struct Allocator_t* it, size_t size)
{
if(size <= it->capacity - getUsed(it))
{
return allocate(it, size);
}
else if(getBase(it) == realloc(getBase(it), it->capacity*2))
{
return allocate(it, size);
}
return NULL;
}