Subversion Repositories HelenOS-historic

Rev

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

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