Rev 490 | Rev 532 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 490 | Rev 501 | ||
---|---|---|---|
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> |
31 | #include <mm/heap.h> |
32 | #include <arch/types.h> |
32 | #include <arch/types.h> |
33 | #include <typedefs.h> |
33 | #include <typedefs.h> |
34 | #include <list.h> |
34 | #include <list.h> |
35 | #include <debug.h> |
35 | #include <debug.h> |
36 | 36 | ||
37 | /** Create buddy system |
37 | /** Create buddy system |
38 | * |
38 | * |
39 | * Allocate memory for and initialize new buddy system. |
39 | * Allocate memory for and initialize new buddy system. |
40 | * |
40 | * |
41 | * @param max_order The biggest allocable size will be 2^max_order. |
41 | * @param max_order The biggest allocable size will be 2^max_order. |
42 | * @param op Operations for new buddy system. |
42 | * @param op Operations for new buddy system. |
43 | * @param data Pointer to be used by implementation. |
43 | * @param data Pointer to be used by implementation. |
44 | * |
44 | * |
45 | * @return New buddy system. |
45 | * @return New buddy system. |
46 | */ |
46 | */ |
47 | buddy_system_t *buddy_system_create(__u8 max_order, buddy_system_operations_t *op, void *data) |
47 | buddy_system_t *buddy_system_create(__u8 max_order, buddy_system_operations_t *op, void *data) |
48 | { |
48 | { |
49 | buddy_system_t *b; |
49 | buddy_system_t *b; |
50 | int i; |
50 | int i; |
51 | 51 | ||
52 | ASSERT(max_order < BUDDY_SYSTEM_INNER_BLOCK); |
52 | ASSERT(max_order < BUDDY_SYSTEM_INNER_BLOCK); |
53 | 53 | ||
54 | ASSERT(op->find_buddy); |
54 | ASSERT(op->find_buddy); |
55 | ASSERT(op->set_order); |
55 | ASSERT(op->set_order); |
56 | ASSERT(op->get_order); |
56 | ASSERT(op->get_order); |
57 | ASSERT(op->bisect); |
57 | ASSERT(op->bisect); |
58 | ASSERT(op->coalesce); |
58 | ASSERT(op->coalesce); |
59 | 59 | ||
60 | /* |
60 | /* |
61 | * Allocate memory for structure describing the whole buddy system. |
61 | * Allocate memory for structure describing the whole buddy system. |
62 | */ |
62 | */ |
63 | b = (buddy_system_t *) early_malloc(sizeof(buddy_system_t)); |
63 | b = (buddy_system_t *) early_malloc(sizeof(buddy_system_t)); |
64 | 64 | ||
65 | if (b) { |
65 | if (b) { |
66 | /* |
66 | /* |
67 | * Allocate memory for all orders this buddy system will work with. |
67 | * Allocate memory for all orders this buddy system will work with. |
68 | */ |
68 | */ |
69 | b->order = (link_t *) early_malloc(max_order * sizeof(link_t)); |
69 | b->order = (link_t *) early_malloc(max_order * sizeof(link_t)); |
70 | if (!b->order) { |
70 | if (!b->order) { |
71 | early_free(b); |
71 | early_free(b); |
72 | return NULL; |
72 | return NULL; |
73 | } |
73 | } |
74 | 74 | ||
75 | for (i = 0; i < max_order; i++) |
75 | for (i = 0; i < max_order; i++) |
76 | list_initialize(&b->order[i]); |
76 | list_initialize(&b->order[i]); |
77 | 77 | ||
78 | b->max_order = max_order; |
78 | b->max_order = max_order; |
79 | b->op = op; |
79 | b->op = op; |
80 | b->data = data; |
80 | b->data = data; |
81 | } |
81 | } |
82 | 82 | ||
83 | return b; |
83 | return b; |
84 | } |
84 | } |
85 | 85 | ||
86 | /** Allocate block from buddy system. |
86 | /** Allocate block from buddy system. |
87 | * |
87 | * |
88 | * @param b Buddy system pointer. |
88 | * @param b Buddy system pointer. |
89 | * @param i Returned block will be 2^i big. |
89 | * @param i Returned block will be 2^i big. |
90 | * |
90 | * |
91 | * @return Block of data represented by link_t. |
91 | * @return Block of data represented by link_t. |
92 | */ |
92 | */ |
93 | link_t *buddy_system_alloc(buddy_system_t *b, __u8 i) |
93 | link_t *buddy_system_alloc(buddy_system_t *b, __u8 i) |
94 | { |
94 | { |
95 | link_t *res, *hlp; |
95 | link_t *res, *hlp; |
96 | 96 | ||
97 | ASSERT(i < b->max_order); |
97 | ASSERT(i < b->max_order); |
98 | 98 | ||
99 | /* |
99 | /* |
100 | * If the list of order i is not empty, |
100 | * If the list of order i is not empty, |
101 | * the request can be immediatelly satisfied. |
101 | * the request can be immediatelly satisfied. |
102 | */ |
102 | */ |
103 | if (!list_empty(&b->order[i])) { |
103 | if (!list_empty(&b->order[i])) { |
104 | res = b->order[i].next; |
104 | res = b->order[i].next; |
105 | list_remove(res); |
105 | list_remove(res); |
106 | return res; |
106 | return res; |
107 | } |
107 | } |
108 | 108 | ||
109 | /* |
109 | /* |
110 | * If order i is already the maximal order, |
110 | * If order i is already the maximal order, |
111 | * the request cannot be satisfied. |
111 | * the request cannot be satisfied. |
112 | */ |
112 | */ |
113 | if (i == b->max_order - 1) |
113 | if (i == b->max_order - 1) |
114 | return NULL; |
114 | return NULL; |
115 | 115 | ||
116 | /* |
116 | /* |
117 | * Try to recursively satisfy the request from higher order lists. |
117 | * Try to recursively satisfy the request from higher order lists. |
118 | */ |
118 | */ |
119 | hlp = buddy_system_alloc(b, i + 1); |
119 | hlp = buddy_system_alloc(b, i + 1); |
120 | 120 | ||
121 | /* |
121 | /* |
122 | * The request could not be satisfied |
122 | * The request could not be satisfied |
123 | * from higher order lists. |
123 | * from higher order lists. |
124 | */ |
124 | */ |
125 | if (!hlp) |
125 | if (!hlp) |
126 | return NULL; |
126 | return NULL; |
127 | 127 | ||
128 | res = hlp; |
128 | res = hlp; |
129 | 129 | ||
130 | /* |
130 | /* |
131 | * Bisect the block and set order of both of its parts to i. |
131 | * Bisect the block and set order of both of its parts to i. |
132 | */ |
132 | */ |
133 | hlp = b->op->bisect(b, res); |
133 | hlp = b->op->bisect(b, res); |
134 | b->op->set_order(b, res, i); |
134 | b->op->set_order(b, res, i); |
135 | b->op->set_order(b, hlp, i); |
135 | b->op->set_order(b, hlp, i); |
136 | 136 | ||
137 | /* |
137 | /* |
138 | * Return the other half to buddy system. |
138 | * Return the other half to buddy system. |
139 | */ |
139 | */ |
140 | buddy_system_free(b, hlp); |
140 | buddy_system_free(b, hlp); |
141 | 141 | ||
142 | return res; |
142 | return res; |
143 | 143 | ||
144 | } |
144 | } |
145 | 145 | ||
146 | /** Return block to buddy system. |
146 | /** Return block to buddy system. |
147 | * |
147 | * |
148 | * @param b Buddy system pointer. |
148 | * @param b Buddy system pointer. |
149 | * @param block Block to return. |
149 | * @param block Block to return. |
150 | */ |
150 | */ |
151 | void buddy_system_free(buddy_system_t *b, link_t *block) |
151 | void buddy_system_free(buddy_system_t *b, link_t *block) |
152 | { |
152 | { |
153 | link_t *buddy, *hlp; |
153 | link_t *buddy, *hlp; |
154 | __u8 i; |
154 | __u8 i; |
155 | 155 | ||
156 | /* |
156 | /* |
157 | * Determine block's order. |
157 | * Determine block's order. |
158 | */ |
158 | */ |
159 | i = b->op->get_order(b, block); |
159 | i = b->op->get_order(b, block); |
160 | 160 | ||
161 | ASSERT(i < b->max_order); |
161 | ASSERT(i < b->max_order); |
162 | 162 | ||
163 | if (i != b->max_order - 1) { |
163 | if (i != b->max_order - 1) { |
164 | /* |
164 | /* |
165 | * See if there is any buddy in the list of order i. |
165 | * See if there is any buddy in the list of order i. |
166 | */ |
166 | */ |
167 | buddy = b->op->find_buddy(b, block); |
167 | buddy = b->op->find_buddy(b, block); |
168 | if (buddy) { |
168 | if (buddy) { |
169 | 169 | ||
170 | ASSERT(b->op->get_order(b, buddy) == i); |
170 | ASSERT(b->op->get_order(b, buddy) == i); |
171 | 171 | ||
172 | /* |
172 | /* |
173 | * Remove buddy from the list of order i. |
173 | * Remove buddy from the list of order i. |
174 | */ |
174 | */ |
175 | list_remove(buddy); |
175 | list_remove(buddy); |
176 | 176 | ||
177 | /* |
177 | /* |
178 | * Invalidate order of both block and buddy. |
178 | * Invalidate order of both block and buddy. |
179 | */ |
179 | */ |
180 | b->op->set_order(b, block, BUDDY_SYSTEM_INNER_BLOCK); |
180 | b->op->set_order(b, block, BUDDY_SYSTEM_INNER_BLOCK); |
181 | b->op->set_order(b, buddy, BUDDY_SYSTEM_INNER_BLOCK); |
181 | b->op->set_order(b, buddy, BUDDY_SYSTEM_INNER_BLOCK); |
182 | 182 | ||
183 | /* |
183 | /* |
184 | * Coalesce block and buddy into one block. |
184 | * Coalesce block and buddy into one block. |
185 | */ |
185 | */ |
186 | hlp = b->op->coalesce(b, block, buddy); |
186 | hlp = b->op->coalesce(b, block, buddy); |
187 | 187 | ||
188 | /* |
188 | /* |
189 | * Set order of the coalesced block to i + 1. |
189 | * Set order of the coalesced block to i + 1. |
190 | */ |
190 | */ |
191 | b->op->set_order(b, hlp, i + 1); |
191 | b->op->set_order(b, hlp, i + 1); |
192 | 192 | ||
193 | /* |
193 | /* |
194 | * Recursively add the coalesced block to the list of order i + 1. |
194 | * Recursively add the coalesced block to the list of order i + 1. |
195 | */ |
195 | */ |
196 | buddy_system_free(b, hlp); |
196 | buddy_system_free(b, hlp); |
197 | return; |
197 | return; |
198 | } |
198 | } |
199 | } |
199 | } |
200 | 200 | ||
201 | /* |
201 | /* |
202 | * Insert block into the list of order i. |
202 | * Insert block into the list of order i. |
203 | */ |
203 | */ |
204 | list_append(block, &b->order[i]); |
204 | list_append(block, &b->order[i]); |
205 | 205 | ||
206 | } |
206 | } |
207 | 207 |