Subversion Repositories HelenOS-historic

Rev

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

Rev 615 Rev 623
1
/*
1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
2
 * Copyright (C) 2001-2004 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/heap.h>
29
#include <mm/heap.h>
30
#include <synch/spinlock.h>
30
#include <synch/spinlock.h>
31
#include <func.h>
31
#include <func.h>
32
#include <memstr.h>
32
#include <memstr.h>
33
#include <panic.h>
33
#include <panic.h>
34
#include <arch/types.h>
34
#include <arch/types.h>
35
#include <arch/asm.h>
35
#include <arch/asm.h>
36
#include <arch.h>
36
#include <arch.h>
37
#include <align.h>
37
#include <align.h>
38
 
38
 
39
/*
39
/*
40
 * First-fit algorithm.
40
 * First-fit algorithm.
41
 * Simple, but hopefully correct.
41
 * Simple, but hopefully correct.
42
 * Chunks being freed are tested for mergability with their neighbours.
42
 * Chunks being freed are tested for mergability with their neighbours.
43
 */
43
 */
44
 
44
 
45
static chunk_t *chunk0;
45
static chunk_t *chunk0;
46
static spinlock_t heaplock;
46
SPINLOCK_INITIALIZE(heaplock);
47
 
47
 
48
void early_heap_init(__address heap, size_t size)
48
void early_heap_init(__address heap, size_t size)
49
{
49
{
50
    spinlock_initialize(&heaplock, "heap_lock");
-
 
51
    memsetb(heap, size, 0);
50
    memsetb(heap, size, 0);
52
    chunk0 = (chunk_t *) heap;
51
    chunk0 = (chunk_t *) heap;
53
    chunk0->used = 0;
52
    chunk0->used = 0;
54
    chunk0->size = size - sizeof(chunk_t);
53
    chunk0->size = size - sizeof(chunk_t);
55
    chunk0->next = NULL;
54
    chunk0->next = NULL;
56
    chunk0->prev = NULL;
55
    chunk0->prev = NULL;
57
}
56
}
58
 
57
 
59
/*
58
/*
60
 * Uses first-fit algorithm.
59
 * Uses first-fit algorithm.
61
 */
60
 */
62
void *early_malloc(size_t size)
61
void *early_malloc(size_t size)
63
{
62
{
64
    ipl_t ipl;
63
    ipl_t ipl;
65
    chunk_t *x, *y, *z;
64
    chunk_t *x, *y, *z;
66
 
65
 
67
    if (size == 0)
66
    if (size == 0)
68
        panic("zero-size allocation request");
67
        panic("zero-size allocation request");
69
       
68
       
70
    size = ALIGN_UP(size, sizeof(__native));
69
    size = ALIGN_UP(size, sizeof(__native));
71
 
70
 
72
    x = chunk0;
71
    x = chunk0;
73
    ipl = interrupts_disable();
72
    ipl = interrupts_disable();
74
    spinlock_lock(&heaplock);      
73
    spinlock_lock(&heaplock);      
75
    while (x) {
74
    while (x) {
76
        if (x->used || x->size < size) {
75
        if (x->used || x->size < size) {
77
            x = x->next;
76
            x = x->next;
78
            continue;
77
            continue;
79
        }
78
        }
80
       
79
       
81
        x->used = 1;
80
        x->used = 1;
82
   
81
   
83
        /*
82
        /*
84
         * If the chunk exactly matches required size or if truncating
83
         * If the chunk exactly matches required size or if truncating
85
         * it would not provide enough space for storing a new chunk
84
         * it would not provide enough space for storing a new chunk
86
         * header plus at least one byte of data, we are finished.
85
         * header plus at least one byte of data, we are finished.
87
         */
86
         */
88
        if (x->size < size + sizeof(chunk_t) + 1) {
87
        if (x->size < size + sizeof(chunk_t) + 1) {
89
            spinlock_unlock(&heaplock);
88
            spinlock_unlock(&heaplock);
90
            interrupts_restore(ipl);
89
            interrupts_restore(ipl);
91
            return &x->data[0];
90
            return &x->data[0];
92
        }
91
        }
93
 
92
 
94
        /*
93
        /*
95
         * Truncate x and create a new chunk.
94
         * Truncate x and create a new chunk.
96
         */
95
         */
97
        y = (chunk_t *) (((__address) x) + size + sizeof(chunk_t));
96
        y = (chunk_t *) (((__address) x) + size + sizeof(chunk_t));
98
        y->used = 0;
97
        y->used = 0;
99
        y->size = x->size - size - sizeof(chunk_t);
98
        y->size = x->size - size - sizeof(chunk_t);
100
        y->prev = x;
99
        y->prev = x;
101
        y->next = NULL;
100
        y->next = NULL;
102
       
101
       
103
        z = x->next;
102
        z = x->next;
104
        if (z) {
103
        if (z) {
105
            z->prev = y;
104
            z->prev = y;
106
            y->next = z;
105
            y->next = z;
107
        }
106
        }
108
       
107
       
109
        x->size = size;
108
        x->size = size;
110
        x->next = y;
109
        x->next = y;
111
        spinlock_unlock(&heaplock);
110
        spinlock_unlock(&heaplock);
112
        interrupts_restore(ipl);
111
        interrupts_restore(ipl);
113
 
112
 
114
        return &x->data[0];
113
        return &x->data[0];
115
    }
114
    }
116
    spinlock_unlock(&heaplock);
115
    spinlock_unlock(&heaplock);
117
    interrupts_restore(ipl);
116
    interrupts_restore(ipl);
118
    return NULL;
117
    return NULL;
119
}
118
}
120
 
