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