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