Home | History | Annotate | Line # | Download | only in libsa
alloc.c revision 1.19
      1  1.19     perry /*	$NetBSD: alloc.c,v 1.19 2005/02/26 22:58:56 perry Exp $	*/
      2   1.3       cgd 
      3   1.5       cgd /*
      4  1.17       agc  * Copyright (c) 1993
      5  1.17       agc  *	The Regents of the University of California.  All rights reserved.
      6  1.17       agc  *
      7  1.17       agc  * This code is derived from software contributed to Berkeley by
      8  1.17       agc  * The Mach Operating System project at Carnegie-Mellon University.
      9  1.17       agc  *
     10  1.17       agc  * Redistribution and use in source and binary forms, with or without
     11  1.17       agc  * modification, are permitted provided that the following conditions
     12  1.17       agc  * are met:
     13  1.17       agc  * 1. Redistributions of source code must retain the above copyright
     14  1.17       agc  *    notice, this list of conditions and the following disclaimer.
     15  1.17       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.17       agc  *    notice, this list of conditions and the following disclaimer in the
     17  1.17       agc  *    documentation and/or other materials provided with the distribution.
     18  1.17       agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.17       agc  *    may be used to endorse or promote products derived from this software
     20  1.17       agc  *    without specific prior written permission.
     21  1.17       agc  *
     22  1.17       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.17       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.17       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.17       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.17       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.17       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.17       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.17       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.17       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.17       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.17       agc  * SUCH DAMAGE.
     33  1.17       agc  *
     34  1.17       agc  *	@(#)alloc.c	8.1 (Berkeley) 6/11/93
     35  1.19     perry  *
     36  1.17       agc  *
     37   1.5       cgd  * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
     38   1.5       cgd  * Copyright (c) 1996
     39   1.5       cgd  *	Matthias Drochner.  All rights reserved.
     40   1.1    brezak  *
     41   1.1    brezak  * This code is derived from software contributed to Berkeley by
     42   1.1    brezak  * The Mach Operating System project at Carnegie-Mellon University.
     43   1.1    brezak  *
     44   1.1    brezak  * Redistribution and use in source and binary forms, with or without
     45   1.1    brezak  * modification, are permitted provided that the following conditions
     46   1.1    brezak  * are met:
     47   1.1    brezak  * 1. Redistributions of source code must retain the above copyright
     48   1.1    brezak  *    notice, this list of conditions and the following disclaimer.
     49   1.1    brezak  * 2. Redistributions in binary form must reproduce the above copyright
     50   1.1    brezak  *    notice, this list of conditions and the following disclaimer in the
     51   1.1    brezak  *    documentation and/or other materials provided with the distribution.
     52   1.1    brezak  * 3. All advertising materials mentioning features or use of this software
     53   1.1    brezak  *    must display the following acknowledgement:
     54   1.1    brezak  *	This product includes software developed by the University of
     55   1.1    brezak  *	California, Berkeley and its contributors.
     56   1.1    brezak  * 4. Neither the name of the University nor the names of its contributors
     57   1.1    brezak  *    may be used to endorse or promote products derived from this software
     58   1.1    brezak  *    without specific prior written permission.
     59   1.1    brezak  *
     60   1.1    brezak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61   1.1    brezak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62   1.1    brezak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63   1.1    brezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64   1.1    brezak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65   1.1    brezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66   1.1    brezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67   1.1    brezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68   1.1    brezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69   1.1    brezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70   1.1    brezak  * SUCH DAMAGE.
     71   1.1    brezak  *
     72   1.3       cgd  *	@(#)alloc.c	8.1 (Berkeley) 6/11/93
     73  1.19     perry  *
     74   1.1    brezak  *
     75   1.1    brezak  * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
     76   1.1    brezak  * All Rights Reserved.
     77   1.1    brezak  *
     78   1.1    brezak  * Author: Alessandro Forin
     79  1.19     perry  *
     80   1.1    brezak  * Permission to use, copy, modify and distribute this software and its
     81   1.1    brezak  * documentation is hereby granted, provided that both the copyright
     82   1.1    brezak  * notice and this permission notice appear in all copies of the
     83   1.1    brezak  * software, derivative works or modified versions, and any portions
     84   1.1    brezak  * thereof, and that both notices appear in supporting documentation.
     85  1.19     perry  *
     86   1.1    brezak  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     87   1.1    brezak  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     88   1.1    brezak  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     89  1.19     perry  *
     90   1.1    brezak  * Carnegie Mellon requests users of this software to return to
     91  1.19     perry  *
     92   1.1    brezak  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     93   1.1    brezak  *  School of Computer Science
     94   1.1    brezak  *  Carnegie Mellon University
     95   1.1    brezak  *  Pittsburgh PA 15213-3890
     96  1.19     perry  *
     97   1.1    brezak  * any improvements or extensions that they make and grant Carnegie the
     98   1.1    brezak  * rights to redistribute these changes.
     99   1.1    brezak  */
    100   1.1    brezak 
    101   1.5       cgd /*
    102   1.5       cgd  * Dynamic memory allocator.
    103   1.5       cgd  *
    104   1.5       cgd  * Compile options:
    105   1.5       cgd  *
    106   1.5       cgd  *	ALLOC_TRACE	enable tracing of allocations/deallocations
    107   1.5       cgd 
    108   1.5       cgd  *	ALLOC_FIRST_FIT	use a first-fit allocation algorithm, rather than
    109   1.5       cgd  *			the default best-fit algorithm.
    110   1.5       cgd  *
    111   1.5       cgd  *	HEAP_LIMIT	heap limit address (defaults to "no limit").
    112   1.5       cgd  *
    113   1.5       cgd  *	HEAP_START	start address of heap (defaults to '&end').
    114   1.5       cgd  *
    115   1.5       cgd  *	DEBUG		enable debugging sanity checks.
    116   1.5       cgd  */
    117   1.5       cgd 
    118   1.2       cgd #include <sys/param.h>
    119   1.8  drochner #include "stand.h"
    120   1.2       cgd 
    121   1.1    brezak /*
    122   1.5       cgd  * Each block actually has ALIGN(unsigned) + ALIGN(size) bytes allocated
    123   1.5       cgd  * to it, as follows:
    124   1.5       cgd  *
    125   1.5       cgd  * 0 ... (sizeof(unsigned) - 1)
    126   1.5       cgd  *	allocated or unallocated: holds size of user-data part of block.
    127   1.5       cgd  *
    128   1.5       cgd  * sizeof(unsigned) ... (ALIGN(sizeof(unsigned)) - 1)
    129   1.5       cgd  *	allocated: unused
    130   1.5       cgd  *	unallocated: depends on packing of struct fl
    131   1.5       cgd  *
    132   1.5       cgd  * ALIGN(sizeof(unsigned)) ... (ALIGN(sizeof(unsigned)) + ALIGN(data size) - 1)
    133   1.5       cgd  *	allocated: user data
    134   1.5       cgd  *	unallocated: depends on packing of struct fl
    135   1.5       cgd  *
    136   1.5       cgd  * 'next' is only used when the block is unallocated (i.e. on the free list).
    137   1.5       cgd  * However, note that ALIGN(sizeof(unsigned)) + ALIGN(data size) must
    138   1.5       cgd  * be at least 'sizeof(struct fl)', so that blocks can be used as structures
    139   1.5       cgd  * when on the free list.
    140   1.1    brezak  */
    141   1.1    brezak struct fl {
    142   1.5       cgd 	unsigned	size;
    143   1.1    brezak 	struct fl	*next;
    144  1.14       cgd } *freelist;
    145   1.1    brezak 
    146   1.9  drochner #ifdef HEAP_VARIABLE
    147  1.10  drochner static char *top, *heapstart, *heaplimit;
    148  1.16       dsl void setheap(void *start, void *limit)
    149   1.9  drochner {
    150  1.10  drochner     heapstart = top = start;
    151   1.9  drochner     heaplimit = limit;
    152   1.9  drochner }
    153  1.10  drochner #define HEAP_START heapstart
    154   1.9  drochner #define HEAP_LIMIT heaplimit
    155  1.10  drochner #else /* !HEAP_VARIABLE */
    156  1.10  drochner #ifndef HEAP_START
    157  1.10  drochner extern char end[];
    158  1.10  drochner #define HEAP_START end
    159   1.5       cgd #endif
    160  1.10  drochner static char *top = (char*)HEAP_START;
    161  1.10  drochner #endif /* HEAP_VARIABLE */
    162   1.1    brezak 
    163   1.1    brezak void *
    164  1.16       dsl alloc(u_int size)
    165   1.1    brezak {
    166  1.15  augustss 	struct fl **f = &freelist, **bestf = NULL;
    167  1.12    simonb #ifndef ALLOC_FIRST_FIT
    168   1.5       cgd 	unsigned bestsize = 0xffffffff;	/* greater than any real size */
    169  1.12    simonb #endif
    170   1.5       cgd 	char *help;
    171   1.5       cgd 	int failed;
    172   1.5       cgd 
    173   1.5       cgd #ifdef ALLOC_TRACE
    174   1.5       cgd 	printf("alloc(%u)", size);
    175   1.5       cgd #endif
    176   1.5       cgd 
    177   1.5       cgd #ifdef ALLOC_FIRST_FIT
    178   1.5       cgd 	while (*f != (struct fl *)0 && (*f)->size < size)
    179   1.5       cgd 		f = &((*f)->next);
    180   1.5       cgd 	bestf = f;
    181   1.5       cgd 	failed = (*bestf == (struct fl *)0);
    182   1.5       cgd #else
    183   1.5       cgd 	/* scan freelist */
    184   1.5       cgd 	while (*f) {
    185   1.5       cgd 		if ((*f)->size >= size) {
    186   1.5       cgd 			if ((*f)->size == size) /* exact match */
    187   1.5       cgd 				goto found;
    188   1.5       cgd 
    189   1.5       cgd 			if ((*f)->size < bestsize) {
    190   1.5       cgd 				/* keep best fit */
    191   1.5       cgd 	                        bestf = f;
    192   1.5       cgd 	                        bestsize = (*f)->size;
    193   1.5       cgd 	                }
    194   1.5       cgd 	        }
    195   1.5       cgd 	        f = &((*f)->next);
    196   1.5       cgd 	}
    197   1.1    brezak 
    198   1.5       cgd 	/* no match in freelist if bestsize unchanged */
    199   1.5       cgd 	failed = (bestsize == 0xffffffff);
    200   1.5       cgd #endif
    201   1.5       cgd 
    202   1.5       cgd 	if (failed) { /* nothing found */
    203   1.5       cgd 	        /*
    204   1.5       cgd 		 * allocate from heap, keep chunk len in
    205   1.5       cgd 		 * first word
    206   1.5       cgd 		 */
    207   1.5       cgd 	        help = top;
    208   1.5       cgd 
    209   1.5       cgd 		/* make _sure_ the region can hold a struct fl. */
    210   1.5       cgd 		if (size < ALIGN(sizeof (struct fl *)))
    211   1.5       cgd 			size = ALIGN(sizeof (struct fl *));
    212   1.5       cgd 		top += ALIGN(sizeof(unsigned)) + ALIGN(size);
    213   1.5       cgd #ifdef HEAP_LIMIT
    214   1.5       cgd 		if (top > (char*)HEAP_LIMIT)
    215   1.5       cgd 		        panic("heap full (0x%lx+%u)", help, size);
    216   1.5       cgd #endif
    217  1.18  christos 		*(unsigned *)(void *)help = (unsigned)ALIGN(size);
    218   1.5       cgd #ifdef ALLOC_TRACE
    219  1.11  drochner 		printf("=%lx\n", (u_long)help + ALIGN(sizeof(unsigned)));
    220   1.5       cgd #endif
    221   1.5       cgd 		return(help + ALIGN(sizeof(unsigned)));
    222   1.1    brezak 	}
    223   1.5       cgd 
    224   1.5       cgd 	/* we take the best fit */
    225   1.5       cgd 	f = bestf;
    226   1.5       cgd 
    227  1.13    simonb #ifndef ALLOC_FIRST_FIT
    228   1.5       cgd found:
    229  1.13    simonb #endif
    230   1.5       cgd         /* remove from freelist */
    231  1.18  christos         help = (char *)(void *)*f;
    232   1.5       cgd 	*f = (*f)->next;
    233   1.5       cgd #ifdef ALLOC_TRACE
    234  1.11  drochner 	printf("=%lx (origsize %u)\n", (u_long)help + ALIGN(sizeof(unsigned)),
    235   1.5       cgd 	    *(unsigned *)help);
    236   1.5       cgd #endif
    237   1.5       cgd 	return(help + ALIGN(sizeof(unsigned)));
    238   1.1    brezak }
    239   1.1    brezak 
    240   1.1    brezak void
    241  1.18  christos /*ARGSUSED*/
    242  1.16       dsl free(void *ptr, u_int size)
    243   1.1    brezak {
    244  1.15  augustss 	struct fl *f =
    245  1.18  christos 	    (struct fl *)(void *)((char*)(void *)ptr - ALIGN(sizeof(unsigned)));
    246   1.5       cgd #ifdef ALLOC_TRACE
    247  1.11  drochner 	printf("free(%lx, %u) (origsize %u)\n", (u_long)ptr, size, f->size);
    248   1.5       cgd #endif
    249   1.5       cgd #ifdef DEBUG
    250   1.5       cgd         if (size > f->size)
    251   1.5       cgd 	        printf("free %u bytes @%lx, should be <=%u\n",
    252  1.11  drochner 		    size, (u_long)ptr, f->size);
    253  1.10  drochner 
    254   1.6   thorpej 	if (ptr < (void *)HEAP_START)
    255   1.6   thorpej 		printf("free: %lx before start of heap.\n", (u_long)ptr);
    256   1.6   thorpej 
    257   1.6   thorpej #ifdef HEAP_LIMIT
    258   1.6   thorpej 	if (ptr > (void *)HEAP_LIMIT)
    259   1.6   thorpej 		printf("free: %lx beyond end of heap.\n", (u_long)ptr);
    260   1.5       cgd #endif
    261   1.6   thorpej #endif /* DEBUG */
    262   1.5       cgd 	/* put into freelist */
    263   1.1    brezak 	f->next = freelist;
    264   1.1    brezak 	freelist = f;
    265   1.1    brezak }
    266