Subversion Repositories HelenOS

Rev

Rev 3425 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
377 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2005 Jakub Jermar
377 jermar 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
1757 jermar 29
/** @addtogroup genericmm
1702 cejka 30
 * @{
31
 */
32
 
1248 jermar 33
/**
1702 cejka 34
 * @file
1248 jermar 35
 * @brief   Buddy allocator framework.
36
 *
37
 * This file contains buddy system allocator framework.
3425 svoboda 38
 * Specialized functions are needed for this abstract framework to be useful.
1248 jermar 39
 */
40
 
377 jermar 41
#include <mm/buddy.h>
42
#include <mm/frame.h>
43
#include <arch/types.h>
44
#include <debug.h>
533 bondari 45
#include <print.h>
3424 svoboda 46
#include <macros.h>
377 jermar 47
 
3425 svoboda 48
/** Return size needed for the buddy configuration data. */
4377 svoboda 49
size_t buddy_conf_size(size_t max_order)
814 palkovsky 50
{
51
    return sizeof(buddy_system_t) + (max_order + 1) * sizeof(link_t);
52
}
53
 
54
 
3425 svoboda 55
/** Create buddy system.
377 jermar 56
 *
57
 * Allocate memory for and initialize new buddy system.
58
 *
3425 svoboda 59
 * @param b     Preallocated buddy system control data.
60
 * @param max_order The biggest allocable size will be 2^max_order.
61
 * @param op        Operations for new buddy system.
62
 * @param data      Pointer to be used by implementation.
377 jermar 63
 *
3425 svoboda 64
 * @return      New buddy system.
377 jermar 65
 */
3425 svoboda 66
void
67
buddy_system_create(buddy_system_t *b, uint8_t max_order,
68
    buddy_system_operations_t *op, void *data)
377 jermar 69
{
70
    int i;
71
 
378 jermar 72
    ASSERT(max_order < BUDDY_SYSTEM_INNER_BLOCK);
73
 
377 jermar 74
    ASSERT(op->find_buddy);
75
    ASSERT(op->set_order);
76
    ASSERT(op->get_order);
77
    ASSERT(op->bisect);
78
    ASSERT(op->coalesce);
533 bondari 79
    ASSERT(op->mark_busy);
377 jermar 80
 
81
    /*
3425 svoboda 82
     * Use memory after our own structure.
814 palkovsky 83
     */
84
    b->order = (link_t *) (&b[1]);
377 jermar 85
 
814 palkovsky 86
    for (i = 0; i <= max_order; i++)
87
        list_initialize(&b->order[i]);
88
 
89
    b->max_order = max_order;
90
    b->op = op;
91
    b->data = data;
377 jermar 92
}
93
 
3425 svoboda 94
/** Check if buddy system can allocate block.
533 bondari 95
 *
3425 svoboda 96
 * @param b     Buddy system pointer.
97
 * @param i     Size of the block (2^i).
533 bondari 98
 *
3425 svoboda 99
 * @return      True if block can be allocated.
533 bondari 100
 */
3425 svoboda 101
bool buddy_system_can_alloc(buddy_system_t *b, uint8_t i)
102
{
1780 jermar 103
    uint8_t k;
533 bondari 104
 
735 bondari 105
    /*
106
     * If requested block is greater then maximal block
107
     * we know immediatly that we cannot satisfy the request.
108
     */
3425 svoboda 109
    if (i > b->max_order)
110
        return false;
735 bondari 111
 
112
    /*
113
     * Check if any bigger or equal order has free elements
114
     */
3425 svoboda 115
    for (k = i; k <= b->max_order; k++) {
533 bondari 116
        if (!list_empty(&b->order[k])) {
117
            return true;
118
        }
119
    }
120
 
121
    return false;
122
}
123
 
3425 svoboda 124
/** Allocate PARTICULAR block from buddy system.
814 palkovsky 125
 *
3425 svoboda 126
 * @return      Block of data or NULL if no such block was found.
814 palkovsky 127
 */
128
link_t *buddy_system_alloc_block(buddy_system_t *b, link_t *block)
129
{
130
    link_t *left,*right, *tmp;
1780 jermar 131
    uint8_t order;
814 palkovsky 132
 
133
    left = b->op->find_block(b, block, BUDDY_SYSTEM_INNER_BLOCK);
134
    ASSERT(left);
135
    list_remove(left);
136
    while (1) {
3425 svoboda 137
        if (!b->op->get_order(b, left)) {
814 palkovsky 138
            b->op->mark_busy(b, left);
139
            return left;
140
        }
141
 
142
        order = b->op->get_order(b, left);
143
 
144
        right = b->op->bisect(b, left);
3425 svoboda 145
        b->op->set_order(b, left, order - 1);
146
        b->op->set_order(b, right, order - 1);
814 palkovsky 147
 
148
        tmp = b->op->find_block(b, block, BUDDY_SYSTEM_INNER_BLOCK);
149
 
150
        if (tmp == right) {
151
            right = left;
152
            left = tmp;
822 palkovsky 153
        }
154
        ASSERT(tmp == left);
814 palkovsky 155
        b->op->mark_busy(b, left);
156
        buddy_system_free(b, right);
157
        b->op->mark_available(b, left);
158
    }
159
}
160
 
