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