|
|
1.1 root 1: /* 1.1.1.2 ! root 2: * Copyright (C) 1992 Wolfgang Solfrank. ! 3: * Copyright (C) 1992 TooLs GmbH. ! 4: * All rights reserved. ! 5: * ! 6: * Redistribution and use in source and binary forms, with or without ! 7: * modification, are permitted provided that the following conditions ! 8: * are met: ! 9: * 1. Redistributions of source code must retain the above copyright ! 10: * notice, this list of conditions and the following disclaimer. ! 11: * 2. Redistributions in binary form must reproduce the above copyright ! 12: * notice, this list of conditions and the following disclaimer in the ! 13: * documentation and/or other materials provided with the distribution. ! 14: * 3. All advertising materials mentioning features or use of this software ! 15: * must display the following acknowledgement: ! 16: * This product includes software developed by TooLs GmbH. ! 17: * 4. The name of TooLs GmbH may not be used to endorse or promote products ! 18: * derived from this software without specific prior written permission. ! 19: * ! 20: * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR ! 21: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ! 22: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ! 23: * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ! 24: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ! 25: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; ! 26: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ! 27: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR ! 28: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ! 29: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ! 30: */ ! 31: /* 1.1 root 32: * Copyright (c) 1982, 1986 Regents of the University of California. 33: * All rights reserved. 34: * 35: * Redistribution and use in source and binary forms, with or without 36: * modification, are permitted provided that the following conditions 37: * are met: 38: * 1. Redistributions of source code must retain the above copyright 39: * notice, this list of conditions and the following disclaimer. 40: * 2. Redistributions in binary form must reproduce the above copyright 41: * notice, this list of conditions and the following disclaimer in the 42: * documentation and/or other materials provided with the distribution. 43: * 3. All advertising materials mentioning features or use of this software 44: * must display the following acknowledgement: 45: * This product includes software developed by the University of 46: * California, Berkeley and its contributors. 47: * 4. Neither the name of the University nor the names of its contributors 48: * may be used to endorse or promote products derived from this software 49: * without specific prior written permission. 50: * 51: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61: * SUCH DAMAGE. 62: * 63: * from: @(#)subr_rmap.c 7.9 (Berkeley) 5/11/91 1.1.1.2 ! root 64: * subr_rmap.c,v 1.2 1993/07/07 06:00:15 cgd Exp 1.1 root 65: */ 66: 67: #include "param.h" 68: #include "systm.h" 69: #include "map.h" 70: #include "dmap.h" /* XXX */ 71: #include "proc.h" 72: #include "kernel.h" 73: 74: /* 75: * Resource map handling routines. 76: */ 77: 78: /* 1.1.1.2 ! root 79: * Initialize map mp to have (mapsize-1) segments 1.1 root 80: * and to be called ``name'', which we print if 81: * the slots become so fragmented that we lose space. 82: * The map itself is initialized with size elements free 83: * starting at addr. 84: */ 85: rminit(mp, size, addr, name, mapsize) 86: register struct map *mp; 87: long size, addr; 88: char *name; 89: int mapsize; 90: { 91: /* 92: * Body deleted. 93: */ 1.1.1.2 ! root 94: /* ! 95: * and reimplemented by ws. ! 96: * Assume small maps. Keep it sorted by addr with empty mapents last. ! 97: */ ! 98: register struct mapent *ep; ! 99: ! 100: /* mapsize had better be at least 2 */ ! 101: if (mapsize < 2 || addr <= 0 || size < 0) { ! 102: panic("rminit"); ! 103: } ! 104: mp->m_name = name; ! 105: mp->m_limit = (struct mapent *)mp + mapsize; ! 106: /* initially all is free */ ! 107: ep = (struct mapent *)mp + 1; ! 108: ep->m_size = size; ! 109: ep->m_addr = addr; ! 110: while (++ep < mp->m_limit) { ! 111: ep->m_addr = 0; ! 112: } 1.1 root 113: } 114: 115: long 116: rmalloc(mp, size) 117: register struct map *mp; 118: long size; 119: { 120: /* 121: * Body deleted. 122: */ 1.1.1.2 ! root 123: /* ! 124: * and reimplemented by ws. ! 125: */ ! 126: register struct mapent *ep, *fp; ! 127: long addr; ! 128: ! 129: /* first check arguments */ ! 130: if (size < 0) { ! 131: panic("rmalloc"); ! 132: } ! 133: if (!size) { ! 134: return 0; ! 135: } ! 136: addr = 0; ! 137: fp = 0; ! 138: /* try to find the smallest fit */ ! 139: for (ep = (struct mapent *)mp + 1; ep < mp->m_limit; ep++) { ! 140: if (!ep->m_addr) { ! 141: break; ! 142: } else if (ep->m_size == size) { ! 143: addr = ep->m_addr; ! 144: bcopy(ep + 1,ep,(char *)mp->m_limit - (char *)(ep + 1)); ! 145: return addr; ! 146: } else if (ep->m_size > size ! 147: && (!fp ! 148: || fp->m_size > ep->m_size)) { ! 149: fp = ep; ! 150: } ! 151: } ! 152: if (fp) { ! 153: addr = fp->m_addr; ! 154: fp->m_addr += size; ! 155: fp->m_size -= size; ! 156: } ! 157: return addr; 1.1 root 158: } 159: 160: rmfree(mp, size, addr) 161: struct map *mp; 162: long size, addr; 163: { 164: /* 165: * Body deleted. 166: */ 1.1.1.2 ! root 167: /* ! 168: * and reimplemented by ws. ! 169: */ ! 170: register struct mapent *ep, *fp; ! 171: ! 172: if (size <= 0 || addr <= 0) { ! 173: panic("rmfree"); ! 174: } ! 175: ! 176: while (1) { ! 177: fp = 0; ! 178: for (ep = (struct mapent *)mp + 1; ep < mp->m_limit; ep++) { ! 179: if (!ep->m_addr) { ! 180: break; ! 181: } ! 182: if (ep->m_addr + ep->m_size == addr) { ! 183: ep->m_size += size; ! 184: if (ep < mp->m_limit && ep[1].m_addr && (addr += size) >= ep[1].m_addr) { ! 185: if (addr > ep[1].m_addr) { ! 186: panic("rmfree"); ! 187: } ! 188: ep->m_size += ep[1].m_size; ! 189: bcopy(ep + 2, ep + 1, (char *)mp->m_limit - (char *)(ep + 2)); ! 190: } ! 191: return; ! 192: } ! 193: if (addr + size == ep->m_addr) { ! 194: ep->m_addr = addr; ! 195: ep->m_size += size; ! 196: return; ! 197: } ! 198: if (addr < ep->m_addr ! 199: && !mp->m_limit[-1].m_addr) { ! 200: bcopy(ep,ep + 1,(char *)(mp->m_limit - 1) - (char *)ep); ! 201: ep->m_addr = addr; ! 202: ep->m_size = size; ! 203: return; ! 204: } ! 205: if (!fp || fp->m_size > ep->m_size) { ! 206: fp = ep; ! 207: } ! 208: } ! 209: if (ep != (struct mapent *)mp + 1 ! 210: && ep[-1].m_addr + ep[-1].m_size == addr) { ! 211: (--ep)->m_size += size; ! 212: return; ! 213: } ! 214: if (ep != mp->m_limit) { ! 215: ep->m_addr = addr; ! 216: ep->m_size = size; ! 217: return; ! 218: } ! 219: /* sorry, have to loose space */ ! 220: /* fp contains the smallest slot */ ! 221: if (fp->m_size > size) { ! 222: printf("rmfree: map '%s' looses space (%d)\n",mp->m_name,size); ! 223: } else { ! 224: printf("rmfree: map '%s' looses space (%d)\n",mp->m_name,fp->m_size); ! 225: bcopy(fp + 1,fp,(char *)(mp->m_limit - 1) - (char *)fp); ! 226: mp->m_limit[-1].m_addr = 0; ! 227: /* now retry */ ! 228: } ! 229: } 1.1 root 230: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.