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