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