1/* $NetBSD: alloc.c,v 1.28 2019/03/31 20:08:45 christos Exp $ */ 2 3/* 4 * Copyright (c) 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * The Mach Operating System project at Carnegie-Mellon University. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 21 * 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 32 * SUCH DAMAGE. 33 * 34 * @(#)alloc.c 8.1 (Berkeley) 6/11/93 35 * 36 * 37 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved. 38 * Copyright (c) 1996 39 * Matthias Drochner. All rights reserved. 40 * 41 * This code is derived from software contributed to Berkeley by 42 * The Mach Operating System project at Carnegie-Mellon University. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 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. 59 * 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 70 * SUCH DAMAGE. 71 * 72 * @(#)alloc.c 8.1 (Berkeley) 6/11/93 73 * 74 * 75 * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University 76 * All Rights Reserved. 77 * 78 * Author: Alessandro Forin 79 * 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. 85 * 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. 89 * 90 * Carnegie Mellon requests users of this software to return to 91 * 92 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 93 * School of Computer Science 94 * Carnegie Mellon University 95 * Pittsburgh PA 15213-3890 96 * 97 * any improvements or extensions that they make and grant Carnegie the 98 * rights to redistribute these changes. 99 */ 100 101/* 102 * Dynamic memory allocator. 103 * 104 * Compile options: 105 * 106 * HEAP_LIMIT heap limit address (defaults to "no limit"). 107 * 108 * HEAP_START start address of heap (defaults to '&end'). 109 * 110 * DEBUG enable debugging sanity checks. 111 */ 112 113#include <sys/param.h> 114#include "stand.h" 115 116/* 117 * Each block actually has ALIGN(unsigned int) + ALIGN(size) bytes allocated 118 * to it, as follows: 119 * 120 * 0 ... (sizeof(unsigned int) - 1) 121 * allocated or unallocated: holds size of user-data part of block. 122 * 123 * sizeof(unsigned int) ... (ALIGN(sizeof(unsigned int)) - 1) 124 * allocated: unused 125 * unallocated: depends on packing of struct fl 126 * 127 * ALIGN(sizeof(unsigned int)) ... 128 * (ALIGN(sizeof(unsigned int)) + ALIGN(data size) - 1) 129 * allocated: user data 130 * unallocated: depends on packing of struct fl 131 * 132 * 'next' is only used when the block is unallocated (i.e. on the free list). 133 * However, note that ALIGN(sizeof(unsigned int)) + ALIGN(data size) must 134 * be at least 'sizeof(struct fl)', so that blocks can be used as structures 135 * when on the free list. 136 * 137 * When HEAP_LIMIT is defined and the heap limit is reached, alloc() panics. 138 * Otherwise, it never fails. 139 */ 140struct fl { 141 unsigned int size; 142 struct fl *next; 143} *freelist; 144 145#ifdef HEAP_VARIABLE 146static char *top, *heapstart, *heaplimit; 147void 148setheap(void *start, void *limit) 149{ 150 heapstart = top = start; 151 heaplimit = limit; 152} 153#define HEAP_START heapstart 154#define HEAP_LIMIT heaplimit 155#else /* !HEAP_VARIABLE */ 156#ifndef HEAP_START 157extern char end[]; 158#define HEAP_START end 159#endif 160static char *top = (char *)HEAP_START; 161#endif /* HEAP_VARIABLE */ 162 163__compactcall void * 164alloc(size_t size) 165{ 166 struct fl **f = &freelist, **bestf = NULL; 167 unsigned int bestsize = 0xffffffff; /* greater than any real size */ 168 char *help; 169 int failed; 170 171 /* scan freelist */ 172 while (*f) { 173 if ((size_t)(*f)->size >= size) { 174 if ((size_t)(*f)->size == size) /* exact match */ 175 goto found; 176 177 if ((*f)->size < bestsize) { 178 /* keep best fit */ 179 bestf = f; 180 bestsize = (*f)->size; 181 } 182 } 183 f = &((*f)->next); 184 } 185 186 /* no match in freelist if bestsize unchanged */ 187 failed = (bestsize == 0xffffffff); 188 189 if (failed) { /* nothing found */ 190 /* 191 * allocate from heap, keep chunk len in 192 * first word 193 */ 194 help = top; 195 196 /* make _sure_ the region can hold a struct fl. */ 197 if (size < ALIGN(sizeof (struct fl *))) 198 size = ALIGN(sizeof (struct fl *)); 199 top += ALIGN(sizeof(unsigned int)) + ALIGN(size); 200#ifdef HEAP_LIMIT 201 if (top > (char *)HEAP_LIMIT) 202 panic("heap full (%p+%zu)", help, size); 203#endif 204 *(unsigned int *)(void *)help = (unsigned int)ALIGN(size); 205 return help + ALIGN(sizeof(unsigned int)); 206 } 207 208 /* we take the best fit */ 209 f = bestf; 210 211found: 212 /* remove from freelist */ 213 help = (char *)(void *)*f; 214 *f = (*f)->next; 215 return help + ALIGN(sizeof(unsigned int)); 216} 217 218__compactcall void 219/*ARGSUSED*/ 220dealloc(void *ptr, size_t size) 221{ 222 struct fl *f = 223 (struct fl *)(void *)((char *)(void *)ptr - 224 ALIGN(sizeof(unsigned int))); 225#ifdef DEBUG 226 if (size > (size_t)f->size) { 227 printf("%s: %zu bytes @%p, should be <=%u\n", __func__, 228 size, ptr, f->size); 229 } 230 231 if (ptr < (void *)HEAP_START) 232 printf("%s: %p before start of heap.\n", __func__, ptr); 233 234#ifdef HEAP_LIMIT 235 if (ptr > (void *)HEAP_LIMIT) 236 printf("%s: %p beyond end of heap.\n", __func__, ptr); 237#endif 238#endif /* DEBUG */ 239 /* put into freelist */ 240 f->next = freelist; 241 freelist = f; 242} 243