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