alloc.c revision 1.22 1 /* $NetBSD: alloc.c,v 1.22 2007/11/24 13:20:53 isaki Exp $ */
2
3 /*
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
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 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)alloc.c 8.1 (Berkeley) 6/11/93
35 *
36 *
37 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
38 * Copyright (c) 1996
39 * Matthias Drochner. All rights reserved.
40 *
41 * This code is derived from software contributed to Berkeley by
42 * The Mach Operating System project at Carnegie-Mellon University.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)alloc.c 8.1 (Berkeley) 6/11/93
73 *
74 *
75 * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
76 * All Rights Reserved.
77 *
78 * Author: Alessandro Forin
79 *
80 * Permission to use, copy, modify and distribute this software and its
81 * documentation is hereby granted, provided that both the copyright
82 * notice and this permission notice appear in all copies of the
83 * software, derivative works or modified versions, and any portions
84 * thereof, and that both notices appear in supporting documentation.
85 *
86 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
87 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
88 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
89 *
90 * Carnegie Mellon requests users of this software to return to
91 *
92 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
93 * School of Computer Science
94 * Carnegie Mellon University
95 * Pittsburgh PA 15213-3890
96 *
97 * any improvements or extensions that they make and grant Carnegie the
98 * rights to redistribute these changes.
99 */
100
101 /*
102 * Dynamic memory allocator.
103 *
104 * Compile options:
105 *
106 * ALLOC_TRACE enable tracing of allocations/deallocations
107
108 * ALLOC_FIRST_FIT use a first-fit allocation algorithm, rather than
109 * the default best-fit algorithm.
110 *
111 * HEAP_LIMIT heap limit address (defaults to "no limit").
112 *
113 * HEAP_START start address of heap (defaults to '&end').
114 *
115 * DEBUG enable debugging sanity checks.
116 */
117
118 #include <sys/param.h>
119 #include "stand.h"
120
121 /*
122 * Each block actually has ALIGN(unsigned) + ALIGN(size) bytes allocated
123 * to it, as follows:
124 *
125 * 0 ... (sizeof(unsigned) - 1)
126 * allocated or unallocated: holds size of user-data part of block.
127 *
128 * sizeof(unsigned) ... (ALIGN(sizeof(unsigned)) - 1)
129 * allocated: unused
130 * unallocated: depends on packing of struct fl
131 *
132 * ALIGN(sizeof(unsigned)) ... (ALIGN(sizeof(unsigned)) + ALIGN(data size) - 1)
133 * allocated: user data
134 * unallocated: depends on packing of struct fl
135 *
136 * 'next' is only used when the block is unallocated (i.e. on the free list).
137 * However, note that ALIGN(sizeof(unsigned)) + ALIGN(data size) must
138 * be at least 'sizeof(struct fl)', so that blocks can be used as structures
139 * when on the free list.
140 */
141 struct fl {
142 unsigned size;
143 struct fl *next;
144 } *freelist;
145
146 #ifdef HEAP_VARIABLE
147 static char *top, *heapstart, *heaplimit;
148 void
149 setheap(void *start, void *limit)
150 {
151 heapstart = top = start;
152 heaplimit = limit;
153 }
154 #define HEAP_START heapstart
155 #define HEAP_LIMIT heaplimit
156 #else /* !HEAP_VARIABLE */
157 #ifndef HEAP_START
158 extern char end[];
159 #define HEAP_START end
160 #endif
161 static char *top = (char *)HEAP_START;
162 #endif /* HEAP_VARIABLE */
163
164 void *
165 alloc(size_t size)
166 {
167 struct fl **f = &freelist, **bestf = NULL;
168 #ifndef ALLOC_FIRST_FIT
169 unsigned bestsize = 0xffffffff; /* greater than any real size */
170 #endif
171 char *help;
172 int failed;
173
174 #ifdef ALLOC_TRACE
175 printf("alloc(%zu)", size);
176 #endif
177
178 #ifdef ALLOC_FIRST_FIT
179 while (*f != (struct fl *)0 && (size_t)(*f)->size < size)
180 f = &((*f)->next);
181 bestf = f;
182 failed = (*bestf == (struct fl *)0);
183 #else
184 /* scan freelist */
185 while (*f) {
186 if ((size_t)(*f)->size >= size) {
187 if ((size_t)(*f)->size == size) /* exact match */
188 goto found;
189
190 if ((*f)->size < bestsize) {
191 /* keep best fit */
192 bestf = f;
193 bestsize = (*f)->size;
194 }
195 }
196 f = &((*f)->next);
197 }
198
199 /* no match in freelist if bestsize unchanged */
200 failed = (bestsize == 0xffffffff);
201 #endif
202
203 if (failed) { /* nothing found */
204 /*
205 * allocate from heap, keep chunk len in
206 * first word
207 */
208 help = top;
209
210 /* make _sure_ the region can hold a struct fl. */
211 if (size < ALIGN(sizeof (struct fl *)))
212 size = ALIGN(sizeof (struct fl *));
213 top += ALIGN(sizeof(unsigned)) + ALIGN(size);
214 #ifdef HEAP_LIMIT
215 if (top > (char *)HEAP_LIMIT)
216 panic("heap full (0x%lx+%zu)", help, size);
217 #endif
218 *(unsigned *)(void *)help = (unsigned)ALIGN(size);
219 #ifdef ALLOC_TRACE
220 printf("=%lx\n", (u_long)help + ALIGN(sizeof(unsigned)));
221 #endif
222 return help + ALIGN(sizeof(unsigned));
223 }
224
225 /* we take the best fit */
226 f = bestf;
227
228 #ifndef ALLOC_FIRST_FIT
229 found:
230 #endif
231 /* remove from freelist */
232 help = (char *)(void *)*f;
233 *f = (*f)->next;
234 #ifdef ALLOC_TRACE
235 printf("=%lx (origsize %u)\n", (u_long)help + ALIGN(sizeof(unsigned)),
236 *(unsigned *)help);
237 #endif
238 return help + ALIGN(sizeof(unsigned));
239 }
240
241 void
242 /*ARGSUSED*/
243 dealloc(void *ptr, size_t size)
244 {
245 struct fl *f =
246 (struct fl *)(void *)((char *)(void *)ptr - ALIGN(sizeof(unsigned)));
247 #ifdef ALLOC_TRACE
248 printf("dealloc(%lx, %zu) (origsize %u)\n", (u_long)ptr, size, f->size);
249 #endif
250 #ifdef DEBUG
251 if (size > (size_t)f->size) {
252 printf("dealloc %u bytes @%lx, should be <=%u\n",
253 size, (u_long)ptr, f->size);
254 }
255
256 if (ptr < (void *)HEAP_START)
257 printf("dealloc: %lx before start of heap.\n", (u_long)ptr);
258
259 #ifdef HEAP_LIMIT
260 if (ptr > (void *)HEAP_LIMIT)
261 printf("dealloc: %lx beyond end of heap.\n", (u_long)ptr);
262 #endif
263 #endif /* DEBUG */
264 /* put into freelist */
265 f->next = freelist;
266 freelist = f;
267 }
268