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