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