alloc.c revision 1.5 1 1.5 cgd /* $NetBSD: alloc.c,v 1.5 1997/01/22 01:18:23 cgd 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.2 cgd
89 1.1 brezak /*
90 1.5 cgd * Each block actually has ALIGN(unsigned) + ALIGN(size) bytes allocated
91 1.5 cgd * to it, as follows:
92 1.5 cgd *
93 1.5 cgd * 0 ... (sizeof(unsigned) - 1)
94 1.5 cgd * allocated or unallocated: holds size of user-data part of block.
95 1.5 cgd *
96 1.5 cgd * sizeof(unsigned) ... (ALIGN(sizeof(unsigned)) - 1)
97 1.5 cgd * allocated: unused
98 1.5 cgd * unallocated: depends on packing of struct fl
99 1.5 cgd *
100 1.5 cgd * ALIGN(sizeof(unsigned)) ... (ALIGN(sizeof(unsigned)) + ALIGN(data size) - 1)
101 1.5 cgd * allocated: user data
102 1.5 cgd * unallocated: depends on packing of struct fl
103 1.5 cgd *
104 1.5 cgd * 'next' is only used when the block is unallocated (i.e. on the free list).
105 1.5 cgd * However, note that ALIGN(sizeof(unsigned)) + ALIGN(data size) must
106 1.5 cgd * be at least 'sizeof(struct fl)', so that blocks can be used as structures
107 1.5 cgd * when on the free list.
108 1.1 brezak */
109 1.1 brezak struct fl {
110 1.5 cgd unsigned size;
111 1.1 brezak struct fl *next;
112 1.1 brezak } *freelist = (struct fl *)0;
113 1.1 brezak
114 1.5 cgd #ifdef HEAP_START
115 1.5 cgd static char *top = (char*)HEAP_START;
116 1.5 cgd #else
117 1.1 brezak extern char end[];
118 1.1 brezak static char *top = end;
119 1.5 cgd #endif
120 1.1 brezak
121 1.1 brezak void *
122 1.1 brezak alloc(size)
123 1.1 brezak unsigned size;
124 1.1 brezak {
125 1.5 cgd register struct fl **f = &freelist, **bestf;
126 1.5 cgd unsigned bestsize = 0xffffffff; /* greater than any real size */
127 1.5 cgd char *help;
128 1.5 cgd int failed;
129 1.5 cgd
130 1.5 cgd #ifdef ALLOC_TRACE
131 1.5 cgd printf("alloc(%u)", size);
132 1.5 cgd #endif
133 1.5 cgd
134 1.5 cgd #ifdef ALLOC_FIRST_FIT
135 1.5 cgd while (*f != (struct fl *)0 && (*f)->size < size)
136 1.5 cgd f = &((*f)->next);
137 1.5 cgd bestf = f;
138 1.5 cgd failed = (*bestf == (struct fl *)0);
139 1.5 cgd #else
140 1.5 cgd /* scan freelist */
141 1.5 cgd while (*f) {
142 1.5 cgd if ((*f)->size >= size) {
143 1.5 cgd if ((*f)->size == size) /* exact match */
144 1.5 cgd goto found;
145 1.5 cgd
146 1.5 cgd if ((*f)->size < bestsize) {
147 1.5 cgd /* keep best fit */
148 1.5 cgd bestf = f;
149 1.5 cgd bestsize = (*f)->size;
150 1.5 cgd }
151 1.5 cgd }
152 1.5 cgd f = &((*f)->next);
153 1.5 cgd }
154 1.1 brezak
155 1.5 cgd /* no match in freelist if bestsize unchanged */
156 1.5 cgd failed = (bestsize == 0xffffffff);
157 1.5 cgd #endif
158 1.5 cgd
159 1.5 cgd if (failed) { /* nothing found */
160 1.5 cgd /*
161 1.5 cgd * allocate from heap, keep chunk len in
162 1.5 cgd * first word
163 1.5 cgd */
164 1.5 cgd help = top;
165 1.5 cgd
166 1.5 cgd /* make _sure_ the region can hold a struct fl. */
167 1.5 cgd if (size < ALIGN(sizeof (struct fl *)))
168 1.5 cgd size = ALIGN(sizeof (struct fl *));
169 1.5 cgd top += ALIGN(sizeof(unsigned)) + ALIGN(size);
170 1.5 cgd #ifdef HEAP_LIMIT
171 1.5 cgd if (top > (char*)HEAP_LIMIT)
172 1.5 cgd panic("heap full (0x%lx+%u)", help, size);
173 1.5 cgd #endif
174 1.5 cgd *(unsigned *)help = ALIGN(size);
175 1.5 cgd #ifdef ALLOC_TRACE
176 1.5 cgd printf("=%x\n", help + ALIGN(sizeof(unsigned)));
177 1.5 cgd #endif
178 1.5 cgd return(help + ALIGN(sizeof(unsigned)));
179 1.1 brezak }
180 1.5 cgd
181 1.5 cgd /* we take the best fit */
182 1.5 cgd f = bestf;
183 1.5 cgd
184 1.5 cgd found:
185 1.5 cgd /* remove from freelist */
186 1.5 cgd help = (char*)*f;
187 1.5 cgd *f = (*f)->next;
188 1.5 cgd #ifdef ALLOC_TRACE
189 1.5 cgd printf("=%lx (origsize %u)\n", help + ALIGN(sizeof(unsigned)),
190 1.5 cgd *(unsigned *)help);
191 1.5 cgd #endif
192 1.5 cgd return(help + ALIGN(sizeof(unsigned)));
193 1.1 brezak }
194 1.1 brezak
195 1.1 brezak void
196 1.1 brezak free(ptr, size)
197 1.1 brezak void *ptr;
198 1.5 cgd unsigned size; /* only for consistence check */
199 1.1 brezak {
200 1.5 cgd register struct fl *f =
201 1.5 cgd (struct fl *)((char*)ptr - ALIGN(sizeof(unsigned)));
202 1.5 cgd #ifdef ALLOC_TRACE
203 1.5 cgd printf("free(%lx, %u) (origsize %u)\n", ptr, size, f->size);
204 1.5 cgd #endif
205 1.5 cgd #ifdef DEBUG
206 1.5 cgd if (size > f->size)
207 1.5 cgd printf("free %u bytes @%lx, should be <=%u\n",
208 1.5 cgd size, ptr, f->size);
209 1.5 cgd #endif
210 1.5 cgd /* put into freelist */
211 1.1 brezak f->next = freelist;
212 1.1 brezak freelist = f;
213 1.1 brezak }
214