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