Home | History | Annotate | Line # | Download | only in ofwboot
alloc.c revision 1.4.2.1
      1 /*	$NetBSD: alloc.c,v 1.4.2.1 2004/08/03 10:37:31 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
     41  * Copyright (c) 1996
     42  *	Matthias Drochner.  All rights reserved.
     43  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
     44  * Copyright (C) 1995, 1996 TooLs GmbH.
     45  * All rights reserved.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. All advertising materials mentioning features or use of this software
     56  *    must display the following acknowledgement:
     57  *	This product includes software developed by TooLs GmbH.
     58  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     59  *    derived from this software without specific prior written permission.
     60  *
     61  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     62  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     63  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     64  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     65  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     66  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     67  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     68  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     69  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     70  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     71  */
     72 
     73 /*
     74  * Dynamic memory allocator suitable for use with OpenFirmware.
     75  *
     76  * Compile options:
     77  *
     78  *	ALLOC_TRACE	enable tracing of allocations/deallocations
     79  *
     80  *	ALLOC_FIRST_FIT	use a first-fit allocation algorithm, rather than
     81  *			the default best-fit algorithm.
     82  *
     83  *	DEBUG		enable debugging sanity checks.
     84  */
     85 
     86 #include <sys/param.h>
     87 #include <sys/queue.h>
     88 
     89 #include <lib/libsa/stand.h>
     90 
     91 #include "openfirm.h"
     92 
     93 /*
     94  * Each block actually has ALIGN(struct ml) + ALIGN(size) bytes allocated
     95  * to it, as follows:
     96  *
     97  * 0 ... (sizeof(struct ml) - 1)
     98  *	allocated or unallocated: holds size of user-data part of block.
     99  *
    100  * sizeof(struct ml) ... (ALIGN(sizeof(struct ml)) - 1)
    101  *	allocated: unused
    102  *	unallocated: depends on packing of struct fl
    103  *
    104  * ALIGN(sizeof(struct ml)) ... (ALIGN(sizeof(struct ml)) +
    105  *   ALIGN(data size) - 1)
    106  *	allocated: user data
    107  *	unallocated: depends on packing of struct fl
    108  *
    109  * 'next' is only used when the block is unallocated (i.e. on the free list).
    110  * However, note that ALIGN(sizeof(struct ml)) + ALIGN(data size) must
    111  * be at least 'sizeof(struct fl)', so that blocks can be used as structures
    112  * when on the free list.
    113  */
    114 
    115 /*
    116  * Memory lists.
    117  */
    118 struct ml {
    119 	unsigned	size;
    120 	LIST_ENTRY(ml)	list;
    121 };
    122 
    123 LIST_HEAD(, ml) freelist = LIST_HEAD_INITIALIZER(freelist);
    124 LIST_HEAD(, ml) allocatedlist = LIST_HEAD_INITIALIZER(allocatedlist);
    125 
    126 #define	OVERHEAD	ALIGN(sizeof (struct ml))	/* shorthand */
    127 
    128 void *
    129 alloc(unsigned size)
    130 {
    131 	struct ml *f, *bestf;
    132 	unsigned bestsize = 0xffffffff;	/* greater than any real size */
    133 	char *help;
    134 	int failed;
    135 
    136 #ifdef ALLOC_TRACE
    137 	printf("alloc(%u)", size);
    138 #endif
    139 
    140 	/*
    141 	 * Account for overhead now, so that we don't get an
    142 	 * "exact fit" which doesn't have enough space.
    143 	 */
    144 	size = ALIGN(size) + OVERHEAD;
    145 
    146 #ifdef ALLOC_FIRST_FIT
    147 	/* scan freelist */
    148 	for (f = freelist.lh_first; f != NULL && f->size < size;
    149 	    f = f->list.le_next)
    150 		/* noop */ ;
    151 	bestf = f;
    152 	failed = (bestf == (struct fl *)0);
    153 #else
    154 	/* scan freelist */
    155 	f = freelist.lh_first;
    156 	while (f != NULL) {
    157 		if (f->size >= size) {
    158 			if (f->size == size)	/* exact match */
    159 				goto found;
    160 
    161 			if (f->size < bestsize) {
    162 				/* keep best fit */
    163 				bestf = f;
    164 				bestsize = f->size;
    165 			}
    166 		}
    167 		f = f->list.le_next;
    168 	}
    169 
    170 	/* no match in freelist if bestsize unchanged */
    171 	failed = (bestsize == 0xffffffff);
    172 #endif
    173 
    174 	if (failed) {	/* nothing found */
    175 		/*
    176 		 * Allocate memory from the OpenFirmware, rounded
    177 		 * to page size, and record the chunk size.
    178 		 */
    179 		size = roundup(size, NBPG);
    180 		help = OF_claim(0, size, NBPG);
    181 		if (help == (char *)-1)
    182 			panic("alloc: out of memory");
    183 
    184 		f = (struct ml *)help;
    185 		f->size = size;
    186 #ifdef ALLOC_TRACE
    187 		printf("=%lx (new chunk size %u)\n",
    188 		    (u_long)(help + OVERHEAD), f->size);
    189 #endif
    190 		goto out;
    191 	}
    192 
    193 	/* we take the best fit */
    194 	f = bestf;
    195 
    196  found:
    197 	/* remove from freelist */
    198 	LIST_REMOVE(f, list);
    199 	help = (char *)f;
    200 #ifdef ALLOC_TRACE
    201 	printf("=%lx (origsize %u)\n", (u_long)(help + OVERHEAD), f->size);
    202 #endif
    203  out:
    204 	/* place on allocated list */
    205 	LIST_INSERT_HEAD(&allocatedlist, f, list);
    206 	return (help + OVERHEAD);
    207 }
    208 
    209 void
    210 free(void *ptr, unsigned size)
    211 {
    212 	register struct ml *a = (struct ml *)((char*)ptr - OVERHEAD);
    213 
    214 #ifdef ALLOC_TRACE
    215 	printf("free(%lx, %u) (origsize %u)\n", (u_long)ptr, size, a->size);
    216 #endif
    217 #ifdef DEBUG
    218 	if (size > a->size)
    219 		printf("free %u bytes @%lx, should be <=%u\n",
    220 		    size, (u_long)ptr, a->size);
    221 #endif
    222 
    223 	/* Remove from allocated list, place on freelist. */
    224 	LIST_REMOVE(a, list);
    225 	LIST_INSERT_HEAD(&freelist, a, list);
    226 }
    227 
    228 #ifdef __notyet__
    229 void
    230 freeall(void)
    231 {
    232 	struct ml *m;
    233 
    234 	/* Release chunks on freelist... */
    235 	while ((m = freelist.lh_first) != NULL) {
    236 		LIST_REMOVE(m, list);
    237 		OF_release(m, m->size);
    238 	}
    239 
    240 	/* ...and allocated list. */
    241 	while ((m = allocatedlist.lh_first) != NULL) {
    242 		LIST_REMOVE(m, list);
    243 		OF_release(m, m->size);
    244 	}
    245 }
    246 #endif /* __notyet__ */
    247