119
 
121
void early_free(void *ptr)
120
void early_free(void *ptr)
122
{
121
{
123
    ipl_t ipl;
122
    ipl_t ipl;
124
    chunk_t *x, *y, *z;
123
    chunk_t *x, *y, *z;
125
 
124
 
126
    if (!ptr)
125
    if (!ptr)
127
        panic("free on NULL");
126
        panic("free on NULL");
128
 
127
 
129
 
128
 
130
    y = (chunk_t *) (((__u8 *) ptr) - sizeof(chunk_t));
129
    y = (chunk_t *) (((__u8 *) ptr) - sizeof(chunk_t));
131
    if (y->used != 1)
130
    if (y->used != 1)
132
        panic("freeing unused/damaged chunk");
131
        panic("freeing unused/damaged chunk");
133
 
132
 
134
    ipl = interrupts_disable();
133
    ipl = interrupts_disable();
135
    spinlock_lock(&heaplock);
134
    spinlock_lock(&heaplock);
136
    x = y->prev;
135
    x = y->prev;
137
    z = y->next;
136
    z = y->next;
138
    /* merge x and y */
137
    /* merge x and y */
139
    if (x && !x->used) {
138
    if (x && !x->used) {
140
        x->size += y->size + sizeof(chunk_t);
139
        x->size += y->size + sizeof(chunk_t);
141
        x->next = z;
140
        x->next = z;
142
        if (z)
141
        if (z)
143
            z->prev = x;
142
            z->prev = x;
144
        y = x;
143
        y = x;
145
    }
144
    }
146
    /* merge y and z or merge (x merged with y) and z */
145
    /* merge y and z or merge (x merged with y) and z */
147
    if (z && !z->used) {
146
    if (z && !z->used) {
148
        y->size += z->size + sizeof(chunk_t);
147
        y->size += z->size + sizeof(chunk_t);
149
        y->next = z->next;
148
        y->next = z->next;
150
        if (z->next)  {
149
        if (z->next)  {
151
            /* y is either y or x */
150
            /* y is either y or x */
152
            z->next->prev = y;
151
            z->next->prev = y;
153
        }
152
        }
154
    }
153
    }
155
    y->used = 0;
154
    y->used = 0;
156
    spinlock_unlock(&heaplock);
155
    spinlock_unlock(&heaplock);
157
    interrupts_restore(ipl);
156
    interrupts_restore(ipl);
158
}
157
}
159
 
158