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 |
|
---|
51 | static hash_table_operations_t used_ops = {
|
---|
52 | .hash = NULL,
|
---|
53 | .compare = NULL,
|
---|
54 | .remove_callback = NULL,
|
---|
55 | };
|
---|
56 |
|
---|
57 | static ra_segment_t *ra_segment_create(uintptr_t base, size_t size)
|
---|
58 | {
|
---|
59 | ra_segment_t *seg;
|
---|
60 |
|
---|
61 | seg = (ra_segment_t *) malloc(sizeof(ra_segment_t), FRAME_ATOMIC);
|
---|
62 | if (!seg)
|
---|
63 | return NULL;
|
---|
64 |
|
---|
65 | link_initialize(&seg->segment_link);
|
---|
66 | link_initialize(&seg->free_link);
|
---|
67 | link_initialize(&seg->used_link);
|
---|
68 |
|
---|
69 | seg->base = base;
|
---|
70 | seg->size = size;
|
---|
71 |
|
---|
72 | return seg;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /*
|
---|
76 | static void ra_segment_destroy(ra_segment_t *seg)
|
---|
77 | {
|
---|
78 | free(seg);
|
---|
79 | }
|
---|
80 | */
|
---|
81 |
|
---|
82 | static ra_span_t *ra_span_create(uintptr_t base, size_t size)
|
---|
83 | {
|
---|
84 | ra_span_t *span;
|
---|
85 | ra_segment_t *seg;
|
---|
86 | unsigned int i;
|
---|
87 |
|
---|
88 | span = (ra_span_t *) malloc(sizeof(ra_span_t), FRAME_ATOMIC);
|
---|
89 | if (!span)
|
---|
90 | return NULL;
|
---|
91 |
|
---|
92 | span->max_order = fnzb(size);
|
---|
93 |
|
---|
94 | span->free = (list_t *) malloc((span->max_order + 1) * sizeof(list_t),
|
---|
95 | FRAME_ATOMIC);
|
---|
96 | if (!span->free) {
|
---|
97 | free(span);
|
---|
98 | return NULL;
|
---|
99 | }
|
---|
100 | seg = ra_segment_create(base, size);
|
---|
101 | if (!seg) {
|
---|
102 | free(span->free);
|
---|
103 | free(span);
|
---|
104 | return NULL;
|
---|
105 | }
|
---|
106 |
|
---|
107 | link_initialize(&span->span_link);
|
---|
108 | list_initialize(&span->segments);
|
---|
109 |
|
---|
110 | hash_table_create(&span->used, span->max_order + 1, 1, &used_ops);
|
---|
111 |
|
---|
112 | for (i = 0; i < span->max_order; i++)
|
---|
113 | list_initialize(&span->free[i]);
|
---|
114 |
|
---|
115 | list_append(&seg->segment_link, &span->segments);
|
---|
116 | list_append(&seg->free_link, &span->free[span->max_order]);
|
---|
117 |
|
---|
118 | return span;
|
---|
119 | }
|
---|
120 |
|
---|
121 | ra_arena_t *ra_arena_create(uintptr_t base, size_t size)
|
---|
122 | {
|
---|
123 | ra_arena_t *arena;
|
---|
124 | ra_span_t *span;
|
---|
125 |
|
---|
126 | arena = (ra_arena_t *) malloc(sizeof(ra_arena_t), FRAME_ATOMIC);
|
---|
127 | if (!arena)
|
---|
128 | return NULL;
|
---|
129 |
|
---|
130 | span = ra_span_create(base, size);
|
---|
131 | if (!span) {
|
---|
132 | free(arena);
|
---|
133 | return NULL;
|
---|
134 | }
|
---|
135 |
|
---|
136 | list_initialize(&arena->spans);
|
---|
137 | list_append(&span->span_link, &arena->spans);
|
---|
138 |
|
---|
139 | return arena;
|
---|
140 | }
|
---|
141 |
|
---|
142 | void ra_span_add(ra_arena_t *arena, uintptr_t base, size_t size)
|
---|
143 | {
|
---|
144 | ra_span_t *span;
|
---|
145 |
|
---|
146 | span = ra_span_create(base, size);
|
---|
147 | ASSERT(span);
|
---|
148 |
|
---|
149 | /* TODO: check for overlaps */
|
---|
150 | list_append(&span->span_link, &arena->spans);
|
---|
151 | }
|
---|
152 |
|
---|
153 | uintptr_t ra_alloc(ra_arena_t *arena, size_t size, size_t alignment)
|
---|
154 | {
|
---|
155 | return 0;
|
---|
156 | }
|
---|
157 |
|
---|
158 | void ra_free(ra_arena_t *arena, uintptr_t base, size_t size)
|
---|
159 | {
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | /** @}
|
---|
164 | */
|
---|