Subversion Repositories HelenOS-historic

Rev

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

Rev 374 Rev 413
Line 58... Line 58...
58
/*
58
/*
59
 * Uses first-fit algorithm.
59
 * Uses first-fit algorithm.
60
 */
60
 */
61
void *early_malloc(size_t size)
61
void *early_malloc(size_t size)
62
{
62
{
63
    pri_t pri;
63
    ipl_t ipl;
64
    chunk_t *x, *y, *z;
64
    chunk_t *x, *y, *z;
65
 
65
 
66
    if (size == 0)
66
    if (size == 0)
67
        panic("zero-size allocation request");
67
        panic("zero-size allocation request");
68
       
68
       
69
    x = chunk0;
69
    x = chunk0;
70
    pri = cpu_priority_high();
70
    ipl = interrupts_disable();
71
    spinlock_lock(&heaplock);      
71
    spinlock_lock(&heaplock);      
72
    while (x) {
72
    while (x) {
73
        if (x->used || x->size < size) {
73
        if (x->used || x->size < size) {
74
            x = x->next;
74
            x = x->next;
75
            continue;
75
            continue;
Line 82... Line 82...
82
         * it would not provide enough space for storing a new chunk
82
         * it would not provide enough space for storing a new chunk
83
         * header plus at least one byte of data, we are finished.
83
         * header plus at least one byte of data, we are finished.
84
         */
84
         */
85
        if (x->size < size + sizeof(chunk_t) + 1) {
85
        if (x->size < size + sizeof(chunk_t) + 1) {
86
            spinlock_unlock(&heaplock);
86
            spinlock_unlock(&heaplock);
87
            cpu_priority_restore(pri);
87
            interrupts_restore(ipl);
88
            return &x->data[0];
88
            return &x->data[0];
89
        }
89
        }
90
 
90
 
91
        /*
91
        /*
92
         * Truncate x and create a new chunk.
92
         * Truncate x and create a new chunk.
Line 103... Line 103...
103
        }
103
        }
104
       
104
       
105
        x->size = size;
105
        x->size = size;
106
        x->next = y;
106
        x->next = y;
107
        spinlock_unlock(&heaplock);
107
        spinlock_unlock(&heaplock);
108
        cpu_priority_restore(pri);
108
        interrupts_restore(ipl);
109
 
109
 
110
        return &x->data[0];
110
        return &x->data[0];
111
    }
111
    }
112
    spinlock_unlock(&heaplock);
112
    spinlock_unlock(&heaplock);
113
    cpu_priority_restore(pri);
113
    interrupts_restore(ipl);
114
    return NULL;
114
    return NULL;
115
}
115
}
116
 
116
 
117
void early_free(void *ptr)
117
void early_free(void *ptr)
118
{
118
{
119
    pri_t pri;
119
    ipl_t ipl;
120
    chunk_t *x, *y, *z;
120
    chunk_t *x, *y, *z;
121
 
121
 
122
    if (!ptr)
122
    if (!ptr)
123
        panic("free on NULL");
123
        panic("free on NULL");
124
 
124
 
125
 
125
 
126
    y = (chunk_t *) (((__u8 *) ptr) - sizeof(chunk_t));
126
    y = (chunk_t *) (((__u8 *) ptr) - sizeof(chunk_t));
127
    if (y->used != 1)
127
    if (y->used != 1)
128
        panic("freeing unused/damaged chunk");
128
        panic("freeing unused/damaged chunk");
129
 
129
 
130
    pri = cpu_priority_high();
130
    ipl = interrupts_disable();
131
    spinlock_lock(&heaplock);
131
    spinlock_lock(&heaplock);
132
    x = y->prev;
132
    x = y->prev;
133
    z = y->next;
133
    z = y->next;
134
    /* merge x and y */
134
    /* merge x and y */
135
    if (x && !x->used) {
135
    if (x && !x->used) {
Line 148... Line 148...
148
            z->next->prev = y;
148
            z->next->prev = y;
149
        }
149
        }
150
    }
150
    }
151
    y->used = 0;
151
    y->used = 0;
152
    spinlock_unlock(&heaplock);
152
    spinlock_unlock(&heaplock);
153
    cpu_priority_restore(pri);
153
    interrupts_restore(ipl);
154
}
154
}