Subversion Repositories HelenOS-historic

Rev

Rev 1229 | Rev 1708 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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