Rev 1248 | Rev 1708 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1248 | Rev 1702 | ||
---|---|---|---|
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 |
|
- | 30 | * @{ |
|
- | 31 | */ |
|
- | 32 | ||
29 | /** |
33 | /** |
30 | * @file buddy.c |
34 | * @file |
31 | * @brief Buddy allocator framework. |
35 | * @brief Buddy allocator framework. |
32 | * |
36 | * |
33 | * This file contains buddy system allocator framework. |
37 | * This file contains buddy system allocator framework. |
34 | * Specialized functions are needed for this abstract framework |
38 | * Specialized functions are needed for this abstract framework |
35 | * to be useful. |
39 | * to be useful. |
36 | */ |
40 | */ |
37 | 41 | ||
38 | #include <mm/buddy.h> |
42 | #include <mm/buddy.h> |
39 | #include <mm/frame.h> |
43 | #include <mm/frame.h> |
40 | #include <arch/types.h> |
44 | #include <arch/types.h> |
41 | #include <typedefs.h> |
45 | #include <typedefs.h> |
42 | #include <adt/list.h> |
46 | #include <adt/list.h> |
43 | #include <debug.h> |
47 | #include <debug.h> |
44 | #include <print.h> |
48 | #include <print.h> |
45 | 49 | ||
46 | /** Return size needed for the buddy configuration data */ |
50 | /** Return size needed for the buddy configuration data */ |
47 | size_t buddy_conf_size(int max_order) |
51 | size_t buddy_conf_size(int max_order) |
48 | { |
52 | { |
49 | return sizeof(buddy_system_t) + (max_order + 1) * sizeof(link_t); |
53 | return sizeof(buddy_system_t) + (max_order + 1) * sizeof(link_t); |
50 | } |
54 | } |
51 | 55 | ||
52 | 56 | ||
53 | /** Create buddy system |
57 | /** Create buddy system |
54 | * |
58 | * |
55 | * Allocate memory for and initialize new buddy system. |
59 | * Allocate memory for and initialize new buddy system. |
56 | * |
60 | * |
57 | * @param b Preallocated buddy system control data. |
61 | * @param b Preallocated buddy system control data. |
58 | * @param max_order The biggest allocable size will be 2^max_order. |
62 | * @param max_order The biggest allocable size will be 2^max_order. |
59 | * @param op Operations for new buddy system. |
63 | * @param op Operations for new buddy system. |
60 | * @param data Pointer to be used by implementation. |
64 | * @param data Pointer to be used by implementation. |
61 | * |
65 | * |
62 | * @return New buddy system. |
66 | * @return New buddy system. |
63 | */ |
67 | */ |
64 | void buddy_system_create(buddy_system_t *b, |
68 | void buddy_system_create(buddy_system_t *b, |
65 | __u8 max_order, |
69 | __u8 max_order, |
66 | buddy_system_operations_t *op, |
70 | buddy_system_operations_t *op, |
67 | void *data) |
71 | void *data) |
68 | { |
72 | { |
69 | int i; |
73 | int i; |
70 | 74 | ||
71 | ASSERT(max_order < BUDDY_SYSTEM_INNER_BLOCK); |
75 | ASSERT(max_order < BUDDY_SYSTEM_INNER_BLOCK); |
72 | 76 | ||
73 | ASSERT(op->find_buddy); |
77 | ASSERT(op->find_buddy); |
74 | ASSERT(op->set_order); |
78 | ASSERT(op->set_order); |
75 | ASSERT(op->get_order); |
79 | ASSERT(op->get_order); |
76 | ASSERT(op->bisect); |
80 | ASSERT(op->bisect); |
77 | ASSERT(op->coalesce); |
81 | ASSERT(op->coalesce); |
78 | ASSERT(op->mark_busy); |
82 | ASSERT(op->mark_busy); |
79 | 83 | ||
80 | /* |
84 | /* |
81 | * Use memory after our own structure |
85 | * Use memory after our own structure |
82 | */ |
86 | */ |
83 | b->order = (link_t *) (&b[1]); |
87 | b->order = (link_t *) (&b[1]); |
84 | 88 | ||
85 | for (i = 0; i <= max_order; i++) |
89 | for (i = 0; i <= max_order; i++) |
86 | list_initialize(&b->order[i]); |
90 | list_initialize(&b->order[i]); |
87 | 91 | ||
88 | b->max_order = max_order; |
92 | b->max_order = max_order; |
89 | b->op = op; |
93 | b->op = op; |
90 | b->data = data; |
94 | b->data = data; |
91 | } |
95 | } |
92 | 96 | ||
93 | /** Check if buddy system can allocate block |
97 | /** Check if buddy system can allocate block |
94 | * |
98 | * |
95 | * @param b Buddy system pointer |
99 | * @param b Buddy system pointer |
96 | * @param i Size of the block (2^i) |
100 | * @param i Size of the block (2^i) |
97 | * |
101 | * |
98 | * @return True if block can be allocated |
102 | * @return True if block can be allocated |
99 | */ |
103 | */ |
100 | bool buddy_system_can_alloc(buddy_system_t *b, __u8 i) { |
104 | bool buddy_system_can_alloc(buddy_system_t *b, __u8 i) { |
101 | __u8 k; |
105 | __u8 k; |
102 | 106 | ||
103 | /* |
107 | /* |
104 | * If requested block is greater then maximal block |
108 | * If requested block is greater then maximal block |
105 | * we know immediatly that we cannot satisfy the request. |
109 | * we know immediatly that we cannot satisfy the request. |
106 | */ |
110 | */ |
107 | if (i > b->max_order) return false; |
111 | if (i > b->max_order) return false; |
108 | 112 | ||
109 | /* |
113 | /* |
110 | * Check if any bigger or equal order has free elements |
114 | * Check if any bigger or equal order has free elements |
111 | */ |
115 | */ |
112 | for (k=i; k <= b->max_order; k++) { |
116 | for (k=i; k <= b->max_order; k++) { |
113 | if (!list_empty(&b->order[k])) { |
117 | if (!list_empty(&b->order[k])) { |
114 | return true; |
118 | return true; |
115 | } |
119 | } |
116 | } |
120 | } |
117 | 121 | ||
118 | return false; |
122 | return false; |
119 | 123 | ||
120 | } |
124 | } |
121 | 125 | ||
122 | /** Allocate PARTICULAR block from buddy system |
126 | /** Allocate PARTICULAR block from buddy system |
123 | * |
127 | * |
124 | * @ return Block of data or NULL if no such block was found |
128 | * @ return Block of data or NULL if no such block was found |
125 | */ |
129 | */ |
126 | link_t *buddy_system_alloc_block(buddy_system_t *b, link_t *block) |
130 | link_t *buddy_system_alloc_block(buddy_system_t *b, link_t *block) |
127 | { |
131 | { |
128 | link_t *left,*right, *tmp; |
132 | link_t *left,*right, *tmp; |
129 | __u8 order; |
133 | __u8 order; |
130 | 134 | ||
131 | left = b->op->find_block(b, block, BUDDY_SYSTEM_INNER_BLOCK); |
135 | left = b->op->find_block(b, block, BUDDY_SYSTEM_INNER_BLOCK); |
132 | ASSERT(left); |
136 | ASSERT(left); |
133 | list_remove(left); |
137 | list_remove(left); |
134 | while (1) { |
138 | while (1) { |
135 | if (! b->op->get_order(b,left)) { |
139 | if (! b->op->get_order(b,left)) { |
136 | b->op->mark_busy(b, left); |
140 | b->op->mark_busy(b, left); |
137 | return left; |
141 | return left; |
138 | } |
142 | } |
139 | 143 | ||
140 | order = b->op->get_order(b, left); |
144 | order = b->op->get_order(b, left); |
141 | 145 | ||
142 | right = b->op->bisect(b, left); |
146 | right = b->op->bisect(b, left); |
143 | b->op->set_order(b, left, order-1); |
147 | b->op->set_order(b, left, order-1); |
144 | b->op->set_order(b, right, order-1); |
148 | b->op->set_order(b, right, order-1); |
145 | 149 | ||
146 | tmp = b->op->find_block(b, block, BUDDY_SYSTEM_INNER_BLOCK); |
150 | tmp = b->op->find_block(b, block, BUDDY_SYSTEM_INNER_BLOCK); |
147 | 151 | ||
148 | if (tmp == right) { |
152 | if (tmp == right) { |
149 | right = left; |
153 | right = left; |
150 | left = tmp; |
154 | left = tmp; |
151 | } |
155 | } |
152 | ASSERT(tmp == left); |
156 | ASSERT(tmp == left); |
153 | b->op->mark_busy(b, left); |
157 | b->op->mark_busy(b, left); |
154 | buddy_system_free(b, right); |
158 | buddy_system_free(b, right); |
155 | b->op->mark_available(b, left); |
159 | b->op->mark_available(b, left); |
156 | } |
160 | } |
157 | } |
161 | } |
158 | 162 | ||
159 | /** Allocate block from buddy system. |
163 | /** Allocate block from buddy system. |
160 | * |
164 | * |
161 | * @param b Buddy system pointer. |
165 | * @param b Buddy system pointer. |
162 | * @param i Returned block will be 2^i big. |
166 | * @param i Returned block will be 2^i big. |
163 | * |
167 | * |
164 | * @return Block of data represented by link_t. |
168 | * @return Block of data represented by link_t. |
165 | */ |
169 | */ |
166 | link_t *buddy_system_alloc(buddy_system_t *b, __u8 i) |
170 | link_t *buddy_system_alloc(buddy_system_t *b, __u8 i) |
167 | { |
171 | { |
168 | link_t *res, *hlp; |
172 | link_t *res, *hlp; |
169 | 173 | ||
170 | ASSERT(i <= b->max_order); |
174 | ASSERT(i <= b->max_order); |
171 | 175 | ||
172 | /* |
176 | /* |
173 | * If the list of order i is not empty, |
177 | * If the list of order i is not empty, |
174 | * the request can be immediatelly satisfied. |
178 | * the request can be immediatelly satisfied. |
175 | */ |
179 | */ |
176 | if (!list_empty(&b->order[i])) { |
180 | if (!list_empty(&b->order[i])) { |
177 | res = b->order[i].next; |
181 | res = b->order[i].next; |
178 | list_remove(res); |
182 | list_remove(res); |
179 | b->op->mark_busy(b, res); |
183 | b->op->mark_busy(b, res); |
180 | return res; |
184 | return res; |
181 | } |
185 | } |
182 | /* |
186 | /* |
183 | * If order i is already the maximal order, |
187 | * If order i is already the maximal order, |
184 | * the request cannot be satisfied. |
188 | * the request cannot be satisfied. |
185 | */ |
189 | */ |
186 | if (i == b->max_order) |
190 | if (i == b->max_order) |
187 | return NULL; |
191 | return NULL; |
188 | 192 | ||
189 | /* |
193 | /* |
190 | * Try to recursively satisfy the request from higher order lists. |
194 | * Try to recursively satisfy the request from higher order lists. |
191 | */ |
195 | */ |
192 | hlp = buddy_system_alloc(b, i + 1); |
196 | hlp = buddy_system_alloc(b, i + 1); |
193 | 197 | ||
194 | /* |
198 | /* |
195 | * The request could not be satisfied |
199 | * The request could not be satisfied |
196 | * from higher order lists. |
200 | * from higher order lists. |
197 | */ |
201 | */ |
198 | if (!hlp) |
202 | if (!hlp) |
199 | return NULL; |
203 | return NULL; |
200 | 204 | ||
201 | res = hlp; |
205 | res = hlp; |
202 | 206 | ||
203 | /* |
207 | /* |
204 | * Bisect the block and set order of both of its parts to i. |
208 | * Bisect the block and set order of both of its parts to i. |
205 | */ |
209 | */ |
206 | hlp = b->op->bisect(b, res); |
210 | hlp = b->op->bisect(b, res); |
207 | b->op->set_order(b, res, i); |
211 | b->op->set_order(b, res, i); |
208 | b->op->set_order(b, hlp, i); |
212 | b->op->set_order(b, hlp, i); |
209 | 213 | ||
210 | /* |
214 | /* |
211 | * Return the other half to buddy system. Mark the first part |
215 | * Return the other half to buddy system. Mark the first part |
212 | * full, so that it won't coalesce again. |
216 | * full, so that it won't coalesce again. |
213 | */ |
217 | */ |
214 | b->op->mark_busy(b, res); |
218 | b->op->mark_busy(b, res); |
215 | buddy_system_free(b, hlp); |
219 | buddy_system_free(b, hlp); |
216 | 220 | ||
217 | return res; |
221 | return res; |
218 | 222 | ||
219 | } |
223 | } |
220 | 224 | ||
221 | /** Return block to buddy system. |
225 | /** Return block to buddy system. |
222 | * |
226 | * |
223 | * @param b Buddy system pointer. |
227 | * @param b Buddy system pointer. |
224 | * @param block Block to return. |
228 | * @param block Block to return. |
225 | */ |
229 | */ |
226 | void buddy_system_free(buddy_system_t *b, link_t *block) |
230 | void buddy_system_free(buddy_system_t *b, link_t *block) |
227 | { |
231 | { |
228 | link_t *buddy, *hlp; |
232 | link_t *buddy, *hlp; |
229 | __u8 i; |
233 | __u8 i; |
230 | 234 | ||
231 | /* |
235 | /* |
232 | * Determine block's order. |
236 | * Determine block's order. |
233 | */ |
237 | */ |
234 | i = b->op->get_order(b, block); |
238 | i = b->op->get_order(b, block); |
235 | 239 | ||
236 | ASSERT(i <= b->max_order); |
240 | ASSERT(i <= b->max_order); |
237 | 241 | ||
238 | if (i != b->max_order) { |
242 | if (i != b->max_order) { |
239 | /* |
243 | /* |
240 | * See if there is any buddy in the list of order i. |
244 | * See if there is any buddy in the list of order i. |
241 | */ |
245 | */ |
242 | buddy = b->op->find_buddy(b, block); |
246 | buddy = b->op->find_buddy(b, block); |
243 | if (buddy) { |
247 | if (buddy) { |
244 | 248 | ||
245 | ASSERT(b->op->get_order(b, buddy) == i); |
249 | ASSERT(b->op->get_order(b, buddy) == i); |
246 | /* |
250 | /* |
247 | * Remove buddy from the list of order i. |
251 | * Remove buddy from the list of order i. |
248 | */ |
252 | */ |
249 | list_remove(buddy); |
253 | list_remove(buddy); |
250 | 254 | ||
251 | /* |
255 | /* |
252 | * Invalidate order of both block and buddy. |
256 | * Invalidate order of both block and buddy. |
253 | */ |
257 | */ |
254 | b->op->set_order(b, block, BUDDY_SYSTEM_INNER_BLOCK); |
258 | b->op->set_order(b, block, BUDDY_SYSTEM_INNER_BLOCK); |
255 | b->op->set_order(b, buddy, BUDDY_SYSTEM_INNER_BLOCK); |
259 | b->op->set_order(b, buddy, BUDDY_SYSTEM_INNER_BLOCK); |
256 | 260 | ||
257 | /* |
261 | /* |
258 | * Coalesce block and buddy into one block. |
262 | * Coalesce block and buddy into one block. |
259 | */ |
263 | */ |
260 | hlp = b->op->coalesce(b, block, buddy); |
264 | hlp = b->op->coalesce(b, block, buddy); |
261 | 265 | ||
262 | /* |
266 | /* |
263 | * Set order of the coalesced block to i + 1. |
267 | * Set order of the coalesced block to i + 1. |
264 | */ |
268 | */ |
265 | b->op->set_order(b, hlp, i + 1); |
269 | b->op->set_order(b, hlp, i + 1); |
266 | 270 | ||
267 | /* |
271 | /* |
268 | * Recursively add the coalesced block to the list of order i + 1. |
272 | * Recursively add the coalesced block to the list of order i + 1. |
269 | */ |
273 | */ |
270 | buddy_system_free(b, hlp); |
274 | buddy_system_free(b, hlp); |
271 | return; |
275 | return; |
272 | } |
276 | } |
273 | } |
277 | } |
274 | 278 | ||
275 | /* |
279 | /* |
276 | * Insert block into the list of order i. |
280 | * Insert block into the list of order i. |
277 | */ |
281 | */ |
278 | list_append(block, &b->order[i]); |
282 | list_append(block, &b->order[i]); |
279 | 283 | ||
280 | } |
284 | } |
281 | 285 | ||
282 | /** Prints out structure of buddy system |
286 | /** Prints out structure of buddy system |
283 | * |
287 | * |
284 | * @param b Pointer to buddy system |
288 | * @param b Pointer to buddy system |
285 | * @param es Element size |
289 | * @param es Element size |
286 | */ |
290 | */ |
287 | void buddy_system_structure_print(buddy_system_t *b, size_t elem_size) { |
291 | void buddy_system_structure_print(buddy_system_t *b, size_t elem_size) { |
288 | index_t i; |
292 | index_t i; |
289 | count_t cnt, elem_count = 0, block_count = 0; |
293 | count_t cnt, elem_count = 0, block_count = 0; |
290 | link_t * cur; |
294 | link_t * cur; |
291 | 295 | ||
292 | 296 | ||
293 | printf("Order\tBlocks\tSize \tBlock size\tElems per block\n"); |
297 | printf("Order\tBlocks\tSize \tBlock size\tElems per block\n"); |
294 | printf("-----\t------\t--------\t----------\t---------------\n"); |
298 | printf("-----\t------\t--------\t----------\t---------------\n"); |
295 | 299 | ||
296 | for (i=0;i <= b->max_order; i++) { |
300 | for (i=0;i <= b->max_order; i++) { |
297 | cnt = 0; |
301 | cnt = 0; |
298 | if (!list_empty(&b->order[i])) { |
302 | if (!list_empty(&b->order[i])) { |
299 | for (cur = b->order[i].next; cur != &b->order[i]; cur = cur->next) |
303 | for (cur = b->order[i].next; cur != &b->order[i]; cur = cur->next) |
300 | cnt++; |
304 | cnt++; |
301 | } |
305 | } |
302 | 306 | ||
303 | 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); |
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); |
304 | if (!list_empty(&b->order[i])) { |
308 | if (!list_empty(&b->order[i])) { |
305 | for (cur = b->order[i].next; cur != &b->order[i]; cur = cur->next) { |
309 | for (cur = b->order[i].next; cur != &b->order[i]; cur = cur->next) { |
306 | b->op->print_id(b, cur); |
310 | b->op->print_id(b, cur); |
307 | printf(" "); |
311 | printf(" "); |
308 | } |
312 | } |
309 | } |
313 | } |
310 | printf("\n"); |
314 | printf("\n"); |
311 | 315 | ||
312 | block_count += cnt; |
316 | block_count += cnt; |
313 | elem_count += (1 << i) * cnt; |
317 | elem_count += (1 << i) * cnt; |
314 | } |
318 | } |
315 | printf("-----\t------\t--------\t----------\t---------------\n"); |
319 | printf("-----\t------\t--------\t----------\t---------------\n"); |
316 | printf("Buddy system contains %zd free elements (%zd blocks)\n" , elem_count, block_count); |
320 | printf("Buddy system contains %zd free elements (%zd blocks)\n" , elem_count, block_count); |
317 | 321 | ||
318 | } |
322 | } |
- | 323 | ||
- | 324 | /** @} |
|
- | 325 | */ |
|
- | 326 | ||
319 | 327 |