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