Rev 2071 | Rev 2787 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2071 | Rev 2089 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2005 Jakub Jermar |
2 | * Copyright (c) 2005 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genericmm |
29 | /** @addtogroup genericmm |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief Buddy allocator framework. |
35 | * @brief Buddy allocator framework. |
36 | * |
36 | * |
37 | * This file contains buddy system allocator framework. |
37 | * This file contains buddy system allocator framework. |
38 | * Specialized functions are needed for this abstract framework |
38 | * Specialized functions are needed for this abstract framework |
39 | * to be useful. |
39 | * to be useful. |
40 | */ |
40 | */ |
41 | 41 | ||
42 | #include <mm/buddy.h> |
42 | #include <mm/buddy.h> |
43 | #include <mm/frame.h> |
43 | #include <mm/frame.h> |
44 | #include <arch/types.h> |
44 | #include <arch/types.h> |
45 | #include <typedefs.h> |
- | |
46 | #include <adt/list.h> |
- | |
47 | #include <debug.h> |
45 | #include <debug.h> |
48 | #include <print.h> |
46 | #include <print.h> |
49 | 47 | ||
50 | /** Return size needed for the buddy configuration data */ |
48 | /** Return size needed for the buddy configuration data */ |
51 | size_t buddy_conf_size(int max_order) |
49 | size_t buddy_conf_size(int max_order) |
52 | { |
50 | { |
53 | return sizeof(buddy_system_t) + (max_order + 1) * sizeof(link_t); |
51 | return sizeof(buddy_system_t) + (max_order + 1) * sizeof(link_t); |
54 | } |
52 | } |
55 | 53 | ||
56 | 54 | ||
57 | /** Create buddy system |
55 | /** Create buddy system |
58 | * |
56 | * |
59 | * Allocate memory for and initialize new buddy system. |
57 | * Allocate memory for and initialize new buddy system. |
60 | * |
58 | * |
61 | * @param b Preallocated buddy system control data. |
59 | * @param b Preallocated buddy system control data. |
62 | * @param max_order The biggest allocable size will be 2^max_order. |
60 | * @param max_order The biggest allocable size will be 2^max_order. |
63 | * @param op Operations for new buddy system. |
61 | * @param op Operations for new buddy system. |
64 | * @param data Pointer to be used by implementation. |
62 | * @param data Pointer to be used by implementation. |
65 | * |
63 | * |
66 | * @return New buddy system. |
64 | * @return New buddy system. |
67 | */ |
65 | */ |
68 | void buddy_system_create(buddy_system_t *b, |
66 | void buddy_system_create(buddy_system_t *b, |
69 | uint8_t max_order, |
67 | uint8_t max_order, |
70 | buddy_system_operations_t *op, |
68 | buddy_system_operations_t *op, |
71 | void *data) |
69 | void *data) |
72 | { |
70 | { |
73 | int i; |
71 | int i; |
74 | 72 | ||
75 | ASSERT(max_order < BUDDY_SYSTEM_INNER_BLOCK); |
73 | ASSERT(max_order < BUDDY_SYSTEM_INNER_BLOCK); |
76 | 74 | ||
77 | ASSERT(op->find_buddy); |
75 | ASSERT(op->find_buddy); |
78 | ASSERT(op->set_order); |
76 | ASSERT(op->set_order); |
79 | ASSERT(op->get_order); |
77 | ASSERT(op->get_order); |
80 | ASSERT(op->bisect); |
78 | ASSERT(op->bisect); |
81 | ASSERT(op->coalesce); |
79 | ASSERT(op->coalesce); |
82 | ASSERT(op->mark_busy); |
80 | ASSERT(op->mark_busy); |
83 | 81 | ||
84 | /* |
82 | /* |
85 | * Use memory after our own structure |
83 | * Use memory after our own structure |
86 | */ |
84 | */ |
87 | b->order = (link_t *) (&b[1]); |
85 | b->order = (link_t *) (&b[1]); |
88 | 86 | ||
89 | for (i = 0; i <= max_order; i++) |
87 | for (i = 0; i <= max_order; i++) |
90 | list_initialize(&b->order[i]); |
88 | list_initialize(&b->order[i]); |
91 | 89 | ||
92 | b->max_order = max_order; |
90 | b->max_order = max_order; |
93 | b->op = op; |
91 | b->op = op; |
94 | b->data = data; |
92 | b->data = data; |
95 | } |
93 | } |
96 | 94 | ||
97 | /** Check if buddy system can allocate block |
95 | /** Check if buddy system can allocate block |
98 | * |
96 | * |
99 | * @param b Buddy system pointer |
97 | * @param b Buddy system pointer |
100 | * @param i Size of the block (2^i) |
98 | * @param i Size of the block (2^i) |
101 | * |
99 | * |
102 | * @return True if block can be allocated |
100 | * @return True if block can be allocated |
103 | */ |
101 | */ |
104 | bool buddy_system_can_alloc(buddy_system_t *b, uint8_t i) { |
102 | bool buddy_system_can_alloc(buddy_system_t *b, uint8_t i) { |
105 | uint8_t k; |
103 | uint8_t k; |
106 | 104 | ||
107 | /* |
105 | /* |
108 | * If requested block is greater then maximal block |
106 | * If requested block is greater then maximal block |
109 | * we know immediatly that we cannot satisfy the request. |
107 | * we know immediatly that we cannot satisfy the request. |
110 | */ |
108 | */ |
111 | if (i > b->max_order) return false; |
109 | if (i > b->max_order) return false; |
112 | 110 | ||
113 | /* |
111 | /* |
114 | * Check if any bigger or equal order has free elements |
112 | * Check if any bigger or equal order has free elements |
115 | */ |
113 | */ |
116 | for (k=i; k <= b->max_order; k++) { |
114 | for (k=i; k <= b->max_order; k++) { |
117 | if (!list_empty(&b->order[k])) { |
115 | if (!list_empty(&b->order[k])) { |
118 | return true; |
116 | return true; |
119 | } |
117 | } |
120 | } |
118 | } |
121 | 119 | ||
122 | return false; |
120 | return false; |
123 | 121 | ||
124 | } |
122 | } |
125 | 123 | ||
126 | /** Allocate PARTICULAR block from buddy system |
124 | /** Allocate PARTICULAR block from buddy system |
127 | * |
125 | * |
128 | * @ return Block of data or NULL if no such block was found |
126 | * @ return Block of data or NULL if no such block was found |
129 | */ |
127 | */ |
130 | link_t *buddy_system_alloc_block(buddy_system_t *b, link_t *block) |
128 | link_t *buddy_system_alloc_block(buddy_system_t *b, link_t *block) |
131 | { |
129 | { |
132 | link_t *left,*right, *tmp; |
130 | link_t *left,*right, *tmp; |
133 | uint8_t order; |
131 | uint8_t order; |
134 | 132 | ||
135 | left = b->op->find_block(b, block, BUDDY_SYSTEM_INNER_BLOCK); |
133 | left = b->op->find_block(b, block, BUDDY_SYSTEM_INNER_BLOCK); |
136 | ASSERT(left); |
134 | ASSERT(left); |
137 | list_remove(left); |
135 | list_remove(left); |
138 | while (1) { |
136 | while (1) { |
139 | if (! b->op->get_order(b,left)) { |
137 | if (! b->op->get_order(b,left)) { |
140 | b->op->mark_busy(b, left); |
138 | b->op->mark_busy(b, left); |
141 | return left; |
139 | return left; |
142 | } |
140 | } |
143 | 141 | ||
144 | order = b->op->get_order(b, left); |
142 | order = b->op->get_order(b, left); |
145 | 143 | ||
146 | right = b->op->bisect(b, left); |
144 | right = b->op->bisect(b, left); |
147 | b->op->set_order(b, left, order-1); |
145 | b->op->set_order(b, left, order-1); |
148 | b->op->set_order(b, right, order-1); |
146 | b->op->set_order(b, right, order-1); |
149 | 147 | ||
150 | tmp = b->op->find_block(b, block, BUDDY_SYSTEM_INNER_BLOCK); |
148 | tmp = b->op->find_block(b, block, BUDDY_SYSTEM_INNER_BLOCK); |
151 | 149 | ||
152 | if (tmp == right) { |
150 | if (tmp == right) { |
153 | right = left; |
151 | right = left; |
154 | left = tmp; |
152 | left = tmp; |
155 | } |
153 | } |
156 | ASSERT(tmp == left); |
154 | ASSERT(tmp == left); |
157 | b->op->mark_busy(b, left); |
155 | b->op->mark_busy(b, left); |
158 | buddy_system_free(b, right); |
156 | buddy_system_free(b, right); |
159 | b->op->mark_available(b, left); |
157 | b->op->mark_available(b, left); |
160 | } |
158 | } |
161 | } |
159 | } |
162 | 160 | ||
163 | /** Allocate block from buddy system. |
161 | /** Allocate block from buddy system. |
164 | * |
162 | * |
165 | * @param b Buddy system pointer. |
163 | * @param b Buddy system pointer. |
166 | * @param i Returned block will be 2^i big. |
164 | * @param i Returned block will be 2^i big. |
167 | * |
165 | * |
168 | * @return Block of data represented by link_t. |
166 | * @return Block of data represented by link_t. |
169 | */ |
167 | */ |
170 | link_t *buddy_system_alloc(buddy_system_t *b, uint8_t i) |
168 | link_t *buddy_system_alloc(buddy_system_t *b, uint8_t i) |
171 | { |
169 | { |
172 | link_t *res, *hlp; |
170 | link_t *res, *hlp; |
173 | 171 | ||
174 | ASSERT(i <= b->max_order); |
172 | ASSERT(i <= b->max_order); |
175 | 173 | ||
176 | /* |
174 | /* |
177 | * If the list of order i is not empty, |
175 | * If the list of order i is not empty, |
178 | * the request can be immediatelly satisfied. |
176 | * the request can be immediatelly satisfied. |
179 | */ |
177 | */ |
180 | if (!list_empty(&b->order[i])) { |
178 | if (!list_empty(&b->order[i])) { |
181 | res = b->order[i].next; |
179 | res = b->order[i].next; |
182 | list_remove(res); |
180 | list_remove(res); |
183 | b->op->mark_busy(b, res); |
181 | b->op->mark_busy(b, res); |
184 | return res; |
182 | return res; |
185 | } |
183 | } |
186 | /* |
184 | /* |
187 | * If order i is already the maximal order, |
185 | * If order i is already the maximal order, |
188 | * the request cannot be satisfied. |
186 | * the request cannot be satisfied. |
189 | */ |
187 | */ |
190 | if (i == b->max_order) |
188 | if (i == b->max_order) |
191 | return NULL; |
189 | return NULL; |
192 | 190 | ||
193 | /* |
191 | /* |
194 | * Try to recursively satisfy the request from higher order lists. |
192 | * Try to recursively satisfy the request from higher order lists. |
195 | */ |
193 | */ |
196 | hlp = buddy_system_alloc(b, i + 1); |
194 | hlp = buddy_system_alloc(b, i + 1); |
197 | 195 | ||
198 | /* |
196 | /* |
199 | * The request could not be satisfied |
197 | * The request could not be satisfied |
200 | * from higher order lists. |
198 | * from higher order lists. |
201 | */ |
199 | */ |
202 | if (!hlp) |
200 | if (!hlp) |
203 | return NULL; |
201 | return NULL; |
204 | 202 | ||
205 | res = hlp; |
203 | res = hlp; |
206 | 204 | ||
207 | /* |
205 | /* |
208 | * Bisect the block and set order of both of its parts to i. |
206 | * Bisect the block and set order of both of its parts to i. |
209 | */ |
207 | */ |
210 | hlp = b->op->bisect(b, res); |
208 | hlp = b->op->bisect(b, res); |
211 | b->op->set_order(b, res, i); |
209 | b->op->set_order(b, res, i); |
212 | b->op->set_order(b, hlp, i); |
210 | b->op->set_order(b, hlp, i); |
213 | 211 | ||
214 | /* |
212 | /* |
215 | * Return the other half to buddy system. Mark the first part |
213 | * Return the other half to buddy system. Mark the first part |
216 | * full, so that it won't coalesce again. |
214 | * full, so that it won't coalesce again. |
217 | */ |
215 | */ |
218 | b->op->mark_busy(b, res); |
216 | b->op->mark_busy(b, res); |
219 | buddy_system_free(b, hlp); |
217 | buddy_system_free(b, hlp); |
220 | 218 | ||
221 | return res; |
219 | return res; |
222 | 220 | ||
223 | } |
221 | } |
224 | 222 | ||
225 | /** Return block to buddy system. |
223 | /** Return block to buddy system. |
226 | * |
224 | * |
227 | * @param b Buddy system pointer. |
225 | * @param b Buddy system pointer. |
228 | * @param block Block to return. |
226 | * @param block Block to return. |
229 | */ |
227 | */ |
230 | void buddy_system_free(buddy_system_t *b, link_t *block) |
228 | void buddy_system_free(buddy_system_t *b, link_t *block) |
231 | { |
229 | { |
232 | link_t *buddy, *hlp; |
230 | link_t *buddy, *hlp; |
233 | uint8_t i; |
231 | uint8_t i; |
234 | 232 | ||
235 | /* |
233 | /* |
236 | * Determine block's order. |
234 | * Determine block's order. |
237 | */ |
235 | */ |
238 | i = b->op->get_order(b, block); |
236 | i = b->op->get_order(b, block); |
239 | 237 | ||
240 | ASSERT(i <= b->max_order); |
238 | ASSERT(i <= b->max_order); |
241 | 239 | ||
242 | if (i != b->max_order) { |
240 | if (i != b->max_order) { |
243 | /* |
241 | /* |
244 | * See if there is any buddy in the list of order i. |
242 | * See if there is any buddy in the list of order i. |
245 | */ |
243 | */ |
246 | buddy = b->op->find_buddy(b, block); |
244 | buddy = b->op->find_buddy(b, block); |
247 | if (buddy) { |
245 | if (buddy) { |
248 | 246 | ||
249 | ASSERT(b->op->get_order(b, buddy) == i); |
247 | ASSERT(b->op->get_order(b, buddy) == i); |
250 | /* |
248 | /* |
251 | * Remove buddy from the list of order i. |
249 | * Remove buddy from the list of order i. |
252 | */ |
250 | */ |
253 | list_remove(buddy); |
251 | list_remove(buddy); |
254 | 252 | ||
255 | /* |
253 | /* |
256 | * Invalidate order of both block and buddy. |
254 | * Invalidate order of both block and buddy. |
257 | */ |
255 | */ |
258 | b->op->set_order(b, block, BUDDY_SYSTEM_INNER_BLOCK); |
256 | b->op->set_order(b, block, BUDDY_SYSTEM_INNER_BLOCK); |
259 | b->op->set_order(b, buddy, BUDDY_SYSTEM_INNER_BLOCK); |
257 | b->op->set_order(b, buddy, BUDDY_SYSTEM_INNER_BLOCK); |
260 | 258 | ||
261 | /* |
259 | /* |
262 | * Coalesce block and buddy into one block. |
260 | * Coalesce block and buddy into one block. |
263 | */ |
261 | */ |
264 | hlp = b->op->coalesce(b, block, buddy); |
262 | hlp = b->op->coalesce(b, block, buddy); |
265 | 263 | ||
266 | /* |
264 | /* |
267 | * Set order of the coalesced block to i + 1. |
265 | * Set order of the coalesced block to i + 1. |
268 | */ |
266 | */ |
269 | b->op->set_order(b, hlp, i + 1); |
267 | b->op->set_order(b, hlp, i + 1); |
270 | 268 | ||
271 | /* |
269 | /* |
272 | * Recursively add the coalesced block to the list of order i + 1. |
270 | * Recursively add the coalesced block to the list of order i + 1. |
273 | */ |
271 | */ |
274 | buddy_system_free(b, hlp); |
272 | buddy_system_free(b, hlp); |
275 | return; |
273 | return; |
276 | } |
274 | } |
277 | } |
275 | } |
278 | 276 | ||
279 | /* |
277 | /* |
280 | * Insert block into the list of order i. |
278 | * Insert block into the list of order i. |
281 | */ |
279 | */ |
282 | list_append(block, &b->order[i]); |
280 | list_append(block, &b->order[i]); |
283 | 281 | ||
284 | } |
282 | } |
285 | 283 | ||
286 | /** Prints out structure of buddy system |
284 | /** Prints out structure of buddy system |
287 | * |
285 | * |
288 | * @param b Pointer to buddy system |
286 | * @param b Pointer to buddy system |
289 | * @param elem_size Element size |
287 | * @param elem_size Element size |
290 | */ |
288 | */ |
291 | void buddy_system_structure_print(buddy_system_t *b, size_t elem_size) { |
289 | void buddy_system_structure_print(buddy_system_t *b, size_t elem_size) { |
292 | index_t i; |
290 | index_t i; |
293 | count_t cnt, elem_count = 0, block_count = 0; |
291 | count_t cnt, elem_count = 0, block_count = 0; |
294 | link_t * cur; |
292 | link_t * cur; |
295 | 293 | ||
296 | 294 | ||
297 | printf("Order\tBlocks\tSize \tBlock size\tElems per block\n"); |
295 | printf("Order\tBlocks\tSize \tBlock size\tElems per block\n"); |
298 | printf("-----\t------\t--------\t----------\t---------------\n"); |
296 | printf("-----\t------\t--------\t----------\t---------------\n"); |
299 | 297 | ||
300 | for (i=0;i <= b->max_order; i++) { |
298 | for (i=0;i <= b->max_order; i++) { |
301 | cnt = 0; |
299 | cnt = 0; |
302 | if (!list_empty(&b->order[i])) { |
300 | if (!list_empty(&b->order[i])) { |
303 | for (cur = b->order[i].next; cur != &b->order[i]; cur = cur->next) |
301 | for (cur = b->order[i].next; cur != &b->order[i]; cur = cur->next) |
304 | cnt++; |
302 | cnt++; |
305 | } |
303 | } |
306 | 304 | ||
307 | printf("#%zd\t%5zd\t%7zdK\t%8zdK\t%6zd\t", i, cnt, (cnt * (1 << i) * elem_size) >> 10, ((1 << i) * elem_size) >> 10, 1 << i); |
305 | printf("#%zd\t%5zd\t%7zdK\t%8zdK\t%6zd\t", i, cnt, (cnt * (1 << i) * elem_size) >> 10, ((1 << i) * elem_size) >> 10, 1 << i); |
308 | if (!list_empty(&b->order[i])) { |
306 | if (!list_empty(&b->order[i])) { |
309 | for (cur = b->order[i].next; cur != &b->order[i]; cur = cur->next) { |
307 | for (cur = b->order[i].next; cur != &b->order[i]; cur = cur->next) { |
310 | b->op->print_id(b, cur); |
308 | b->op->print_id(b, cur); |
311 | printf(" "); |
309 | printf(" "); |
312 | } |
310 | } |
313 | } |
311 | } |
314 | printf("\n"); |
312 | printf("\n"); |
315 | 313 | ||
316 | block_count += cnt; |
314 | block_count += cnt; |
317 | elem_count += (1 << i) * cnt; |
315 | elem_count += (1 << i) * cnt; |
318 | } |
316 | } |
319 | printf("-----\t------\t--------\t----------\t---------------\n"); |
317 | printf("-----\t------\t--------\t----------\t---------------\n"); |
320 | printf("Buddy system contains %zd free elements (%zd blocks)\n" , elem_count, block_count); |
318 | printf("Buddy system contains %zd free elements (%zd blocks)\n" , elem_count, block_count); |
321 | 319 | ||
322 | } |
320 | } |
323 | 321 | ||
324 | /** @} |
322 | /** @} |
325 | */ |
323 | */ |
326 | 324 |