alloc.c revision 1.3.4.2 1 1.3.4.2 nathanw /* $NetBSD: alloc.c,v 1.3.4.2 2002/10/18 02:38:41 nathanw Exp $ */
2 1.3.4.2 nathanw
3 1.3.4.2 nathanw /*-
4 1.3.4.2 nathanw * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 1.3.4.2 nathanw * All rights reserved.
6 1.3.4.2 nathanw *
7 1.3.4.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.3.4.2 nathanw * by Jason R. Thorpe.
9 1.3.4.2 nathanw *
10 1.3.4.2 nathanw * Redistribution and use in source and binary forms, with or without
11 1.3.4.2 nathanw * modification, are permitted provided that the following conditions
12 1.3.4.2 nathanw * are met:
13 1.3.4.2 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.3.4.2 nathanw * notice, this list of conditions and the following disclaimer.
15 1.3.4.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.4.2 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.3.4.2 nathanw * documentation and/or other materials provided with the distribution.
18 1.3.4.2 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.3.4.2 nathanw * must display the following acknowledgement:
20 1.3.4.2 nathanw * This product includes software developed by the NetBSD
21 1.3.4.2 nathanw * Foundation, Inc. and its contributors.
22 1.3.4.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3.4.2 nathanw * contributors may be used to endorse or promote products derived
24 1.3.4.2 nathanw * from this software without specific prior written permission.
25 1.3.4.2 nathanw *
26 1.3.4.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3.4.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3.4.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3.4.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3.4.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3.4.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3.4.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3.4.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3.4.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3.4.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.3.4.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.3.4.2 nathanw */
38 1.3.4.2 nathanw
39 1.3.4.2 nathanw /*
40 1.3.4.2 nathanw * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
41 1.3.4.2 nathanw * Copyright (c) 1996
42 1.3.4.2 nathanw * Matthias Drochner. All rights reserved.
43 1.3.4.2 nathanw * Copyright (C) 1995, 1996 Wolfgang Solfrank.
44 1.3.4.2 nathanw * Copyright (C) 1995, 1996 TooLs GmbH.
45 1.3.4.2 nathanw * All rights reserved.
46 1.3.4.2 nathanw *
47 1.3.4.2 nathanw * Redistribution and use in source and binary forms, with or without
48 1.3.4.2 nathanw * modification, are permitted provided that the following conditions
49 1.3.4.2 nathanw * are met:
50 1.3.4.2 nathanw * 1. Redistributions of source code must retain the above copyright
51 1.3.4.2 nathanw * notice, this list of conditions and the following disclaimer.
52 1.3.4.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
53 1.3.4.2 nathanw * notice, this list of conditions and the following disclaimer in the
54 1.3.4.2 nathanw * documentation and/or other materials provided with the distribution.
55 1.3.4.2 nathanw * 3. All advertising materials mentioning features or use of this software
56 1.3.4.2 nathanw * must display the following acknowledgement:
57 1.3.4.2 nathanw * This product includes software developed by TooLs GmbH.
58 1.3.4.2 nathanw * 4. The name of TooLs GmbH may not be used to endorse or promote products
59 1.3.4.2 nathanw * derived from this software without specific prior written permission.
60 1.3.4.2 nathanw *
61 1.3.4.2 nathanw * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
62 1.3.4.2 nathanw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
63 1.3.4.2 nathanw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
64 1.3.4.2 nathanw * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
65 1.3.4.2 nathanw * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
66 1.3.4.2 nathanw * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
67 1.3.4.2 nathanw * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
68 1.3.4.2 nathanw * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
69 1.3.4.2 nathanw * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
70 1.3.4.2 nathanw * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71 1.3.4.2 nathanw */
72 1.3.4.2 nathanw
73 1.3.4.2 nathanw /*
74 1.3.4.2 nathanw * Dynamic memory allocator suitable for use with OpenFirmware.
75 1.3.4.2 nathanw *
76 1.3.4.2 nathanw * Compile options:
77 1.3.4.2 nathanw *
78 1.3.4.2 nathanw * ALLOC_TRACE enable tracing of allocations/deallocations
79 1.3.4.2 nathanw *
80 1.3.4.2 nathanw * ALLOC_FIRST_FIT use a first-fit allocation algorithm, rather than
81 1.3.4.2 nathanw * the default best-fit algorithm.
82 1.3.4.2 nathanw *
83 1.3.4.2 nathanw * DEBUG enable debugging sanity checks.
84 1.3.4.2 nathanw */
85 1.3.4.2 nathanw
86 1.3.4.2 nathanw #include <sys/param.h>
87 1.3.4.2 nathanw #include <sys/queue.h>
88 1.3.4.2 nathanw
89 1.3.4.2 nathanw #include <lib/libsa/stand.h>
90 1.3.4.2 nathanw
91 1.3.4.2 nathanw #include "openfirm.h"
92 1.3.4.2 nathanw
93 1.3.4.2 nathanw /*
94 1.3.4.2 nathanw * Each block actually has ALIGN(struct ml) + ALIGN(size) bytes allocated
95 1.3.4.2 nathanw * to it, as follows:
96 1.3.4.2 nathanw *
97 1.3.4.2 nathanw * 0 ... (sizeof(struct ml) - 1)
98 1.3.4.2 nathanw * allocated or unallocated: holds size of user-data part of block.
99 1.3.4.2 nathanw *
100 1.3.4.2 nathanw * sizeof(struct ml) ... (ALIGN(sizeof(struct ml)) - 1)
101 1.3.4.2 nathanw * allocated: unused
102 1.3.4.2 nathanw * unallocated: depends on packing of struct fl
103 1.3.4.2 nathanw *
104 1.3.4.2 nathanw * ALIGN(sizeof(struct ml)) ... (ALIGN(sizeof(struct ml)) +
105 1.3.4.2 nathanw * ALIGN(data size) - 1)
106 1.3.4.2 nathanw * allocated: user data
107 1.3.4.2 nathanw * unallocated: depends on packing of struct fl
108 1.3.4.2 nathanw *
109 1.3.4.2 nathanw * 'next' is only used when the block is unallocated (i.e. on the free list).
110 1.3.4.2 nathanw * However, note that ALIGN(sizeof(struct ml)) + ALIGN(data size) must
111 1.3.4.2 nathanw * be at least 'sizeof(struct fl)', so that blocks can be used as structures
112 1.3.4.2 nathanw * when on the free list.
113 1.3.4.2 nathanw */
114 1.3.4.2 nathanw
115 1.3.4.2 nathanw /*
116 1.3.4.2 nathanw * Memory lists.
117 1.3.4.2 nathanw */
118 1.3.4.2 nathanw struct ml {
119 1.3.4.2 nathanw unsigned size;
120 1.3.4.2 nathanw LIST_ENTRY(ml) list;
121 1.3.4.2 nathanw };
122 1.3.4.2 nathanw
123 1.3.4.2 nathanw LIST_HEAD(, ml) freelist = LIST_HEAD_INITIALIZER(freelist);
124 1.3.4.2 nathanw LIST_HEAD(, ml) allocatedlist = LIST_HEAD_INITIALIZER(allocatedlist);
125 1.3.4.2 nathanw
126 1.3.4.2 nathanw #define OVERHEAD ALIGN(sizeof (struct ml)) /* shorthand */
127 1.3.4.2 nathanw
128 1.3.4.2 nathanw void *
129 1.3.4.2 nathanw alloc(size)
130 1.3.4.2 nathanw unsigned size;
131 1.3.4.2 nathanw {
132 1.3.4.2 nathanw struct ml *f, *bestf;
133 1.3.4.2 nathanw unsigned bestsize = 0xffffffff; /* greater than any real size */
134 1.3.4.2 nathanw char *help;
135 1.3.4.2 nathanw int failed;
136 1.3.4.2 nathanw
137 1.3.4.2 nathanw #ifdef ALLOC_TRACE
138 1.3.4.2 nathanw printf("alloc(%u)", size);
139 1.3.4.2 nathanw #endif
140 1.3.4.2 nathanw
141 1.3.4.2 nathanw /*
142 1.3.4.2 nathanw * Account for overhead now, so that we don't get an
143 1.3.4.2 nathanw * "exact fit" which doesn't have enough space.
144 1.3.4.2 nathanw */
145 1.3.4.2 nathanw size = ALIGN(size) + OVERHEAD;
146 1.3.4.2 nathanw
147 1.3.4.2 nathanw #ifdef ALLOC_FIRST_FIT
148 1.3.4.2 nathanw /* scan freelist */
149 1.3.4.2 nathanw for (f = freelist.lh_first; f != NULL && f->size < size;
150 1.3.4.2 nathanw f = f->list.le_next)
151 1.3.4.2 nathanw /* noop */ ;
152 1.3.4.2 nathanw bestf = f;
153 1.3.4.2 nathanw failed = (bestf == (struct fl *)0);
154 1.3.4.2 nathanw #else
155 1.3.4.2 nathanw /* scan freelist */
156 1.3.4.2 nathanw f = freelist.lh_first;
157 1.3.4.2 nathanw while (f != NULL) {
158 1.3.4.2 nathanw if (f->size >= size) {
159 1.3.4.2 nathanw if (f->size == size) /* exact match */
160 1.3.4.2 nathanw goto found;
161 1.3.4.2 nathanw
162 1.3.4.2 nathanw if (f->size < bestsize) {
163 1.3.4.2 nathanw /* keep best fit */
164 1.3.4.2 nathanw bestf = f;
165 1.3.4.2 nathanw bestsize = f->size;
166 1.3.4.2 nathanw }
167 1.3.4.2 nathanw }
168 1.3.4.2 nathanw f = f->list.le_next;
169 1.3.4.2 nathanw }
170 1.3.4.2 nathanw
171 1.3.4.2 nathanw /* no match in freelist if bestsize unchanged */
172 1.3.4.2 nathanw failed = (bestsize == 0xffffffff);
173 1.3.4.2 nathanw #endif
174 1.3.4.2 nathanw
175 1.3.4.2 nathanw if (failed) { /* nothing found */
176 1.3.4.2 nathanw /*
177 1.3.4.2 nathanw * Allocate memory from the OpenFirmware, rounded
178 1.3.4.2 nathanw * to page size, and record the chunk size.
179 1.3.4.2 nathanw */
180 1.3.4.2 nathanw size = roundup(size, NBPG);
181 1.3.4.2 nathanw help = OF_claim(0, size, NBPG);
182 1.3.4.2 nathanw if (help == (char *)-1)
183 1.3.4.2 nathanw panic("alloc: out of memory");
184 1.3.4.2 nathanw
185 1.3.4.2 nathanw f = (struct ml *)help;
186 1.3.4.2 nathanw f->size = size;
187 1.3.4.2 nathanw #ifdef ALLOC_TRACE
188 1.3.4.2 nathanw printf("=%lx (new chunk size %u)\n",
189 1.3.4.2 nathanw (u_long)(help + OVERHEAD), f->f_size);
190 1.3.4.2 nathanw #endif
191 1.3.4.2 nathanw goto out;
192 1.3.4.2 nathanw }
193 1.3.4.2 nathanw
194 1.3.4.2 nathanw /* we take the best fit */
195 1.3.4.2 nathanw f = bestf;
196 1.3.4.2 nathanw
197 1.3.4.2 nathanw found:
198 1.3.4.2 nathanw /* remove from freelist */
199 1.3.4.2 nathanw LIST_REMOVE(f, list);
200 1.3.4.2 nathanw help = (char *)f;
201 1.3.4.2 nathanw #ifdef ALLOC_TRACE
202 1.3.4.2 nathanw printf("=%lx (origsize %u)\n", (u_long)(help + OVERHEAD), f->size);
203 1.3.4.2 nathanw #endif
204 1.3.4.2 nathanw out:
205 1.3.4.2 nathanw /* place on allocated list */
206 1.3.4.2 nathanw LIST_INSERT_HEAD(&allocatedlist, f, list);
207 1.3.4.2 nathanw return (help + OVERHEAD);
208 1.3.4.2 nathanw }
209 1.3.4.2 nathanw
210 1.3.4.2 nathanw void
211 1.3.4.2 nathanw free(ptr, size)
212 1.3.4.2 nathanw void *ptr;
213 1.3.4.2 nathanw unsigned size; /* only for consistenct check */
214 1.3.4.2 nathanw {
215 1.3.4.2 nathanw register struct ml *a = (struct ml *)((char*)ptr - OVERHEAD);
216 1.3.4.2 nathanw
217 1.3.4.2 nathanw #ifdef ALLOC_TRACE
218 1.3.4.2 nathanw printf("free(%lx, %u) (origsize %u)\n", (u_long)ptr, size, a->size);
219 1.3.4.2 nathanw #endif
220 1.3.4.2 nathanw #ifdef DEBUG
221 1.3.4.2 nathanw if (size > a->size)
222 1.3.4.2 nathanw printf("free %u bytes @%lx, should be <=%u\n",
223 1.3.4.2 nathanw size, (u_long)ptr, a->size);
224 1.3.4.2 nathanw #endif
225 1.3.4.2 nathanw
226 1.3.4.2 nathanw /* Remove from allocated list, place on freelist. */
227 1.3.4.2 nathanw LIST_REMOVE(a, list);
228 1.3.4.2 nathanw LIST_INSERT_HEAD(&freelist, a, list);
229 1.3.4.2 nathanw }
230 1.3.4.2 nathanw
231 1.3.4.2 nathanw void
232 1.3.4.2 nathanw freeall()
233 1.3.4.2 nathanw {
234 1.3.4.2 nathanw #ifdef __notyet__ /* Firmware bug ?! */
235 1.3.4.2 nathanw struct ml *m;
236 1.3.4.2 nathanw
237 1.3.4.2 nathanw /* Release chunks on freelist... */
238 1.3.4.2 nathanw while ((m = freelist.lh_first) != NULL) {
239 1.3.4.2 nathanw LIST_REMOVE(m, list);
240 1.3.4.2 nathanw OF_release(m, m->size);
241 1.3.4.2 nathanw }
242 1.3.4.2 nathanw
243 1.3.4.2 nathanw /* ...and allocated list. */
244 1.3.4.2 nathanw while ((m = allocatedlist.lh_first) != NULL) {
245 1.3.4.2 nathanw LIST_REMOVE(m, list);
246 1.3.4.2 nathanw OF_release(m, m->size);
247 1.3.4.2 nathanw }
248 1.3.4.2 nathanw #endif /* __notyet__ */
249 1.3.4.2 nathanw }
250