Subversion Repositories HelenOS-historic

Rev

Rev 532 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 532 Rev 533
Line 31... Line 31...
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
#include <print.h>
36
 
37
 
37
/** Create buddy system
38
/** Create buddy system
38
 *
39
 *
39
 * Allocate memory for and initialize new buddy system.
40
 * Allocate memory for and initialize new buddy system.
40
 *
41
 *
Line 54... Line 55...
54
    ASSERT(op->find_buddy);
55
    ASSERT(op->find_buddy);
55
    ASSERT(op->set_order);
56
    ASSERT(op->set_order);
56
    ASSERT(op->get_order);
57
    ASSERT(op->get_order);
57
    ASSERT(op->bisect);
58
    ASSERT(op->bisect);
58
    ASSERT(op->coalesce);
59
    ASSERT(op->coalesce);
-
 
60
    ASSERT(op->mark_busy);
59
 
61
 
60
    /*
62
    /*
61
     * Allocate memory for structure describing the whole buddy system.
63
     * Allocate memory for structure describing the whole buddy system.
62
     */
64
     */
63
    b = (buddy_system_t *) early_malloc(sizeof(buddy_system_t));
65
    b = (buddy_system_t *) early_malloc(sizeof(buddy_system_t));
Line 81... Line 83...
81
    }
83
    }
82
   
84
   
83
    return b;
85
    return b;
84
}
86
}
85
 
87
 
-
 
88
/** Check if buddy system can allocate block
-
 
89
 *
-
 
90
 * @param b Buddy system pointer
-
 
91
 * @param i Size of the block (2^i)
-
 
92
 *
-
 
93
 * @return True if block can be allocated
-
 
94
 */
-
 
95
bool buddy_system_can_alloc(buddy_system_t *b, __u8 i) {
-
 
96
    __u8 k;
-
 
97
   
-
 
98
    ASSERT(i < b->max_order);
-
 
99
   
-
 
100
    for (k=i; k < b->max_order; k++) {
-
 
101
        if (!list_empty(&b->order[k])) {
-
 
102
            return true;
-
 
103
        }
-
 
104
    }
-
 
105
   
-
 
106
    return false;
-
 
107
   
-
 
108
}
-
 
109
 
86
/** Allocate block from buddy system.
110
/** Allocate block from buddy system.
87
 *
111
 *
88
 * @param b Buddy system pointer.
112
 * @param b Buddy system pointer.
89
 * @param i Returned block will be 2^i big.
113
 * @param i Returned block will be 2^i big.
90
 *
114
 *
Line 101... Line 125...
101
     * the request can be immediatelly satisfied.
125
     * the request can be immediatelly satisfied.
102
     */
126
     */
103
    if (!list_empty(&b->order[i])) {
127
    if (!list_empty(&b->order[i])) {
104
        res = b->order[i].next;
128
        res = b->order[i].next;
105
        list_remove(res);
129
        list_remove(res);
-
 
130
        b->op->mark_busy(b, res);
106
        return res;
131
        return res;
107
    }
132
    }
108
   
133
   
109
    /*
134
    /*
110
     * If order i is already the maximal order,
135
     * If order i is already the maximal order,
Line 134... Line 159...
134
    b->op->set_order(b, res, i);
159
    b->op->set_order(b, res, i);
135
    b->op->set_order(b, hlp, i);
160
    b->op->set_order(b, hlp, i);
136
   
161
   
137
    /*
162
    /*
138
     * Return the other half to buddy system.
163
     * Return the other half to buddy system.
-
 
164
     * PROBLEM!!!! FILL FIND OTHER PART AS BUDDY AND LINK TOGETHER
139
     */
165
     */
-
 
166
    b->op->mark_busy(b, res);
140
    buddy_system_free(b, hlp);
167
    buddy_system_free(b, hlp);
141
   
168
   
142
    return res;
169
    return res;
143
   
170
   
144
}
171
}
Line 150... Line 177...
150
 */
177
 */
151
void buddy_system_free(buddy_system_t *b, link_t *block)
178
void buddy_system_free(buddy_system_t *b, link_t *block)
152
{
179
{
153
    link_t *buddy, *hlp;
180
    link_t *buddy, *hlp;
154
    __u8 i;
181
    __u8 i;
155
   
182
 
156
    /*
183
    /*
157
     * Determine block's order.
184
     * Determine block's order.
158
     */
185
     */
159
    i = b->op->get_order(b, block);
186
    i = b->op->get_order(b, block);
160
 
187
 
Line 166... Line 193...
166
         */
193
         */
167
        buddy = b->op->find_buddy(b, block);
194
        buddy = b->op->find_buddy(b, block);
168
        if (buddy) {
195
        if (buddy) {
169
 
196
 
170
            ASSERT(b->op->get_order(b, buddy) == i);
197
            ASSERT(b->op->get_order(b, buddy) == i);
171
       
-
 
172
            /*
198
            /*
173
             * Remove buddy from the list of order i.
199
             * Remove buddy from the list of order i.
174
             */
200
             */
175
            list_remove(buddy);
201
            list_remove(buddy);
176
       
202