Subversion Repositories HelenOS-historic

Rev

Rev 102 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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