377 jermar 161
/** Allocate block from buddy system.
162
 *
3425 svoboda 163
 * @param b     Buddy system pointer.
164
 * @param i     Returned block will be 2^i big.
377 jermar 165
 *
3425 svoboda 166
 * @return      Block of data represented by link_t.
377 jermar 167
 */
1780 jermar 168
link_t *buddy_system_alloc(buddy_system_t *b, uint8_t i)
377 jermar 169
{
170
    link_t *res, *hlp;
171
 
735 bondari 172
    ASSERT(i <= b->max_order);
377 jermar 173
 
174
    /*
175
     * If the list of order i is not empty,
176
     * the request can be immediatelly satisfied.
177
     */
178
    if (!list_empty(&b->order[i])) {
179
        res = b->order[i].next;
180
        list_remove(res);
533 bondari 181
        b->op->mark_busy(b, res);
377 jermar 182
        return res;
183
    }
184
    /*
185
     * If order i is already the maximal order,
186
     * the request cannot be satisfied.
187
     */
735 bondari 188
    if (i == b->max_order)
377 jermar 189
        return NULL;
190
 
191
    /*
192
     * Try to recursively satisfy the request from higher order lists.
193
     */
194
    hlp = buddy_system_alloc(b, i + 1);
195
 
196
    /*
197
     * The request could not be satisfied
198
     * from higher order lists.
199
     */
200
    if (!hlp)
201
        return NULL;
202
 
203
    res = hlp;
204
 
205
    /*
206
     * Bisect the block and set order of both of its parts to i.
207
     */
489 jermar 208
    hlp = b->op->bisect(b, res);
209
    b->op->set_order(b, res, i);
210
    b->op->set_order(b, hlp, i);
377 jermar 211
 
212
    /*
814 palkovsky 213
     * Return the other half to buddy system. Mark the first part
815 jermar 214
     * full, so that it won't coalesce again.
377 jermar 215
     */
533 bondari 216
    b->op->mark_busy(b, res);
377 jermar 217
    buddy_system_free(b, hlp);
218
 
219
    return res;
220
}
221
 
222
/** Return block to buddy system.
223
 *
3425 svoboda 224
 * @param b     Buddy system pointer.
225
 * @param block     Block to return.
377 jermar 226
 */
227
void buddy_system_free(buddy_system_t *b, link_t *block)
228
{
229
    link_t *buddy, *hlp;
1780 jermar 230
    uint8_t i;
533 bondari 231
 
377 jermar 232
    /*
233
     * Determine block's order.
234
     */
489 jermar 235
    i = b->op->get_order(b, block);
377 jermar 236
 
735 bondari 237
    ASSERT(i <= b->max_order);
377 jermar 238
 
735 bondari 239
    if (i != b->max_order) {
377 jermar 240
        /*
379 jermar 241
         * See if there is any buddy in the list of order i.
377 jermar 242
         */
489 jermar 243
        buddy = b->op->find_buddy(b, block);
379 jermar 244
        if (buddy) {
245
 
489 jermar 246
            ASSERT(b->op->get_order(b, buddy) == i);
379 jermar 247
            /*
248
             * Remove buddy from the list of order i.
249
             */
250
            list_remove(buddy);
378 jermar 251
 
379 jermar 252
            /*
253
             * Invalidate order of both block and buddy.
254
             */
489 jermar 255
            b->op->set_order(b, block, BUDDY_SYSTEM_INNER_BLOCK);
256
            b->op->set_order(b, buddy, BUDDY_SYSTEM_INNER_BLOCK);
379 jermar 257
 
258
            /*
259
             * Coalesce block and buddy into one block.
260
             */
489 jermar 261
            hlp = b->op->coalesce(b, block, buddy);
377 jermar 262
 
379 jermar 263
            /*
264
             * Set order of the coalesced block to i + 1.
265
             */
489 jermar 266
            b->op->set_order(b, hlp, i + 1);
377 jermar 267
 
379 jermar 268
            /*
3425 svoboda 269
             * Recursively add the coalesced block to the list of
270
             * order i + 1.
379 jermar 271
             */
272
            buddy_system_free(b, hlp);
273
            return;
274
        }
377 jermar 275
    }
276
 
379 jermar 277
    /*
278
     * Insert block into the list of order i.
279
     */
280
    list_append(block, &b->order[i]);
377 jermar 281
}
683 bondari 282
 
1757 jermar 283
/** @}
1702 cejka 284
 */