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