source: mainline/kernel/generic/src/lib/ra.c@ 0fd9b35

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0fd9b35 was c24b272f, checked in by Jakub Jermar <jakub@…>, 14 years ago

Fix two off-by-one errors.

  • Property mode set to 100644
File size: 10.6 KB
Line 
1/*
2 * Copyright (c) 2011 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup generic
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Resource allocator.
36 *
37 * This is a generic resource allocator, loosely based on the ideas presented
38 * in chapter 4 of the following paper and further simplified:
39 *
40 * Bonwick J., Adams J.: Magazines and Vmem: Extending the Slab Allocator to
41 * Many CPUs and Arbitrary Resources, USENIX 2001
42 *
43 */
44
45#include <lib/ra.h>
46#include <typedefs.h>
47#include <mm/slab.h>
48#include <bitops.h>
49#include <debug.h>
50#include <panic.h>
51#include <adt/list.h>
52#include <adt/hash_table.h>
53#include <align.h>
54#include <macros.h>
55#include <synch/spinlock.h>
56
57#define USED_BUCKETS 1024
58
59static size_t used_hash(sysarg_t *key)
60{
61 return ((*key >> 2) & (USED_BUCKETS - 1));
62}
63
64static bool used_compare(sysarg_t *key, size_t keys, link_t *item)
65{
66 ra_segment_t *seg;
67
68 seg = hash_table_get_instance(item, ra_segment_t, fu_link);
69 return seg->base == *key;
70}
71
72static hash_table_operations_t used_ops = {
73 .hash = used_hash,
74 .compare = used_compare,
75 .remove_callback = NULL,
76};
77
78/** Calculate the segment size. */
79static size_t ra_segment_size_get(ra_segment_t *seg)
80{
81 ra_segment_t *nextseg;
82
83 nextseg = list_get_instance(seg->segment_link.next, ra_segment_t,
84 segment_link);
85 return nextseg->base - seg->base;
86}
87
88static ra_segment_t *ra_segment_create(uintptr_t base)
89{
90 ra_segment_t *seg;
91
92 seg = (ra_segment_t *) malloc(sizeof(ra_segment_t), FRAME_ATOMIC);
93 if (!seg)
94 return NULL;
95
96 link_initialize(&seg->segment_link);
97 link_initialize(&seg->fu_link);
98
99 seg->base = base;
100 seg->flags = 0;
101
102 return seg;
103}
104
105static void ra_segment_destroy(ra_segment_t *seg)
106{
107 free(seg);
108}
109
110static ra_span_t *ra_span_create(uintptr_t base, size_t size)
111{
112 ra_span_t *span;
113 ra_segment_t *seg, *lastseg;
114 unsigned int i;
115
116 span = (ra_span_t *) malloc(sizeof(ra_span_t), FRAME_ATOMIC);
117 if (!span)
118 return NULL;
119
120 span->max_order = fnzb(size);
121 span->base = base;
122 span->size = size;
123
124 span->free = (list_t *) malloc((span->max_order + 1) * sizeof(list_t),
125 FRAME_ATOMIC);
126 if (!span->free) {
127 free(span);
128 return NULL;
129 }
130
131 /*
132 * Create a segment to represent the entire size of the span.
133 */
134 seg = ra_segment_create(base);
135 if (!seg) {
136 free(span->free);
137 free(span);
138 return NULL;
139 }
140 seg->flags = RA_SEGMENT_FREE;
141
142 /*
143 * The last segment will be used as a sentinel at the end of the
144 * segment list so that it is possible to calculate the size for
145 * all other segments. It will not be placed in any free list or
146 * in the used segment hash and adjacent segments will not be
147 * coalesced with it.
148 */
149 lastseg = ra_segment_create(base + size);
150 if (!lastseg) {
151 ra_segment_destroy(seg);
152 free(span->free);
153 free(span);
154 return NULL;
155 }
156
157 link_initialize(&span->span_link);
158 list_initialize(&span->segments);
159
160 hash_table_create(&span->used, USED_BUCKETS, 1, &used_ops);
161
162 for (i = 0; i <= span->max_order; i++)
163 list_initialize(&span->free[i]);
164
165 /* Insert the first segment into the list of segments. */
166 list_append(&seg->segment_link, &span->segments);
167 /* Insert the last segment into the list of segments. */
168 list_append(&lastseg->segment_link, &span->segments);
169
170 /* Insert the first segment into the respective free list. */
171 list_append(&seg->fu_link, &span->free[span->max_order]);
172
173 return span;
174}
175
176/** Create an empty arena. */
177ra_arena_t *ra_arena_create(void)
178{
179 ra_arena_t *arena;
180
181 arena = (ra_arena_t *) malloc(sizeof(ra_arena_t), FRAME_ATOMIC);
182 if (!arena)
183 return NULL;
184
185 spinlock_initialize(&arena->lock, "arena_lock");
186 list_initialize(&arena->spans);
187
188 return arena;
189}
190
191/** Add a span to arena. */
192bool ra_span_add(ra_arena_t *arena, uintptr_t base, size_t size)
193{
194 ra_span_t *span;
195
196 /*
197 * At the moment, we can only create resources that don't include 0.
198 * If 0 needs to be considered as a valid resource, we would need to
199 * slightly change the API of the resource allocator.
200 */
201 if (base == 0)
202 return false;
203
204 span = ra_span_create(base, size);
205 if (!span)
206 return false;
207
208 /* TODO: check for overlaps */
209 spinlock_lock(&arena->lock);
210 list_append(&span->span_link, &arena->spans);
211 spinlock_unlock(&arena->lock);
212 return true;
213}
214
215static uintptr_t ra_span_alloc(ra_span_t *span, size_t size, size_t align)
216{
217 /*
218 * We need to add the maximum of align - 1 to be able to compensate for
219 * the worst case unaligned segment.
220 */
221 size_t needed = size + align - 1;
222 size_t order = ispwr2(needed) ? fnzb(needed) : fnzb(needed) + 1;
223 ra_segment_t *pred = NULL;
224 ra_segment_t *succ = NULL;
225
226 /*
227 * Find the free list of the smallest order which can satisfy this
228 * request.
229 */
230 for (; order <= span->max_order; order++) {
231 ra_segment_t *seg;
232 uintptr_t newbase;
233
234 if (list_empty(&span->free[order]))
235 continue;
236
237 /* Take the first segment from the free list. */
238 seg = list_get_instance(list_first(&span->free[order]),
239 ra_segment_t, fu_link);
240
241 ASSERT(seg->flags & RA_SEGMENT_FREE);
242
243 /*
244 * See if we need to allocate new segments for the chopped-off
245 * parts of this segment.
246 */
247 if (!IS_ALIGNED(seg->base, align)) {
248 pred = ra_segment_create(seg->base);
249 if (!pred) {
250 /*
251 * Fail as we are unable to split the segment.
252 */
253 break;
254 }
255 pred->flags |= RA_SEGMENT_FREE;
256 }
257 newbase = ALIGN_UP(seg->base, align);
258 if (newbase + size != seg->base + ra_segment_size_get(seg)) {
259 ASSERT(newbase + (size - 1) < seg->base +
260 (ra_segment_size_get(seg) - 1));
261 succ = ra_segment_create(newbase + size);
262 if (!succ) {
263 if (pred)
264 ra_segment_destroy(pred);
265 /*
266 * Fail as we are unable to split the segment.
267 */
268 break;
269 }
270 succ->flags |= RA_SEGMENT_FREE;
271 }
272
273
274 /* Put unneeded parts back. */
275 if (pred) {
276 size_t pred_order;
277
278 list_insert_before(&pred->segment_link,
279 &seg->segment_link);
280 pred_order = fnzb(ra_segment_size_get(pred));
281 list_append(&pred->fu_link, &span->free[pred_order]);
282 }
283 if (succ) {
284 size_t succ_order;
285
286 list_insert_after(&succ->segment_link,
287 &seg->segment_link);
288 succ_order = fnzb(ra_segment_size_get(succ));
289 list_append(&succ->fu_link, &span->free[succ_order]);
290 }
291
292 /* Now remove the found segment from the free list. */
293 list_remove(&seg->fu_link);
294 seg->base = newbase;
295 seg->flags &= ~RA_SEGMENT_FREE;
296
297 /* Hash-in the segment into the used hash. */
298 sysarg_t key = seg->base;
299 hash_table_insert(&span->used, &key, &seg->fu_link);
300
301 return newbase;
302 }
303
304 return 0;
305}
306
307static void ra_span_free(ra_span_t *span, size_t base, size_t size)
308{
309 sysarg_t key = base;
310 link_t *link;
311 ra_segment_t *seg;
312 ra_segment_t *pred;
313 ra_segment_t *succ;
314 size_t order;
315
316 /*
317 * Locate the segment in the used hash table.
318 */
319 link = hash_table_find(&span->used, &key);
320 if (!link) {
321 panic("Freeing segment which is not known to be used (base=%"
322 PRIxn ", size=%" PRIdn ").", base, size);
323 }
324 seg = hash_table_get_instance(link, ra_segment_t, fu_link);
325
326 /*
327 * Hash out the segment.
328 */
329 hash_table_remove(&span->used, &key, 1);
330
331 ASSERT(!(seg->flags & RA_SEGMENT_FREE));
332 ASSERT(seg->base == base);
333 ASSERT(ra_segment_size_get(seg) == size);
334
335 /*
336 * Check whether the segment can be coalesced with its left neighbor.
337 */
338 if (list_first(&span->segments) != &seg->segment_link) {
339 pred = hash_table_get_instance(seg->segment_link.prev,
340 ra_segment_t, segment_link);
341
342 ASSERT(pred->base < seg->base);
343
344 if (pred->flags & RA_SEGMENT_FREE) {
345 /*
346 * The segment can be coalesced with its predecessor.
347 * Remove the predecessor from the free and segment
348 * lists, rebase the segment and throw the predecessor
349 * away.
350 */
351 list_remove(&pred->fu_link);
352 list_remove(&pred->segment_link);
353 seg->base = pred->base;
354 ra_segment_destroy(pred);
355 }
356 }
357
358 /*
359 * Check whether the segment can be coalesced with its right neighbor.
360 */
361 succ = hash_table_get_instance(seg->segment_link.next, ra_segment_t,
362 segment_link);
363 ASSERT(succ->base > seg->base);
364 if (succ->flags & RA_SEGMENT_FREE) {
365 /*
366 * The segment can be coalesced with its successor.
367 * Remove the successor from the free and segment lists
368 * and throw it away.
369 */
370 list_remove(&succ->fu_link);
371 list_remove(&succ->segment_link);
372 ra_segment_destroy(succ);
373 }
374
375 /* Put the segment on the appropriate free list. */
376 seg->flags |= RA_SEGMENT_FREE;
377 order = fnzb(ra_segment_size_get(seg));
378 list_append(&seg->fu_link, &span->free[order]);
379}
380
381/** Allocate resources from arena. */
382uintptr_t ra_alloc(ra_arena_t *arena, size_t size, size_t alignment)
383{
384 uintptr_t base = 0;
385
386 ASSERT(size >= 1);
387 ASSERT(alignment >= 1);
388 ASSERT(ispwr2(alignment));
389
390 spinlock_lock(&arena->lock);
391 list_foreach(arena->spans, cur) {
392 ra_span_t *span = list_get_instance(cur, ra_span_t, span_link);
393
394 base = ra_span_alloc(span, size, alignment);
395 if (base)
396 break;
397 }
398 spinlock_unlock(&arena->lock);
399
400 return base;
401}
402
403/* Return resources to arena. */
404void ra_free(ra_arena_t *arena, uintptr_t base, size_t size)
405{
406 spinlock_lock(&arena->lock);
407 list_foreach(arena->spans, cur) {
408 ra_span_t *span = list_get_instance(cur, ra_span_t, span_link);
409
410 if (iswithin(span->base, span->size, base, size)) {
411 ra_span_free(span, base, size);
412 spinlock_unlock(&arena->lock);
413 return;
414 }
415 }
416 spinlock_unlock(&arena->lock);
417
418 panic("Freeing to wrong arena (base=%" PRIxn ", size=%" PRIdn ").",
419 base, size);
420}
421
422
423/** @}
424 */
Note: See TracBrowser for help on using the repository browser.