-
Notifications
You must be signed in to change notification settings - Fork 3
/
allocator.c
258 lines (224 loc) · 8.34 KB
/
allocator.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
/*
* Copyright (c) 2018, Fabian Freyer <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <allocator.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/queue.h>
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
typedef struct allocation allocation_t;
typedef TAILQ_HEAD(allocations_head, allocation) allocations_head_t;
struct allocation {
TAILQ_ENTRY(allocation) entry;
void* addr;
size_t size;
};
static allocations_head_t allocations = TAILQ_HEAD_INITIALIZER(allocations);
static void* min_alloc_address = NULL;
static void* max_alloc_address = NULL;
/*
* Attempt making an allocation, or return if the allocation would go out of
* usable memory.
*/
#define MK_ALLOCATION(var, at, size) do {\
if (!(var = _allocation(at, size))) return 0; \
} while (0)
static allocation_t*
_allocation(void* at, size_t size)
{
allocation_t *new_allocation = NULL;
/* Check that this allocation would not go out of usable memory. */
if ((at < min_alloc_address) || (at + size > max_alloc_address))
return NULL;
new_allocation = malloc(sizeof(allocation_t));
new_allocation->addr = at;
new_allocation->size = size;
return new_allocation;
}
void
init_allocator(size_t lowmem, size_t highmem)
{
/*
* This function is not reentrant. If max_alloc_address or min_alloc_address
* are already set, just return.
*/
if (min_alloc_address || max_alloc_address)
return;
/* Start allocating above the 1 MiB watermark, above the VGA hole. */
min_alloc_address = (void*) (1 * MiB);
/*
* Check whether we only have a lowmem segment, or there is also a highmem
* segment.
*
* If there is only a lowmem segment, highmem will be set to 0.
*/
if (highmem) {
max_alloc_address = (void*) (4 * GiB) + highmem;
/*
* Reserve (allocate) the MMAP hole above the lowmem segment.
* By default, this starts at 3 GiB (can be changed with
* vm_set_lowmem_limit, but bhyveload does not do this.)
*/
allocate_at((void*) lowmem, 4 * GiB - lowmem);
}
else {
max_alloc_address = (void*) lowmem;
}
}
void*
allocate(size_t size)
{
allocation_t *it = NULL, *new_allocation = NULL;
void *last_endp = min_alloc_address; /* pointer to the end of the last allocation seen */
/*
* If we do not have any allocations yet, just allocate at the first
* possible address.
*/
if (unlikely(TAILQ_EMPTY(&allocations))) {
MK_ALLOCATION(new_allocation, min_alloc_address, size);
TAILQ_INSERT_HEAD(&allocations, new_allocation, entry);
return (void*) min_alloc_address;
}
/*
* We never remove anything from the list, so we don't need to use the safe
* version here.
*/
TAILQ_FOREACH(it, &allocations, entry) {
if (last_endp && (it->addr >= last_endp + size)) {
/* We found a sufficiently large space. */
MK_ALLOCATION(new_allocation, last_endp, size);
TAILQ_INSERT_BEFORE(it, new_allocation, entry);
return last_endp;
}
last_endp = it->addr + it->size;
}
/*
* We arrived at the end of the list without finding a sufficiently
* large free space. Let's allocate one at the end now.
*/
MK_ALLOCATION(new_allocation, last_endp, size);
TAILQ_INSERT_TAIL(&allocations, new_allocation, entry);
return last_endp;
}
static inline void*
__align(void* ptr)
{
if ((uintptr_t) ptr % PAGESZ == 0)
return ptr;
return ptr - ((uintptr_t) ptr % PAGESZ) + PAGESZ;
}
void*
allocate_aligned(size_t size)
{
allocation_t *it = NULL, *new_allocation = NULL;
void *last_endp = min_alloc_address; /* pointer to the end of the last allocation seen */
/*
* If we do not have any allocations yet, just allocate at the first
* possible address.
*/
if (unlikely(TAILQ_EMPTY(&allocations))) {
MK_ALLOCATION(new_allocation, __align(min_alloc_address), size);
TAILQ_INSERT_HEAD(&allocations, new_allocation, entry);
return (void*) min_alloc_address;
}
/*
* We never remove anything from the list, so we don't need to use the safe
* version here.
*/
TAILQ_FOREACH(it, &allocations, entry) {
if (last_endp && (it->addr >= last_endp + size)) {
/* We found a sufficiently large space. */
MK_ALLOCATION(new_allocation, last_endp, size);
TAILQ_INSERT_BEFORE(it, new_allocation, entry);
return last_endp;
}
last_endp = __align(it->addr + it->size);
}
/*
* We arrived at the end of the list without finding a sufficiently
* large free space. Let's allocate one at the end now.
*/
MK_ALLOCATION(new_allocation, last_endp, size);
TAILQ_INSERT_TAIL(&allocations, new_allocation, entry);
return last_endp;
}
void*
allocate_at(void* at, size_t size)
{
allocation_t *it = NULL, *new_allocation = NULL;
/*
* If we do not have any allocations yet, just allocate the requested chunk.
*/
if (unlikely(TAILQ_EMPTY(&allocations))) {
MK_ALLOCATION(new_allocation, at, size);
TAILQ_INSERT_HEAD(&allocations, new_allocation, entry);
return at;
}
/*
* We never remove anything from the list, so we don't need to use the safe
* version here.
*/
TAILQ_FOREACH(it, &allocations, entry) {
/*
* Fastpath: skip over any allocation that lies below this allocation.
*/
if ((it->addr + it->size) <= at)
continue;
/*
* If the start address of the requested chunk is within the current
* chunk, don't allocate anything. Any other chunks cannot be before the
* requested chunk any more.
*
* Otherwise, the current chunk lies above the requested address.
*/
if ((it->addr <= at) && (at < (it->addr + it->size)))
return NULL;
/*
* Check whether the current chunk's start address would lie in the
* requested chunk.
*/
if ((at + size) > it->addr)
return NULL;
/*
* Otherwise, the request can be accomodated.
*
* If the current chunk is the first chunk, insert the allocation at the
* list head.
*/
MK_ALLOCATION(new_allocation, at, size);
if (it == TAILQ_FIRST(&allocations))
TAILQ_INSERT_HEAD(&allocations, new_allocation, entry);
else
/* The requested chunk is before the current chunk. */
TAILQ_INSERT_BEFORE(it, new_allocation, entry);
return at;
}
/* We did not find a chunk that lies after the current chunk. */
MK_ALLOCATION(new_allocation, at, size);
TAILQ_INSERT_TAIL(&allocations, new_allocation, entry);
return at;
}