alloc.c revision 1.8 1 1.8 drochner /* $NetBSD: alloc.c,v 1.8 1997/06/13 14:28:52 drochner 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.1 brezak } *freelist = (struct fl *)0;
114 1.1 brezak
115 1.5 cgd #ifdef HEAP_START
116 1.5 cgd static char *top = (char*)HEAP_START;
117 1.5 cgd #else
118 1.1 brezak extern char end[];
119 1.1 brezak static char *top = end;
120 1.5 cgd #endif
121 1.1 brezak
122 1.1 brezak void *
123 1.1 brezak alloc(size)
124 1.1 brezak unsigned size;
125 1.1 brezak {
126 1.7 leo register struct fl **f = &freelist, **bestf = NULL;
127 1.5 cgd unsigned bestsize = 0xffffffff; /* greater than any real size */
128 1.5 cgd char *help;
129 1.5 cgd int failed;
130 1.5 cgd
131 1.5 cgd #ifdef ALLOC_TRACE
132 1.5 cgd printf("alloc(%u)", size);
133 1.5 cgd #endif
134 1.5 cgd
135 1.5 cgd #ifdef ALLOC_FIRST_FIT
136 1.5 cgd while (*f != (struct fl *)0 && (*f)->size < size)
137 1.5 cgd f = &((*f)->next);
138 1.5 cgd bestf = f;
139 1.5 cgd failed = (*bestf == (struct fl *)0);
140 1.5 cgd #else
141 1.5 cgd /* scan freelist */
142 1.5 cgd while (*f) {
143 1.5 cgd if ((*f)->size >= size) {
144 1.5 cgd if ((*f)->size == size) /* exact match */
145 1.5 cgd goto found;
146 1.5 cgd
147 1.5 cgd if ((*f)->size < bestsize) {
148 1.5 cgd /* keep best fit */
149 1.5 cgd bestf = f;
150 1.5 cgd bestsize = (*f)->size;
151 1.5 cgd }
152 1.5 cgd }
153 1.5 cgd f = &((*f)->next);
154 1.5 cgd }
155 1.1 brezak
156 1.5 cgd /* no match in freelist if bestsize unchanged */
157 1.5 cgd failed = (bestsize == 0xffffffff);
158 1.5 cgd #endif
159 1.5 cgd
160 1.5 cgd if (failed) { /* nothing found */
161 1.5 cgd /*
162 1.5 cgd * allocate from heap, keep chunk len in
163 1.5 cgd * first word
164 1.5 cgd */
165 1.5 cgd help = top;
166 1.5 cgd
167 1.5 cgd /* make _sure_ the region can hold a struct fl. */
168 1.5 cgd if (size < ALIGN(sizeof (struct fl *)))
169 1.5 cgd size = ALIGN(sizeof (struct fl *));
170 1.5 cgd top += ALIGN(sizeof(unsigned)) + ALIGN(size);
171 1.5 cgd #ifdef HEAP_LIMIT
172 1.5 cgd if (top > (char*)HEAP_LIMIT)
173 1.5 cgd panic("heap full (0x%lx+%u)", help, size);
174 1.5 cgd #endif
175 1.5 cgd *(unsigned *)help = ALIGN(size);
176 1.5 cgd #ifdef ALLOC_TRACE
177 1.5 cgd printf("=%x\n", help + ALIGN(sizeof(unsigned)));
178 1.5 cgd #endif
179 1.5 cgd return(help + ALIGN(sizeof(unsigned)));
180 1.1 brezak }
181 1.5 cgd
182 1.5 cgd /* we take the best fit */
183 1.5 cgd f = bestf;
184 1.5 cgd
185 1.5 cgd found:
186 1.5 cgd /* remove from freelist */
187 1.5 cgd help = (char*)*f;
188 1.5 cgd *f = (*f)->next;
189 1.5 cgd #ifdef ALLOC_TRACE
190 1.5 cgd printf("=%lx (origsize %u)\n", help + ALIGN(sizeof(unsigned)),
191 1.5 cgd *(unsigned *)help);
192 1.5 cgd #endif
193 1.5 cgd return(help + ALIGN(sizeof(unsigned)));
194 1.1 brezak }
195 1.1 brezak
196 1.1 brezak void
197 1.1 brezak free(ptr, size)
198 1.1 brezak void *ptr;
199 1.5 cgd unsigned size; /* only for consistence check */
200 1.1 brezak {
201 1.5 cgd register struct fl *f =
202 1.5 cgd (struct fl *)((char*)ptr - ALIGN(sizeof(unsigned)));
203 1.5 cgd #ifdef ALLOC_TRACE
204 1.5 cgd printf("free(%lx, %u) (origsize %u)\n", ptr, size, f->size);
205 1.5 cgd #endif
206 1.5 cgd #ifdef DEBUG
207 1.5 cgd if (size > f->size)
208 1.5 cgd printf("free %u bytes @%lx, should be <=%u\n",
209 1.5 cgd size, ptr, f->size);
210 1.6 thorpej #ifdef HEAP_START
211 1.6 thorpej if (ptr < (void *)HEAP_START)
212 1.6 thorpej #else
213 1.6 thorpej if (ptr < (void *)end[])
214 1.6 thorpej #endif
215 1.6 thorpej printf("free: %lx before start of heap.\n", (u_long)ptr);
216 1.6 thorpej
217 1.6 thorpej #ifdef HEAP_LIMIT
218 1.6 thorpej if (ptr > (void *)HEAP_LIMIT)
219 1.6 thorpej printf("free: %lx beyond end of heap.\n", (u_long)ptr);
220 1.5 cgd #endif
221 1.6 thorpej #endif /* DEBUG */
222 1.5 cgd /* put into freelist */
223 1.1 brezak f->next = freelist;
224 1.1 brezak freelist = f;
225 1.1 brezak }
226