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