1 /* $NetBSD: alloc.c,v 1.26 2011/07/30 03:43:20 jakllsch Exp $ */
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)alloc.c 8.1 (Berkeley) 6/11/93
37 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
39 * Matthias Drochner. All rights reserved.
41 * This code is derived from software contributed to Berkeley by
42 * The Mach Operating System project at Carnegie-Mellon University.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * @(#)alloc.c 8.1 (Berkeley) 6/11/93
75 * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
76 * All Rights Reserved.
78 * Author: Alessandro Forin
80 * Permission to use, copy, modify and distribute this software and its
81 * documentation is hereby granted, provided that both the copyright
82 * notice and this permission notice appear in all copies of the
83 * software, derivative works or modified versions, and any portions
84 * thereof, and that both notices appear in supporting documentation.
86 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
87 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
88 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
90 * Carnegie Mellon requests users of this software to return to
92 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
93 * School of Computer Science
94 * Carnegie Mellon University
95 * Pittsburgh PA 15213-3890
97 * any improvements or extensions that they make and grant Carnegie the
98 * rights to redistribute these changes.
102 * Dynamic memory allocator.
106 * ALLOC_TRACE enable tracing of allocations/deallocations
108 * ALLOC_FIRST_FIT use a first-fit allocation algorithm, rather than
109 * the default best-fit algorithm.
111 * HEAP_LIMIT heap limit address (defaults to "no limit").
113 * HEAP_START start address of heap (defaults to '&end').
115 * DEBUG enable debugging sanity checks.
118 #include <sys/param.h>
122 * Each block actually has ALIGN(unsigned int) + ALIGN(size) bytes allocated
125 * 0 ... (sizeof(unsigned int) - 1)
126 * allocated or unallocated: holds size of user-data part of block.
128 * sizeof(unsigned int) ... (ALIGN(sizeof(unsigned int)) - 1)
130 * unallocated: depends on packing of struct fl
132 * ALIGN(sizeof(unsigned int)) ...
133 * (ALIGN(sizeof(unsigned int)) + ALIGN(data size) - 1)
134 * allocated: user data
135 * unallocated: depends on packing of struct fl
137 * 'next' is only used when the block is unallocated (i.e. on the free list).
138 * However, note that ALIGN(sizeof(unsigned int)) + ALIGN(data size) must
139 * be at least 'sizeof(struct fl)', so that blocks can be used as structures
140 * when on the free list.
148 static char *top
, *heapstart
, *heaplimit
;
150 setheap(void *start
, void *limit
)
152 heapstart
= top
= start
;
155 #define HEAP_START heapstart
156 #define HEAP_LIMIT heaplimit
157 #else /* !HEAP_VARIABLE */
160 #define HEAP_START end
162 static char *top
= (char *)HEAP_START
;
163 #endif /* HEAP_VARIABLE */
168 struct fl
**f
= &freelist
, **bestf
= NULL
;
169 #ifndef ALLOC_FIRST_FIT
170 unsigned int bestsize
= 0xffffffff; /* greater than any real size */
176 printf("alloc(%zu)", size
);
179 #ifdef ALLOC_FIRST_FIT
180 while (*f
!= (struct fl
*)0 && (size_t)(*f
)->size
< size
)
183 failed
= (*bestf
== (struct fl
*)0);
187 if ((size_t)(*f
)->size
>= size
) {
188 if ((size_t)(*f
)->size
== size
) /* exact match */
191 if ((*f
)->size
< bestsize
) {
194 bestsize
= (*f
)->size
;
200 /* no match in freelist if bestsize unchanged */
201 failed
= (bestsize
== 0xffffffff);
204 if (failed
) { /* nothing found */
206 * allocate from heap, keep chunk len in
211 /* make _sure_ the region can hold a struct fl. */
212 if (size
< ALIGN(sizeof (struct fl
*)))
213 size
= ALIGN(sizeof (struct fl
*));
214 top
+= ALIGN(sizeof(unsigned int)) + ALIGN(size
);
216 if (top
> (char *)HEAP_LIMIT
)
217 panic("heap full (%p+%zu)", help
, size
);
219 *(unsigned int *)(void *)help
= (unsigned int)ALIGN(size
);
221 printf("=%lx\n", (u_long
)help
+ ALIGN(sizeof(unsigned int)));
223 return help
+ ALIGN(sizeof(unsigned int));
226 /* we take the best fit */
229 #ifndef ALLOC_FIRST_FIT
232 /* remove from freelist */
233 help
= (char *)(void *)*f
;
236 printf("=%lx (origsize %u)\n",
237 (u_long
)help
+ ALIGN(sizeof(unsigned int)), *(unsigned int *)help
);
239 return help
+ ALIGN(sizeof(unsigned int));
244 dealloc(void *ptr
, size_t size
)
247 (struct fl
*)(void *)((char *)(void *)ptr
-
248 ALIGN(sizeof(unsigned int)));
250 printf("dealloc(%lx, %zu) (origsize %u)\n", (u_long
)ptr
, size
, f
->size
);
253 if (size
> (size_t)f
->size
) {
254 printf("dealloc %zu bytes @%lx, should be <=%u\n",
255 size
, (u_long
)ptr
, f
->size
);
258 if (ptr
< (void *)HEAP_START
)
259 printf("dealloc: %lx before start of heap.\n", (u_long
)ptr
);
262 if (ptr
> (void *)HEAP_LIMIT
)
263 printf("dealloc: %lx beyond end of heap.\n", (u_long
)ptr
);
266 /* put into freelist */