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