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