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