|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. ! 3: * ! 4: * @APPLE_LICENSE_HEADER_START@ ! 5: * ! 6: * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights ! 7: * Reserved. This file contains Original Code and/or Modifications of ! 8: * Original Code as defined in and that are subject to the Apple Public ! 9: * Source License Version 1.1 (the "License"). You may not use this file ! 10: * except in compliance with the License. Please obtain a copy of the ! 11: * License at http://www.apple.com/publicsource and read it before using ! 12: * this file. ! 13: * ! 14: * The Original Code and all software distributed under the License are ! 15: * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER ! 16: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, ! 17: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, ! 18: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the ! 19: * License for the specific language governing rights and limitations ! 20: * under the License. ! 21: * ! 22: * @APPLE_LICENSE_HEADER_END@ ! 23: */ ! 24: ! 25: /* ! 26: * Copyright (c) 1995, 1994, 1993, 1992, 1991, 1990 ! 27: * Open Software Foundation, Inc. ! 28: * ! 29: * Permission to use, copy, modify, and distribute this software and ! 30: * its documentation for any purpose and without fee is hereby granted, ! 31: * provided that the above copyright notice appears in all copies and ! 32: * that both the copyright notice and this permission notice appear in ! 33: * supporting documentation, and that the name of ("OSF") or Open Software ! 34: * Foundation not be used in advertising or publicity pertaining to ! 35: * distribution of the software without specific, written prior permission. ! 36: * ! 37: * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE ! 38: * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS ! 39: * FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL OSF BE LIABLE FOR ANY ! 40: * SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES ! 41: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN ! 42: * ACTION OF CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING ! 43: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE ! 44: */ ! 45: /* ! 46: * OSF Research Institute MK6.1 (unencumbered) 1/31/1995 ! 47: */ ! 48: /* ! 49: * Mach Operating System ! 50: * Copyright (c) 1991,1990,1989 Carnegie Mellon University ! 51: * All Rights Reserved. ! 52: * ! 53: * Permission to use, copy, modify and distribute this software and its ! 54: * documentation is hereby granted, provided that both the copyright ! 55: * notice and this permission notice appear in all copies of the ! 56: * software, derivative works or modified versions, and any portions ! 57: * thereof, and that both notices appear in supporting documentation. ! 58: * ! 59: * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" ! 60: * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR ! 61: * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. ! 62: * ! 63: * Carnegie Mellon requests users of this software to return to ! 64: * ! 65: * Software Distribution Coordinator or [email protected] ! 66: * School of Computer Science ! 67: * Carnegie Mellon University ! 68: * Pittsburgh PA 15213-3890 ! 69: * ! 70: * any improvements or extensions that they make and grant Carnegie Mellon ! 71: * the rights to redistribute these changes. ! 72: */ ! 73: /* ! 74: * File: ipc/ipc_table.c ! 75: * Author: Rich Draves ! 76: * Date: 1989 ! 77: * ! 78: * Functions to manipulate tables of IPC capabilities. ! 79: */ ! 80: ! 81: #include <mach/kern_return.h> ! 82: #include <mach/vm_param.h> ! 83: #include <ipc/ipc_table.h> ! 84: #include <ipc/ipc_port.h> ! 85: #include <ipc/ipc_entry.h> ! 86: #include <kern/kalloc.h> ! 87: #include <vm/vm_kern.h> ! 88: ! 89: /* ! 90: * Forward declarations ! 91: */ ! 92: void ipc_table_fill( ! 93: ipc_table_size_t its, ! 94: unsigned int num, ! 95: unsigned int min, ! 96: vm_size_t elemsize); ! 97: ! 98: /* ! 99: * We borrow the kalloc map, rather than creating ! 100: * yet another submap of the kernel map. ! 101: */ ! 102: ! 103: extern vm_map_t kalloc_map; ! 104: ! 105: ipc_table_size_t ipc_table_entries; ! 106: unsigned int ipc_table_entries_size = 512; ! 107: ! 108: ipc_table_size_t ipc_table_dnrequests; ! 109: unsigned int ipc_table_dnrequests_size = 64; ! 110: ! 111: void ! 112: ipc_table_fill( ! 113: ipc_table_size_t its, /* array to fill */ ! 114: unsigned int num, /* size of array */ ! 115: unsigned int min, /* at least this many elements */ ! 116: vm_size_t elemsize) /* size of elements */ ! 117: { ! 118: unsigned int index; ! 119: vm_size_t minsize = min * elemsize; ! 120: vm_size_t size; ! 121: vm_size_t incrsize; ! 122: ! 123: /* first use powers of two, up to the page size */ ! 124: ! 125: for (index = 0, size = 1; ! 126: (index < num) && (size < PAGE_SIZE); ! 127: size <<= 1) { ! 128: if (size >= minsize) { ! 129: its[index].its_size = size / elemsize; ! 130: index++; ! 131: } ! 132: } ! 133: ! 134: /* then increments of a page, then two pages, etc. */ ! 135: ! 136: for (incrsize = PAGE_SIZE; index < num;) { ! 137: unsigned int period; ! 138: ! 139: for (period = 0; ! 140: (period < 15) && (index < num); ! 141: period++, size += incrsize) { ! 142: if (size >= minsize) { ! 143: its[index].its_size = size / elemsize; ! 144: index++; ! 145: } ! 146: } ! 147: if (incrsize < (PAGE_SIZE << 3)) ! 148: incrsize <<= 1; ! 149: } ! 150: } ! 151: ! 152: void ! 153: ipc_table_init(void) ! 154: { ! 155: ipc_table_entries = (ipc_table_size_t) ! 156: kalloc(sizeof(struct ipc_table_size) * ! 157: ipc_table_entries_size); ! 158: assert(ipc_table_entries != ITS_NULL); ! 159: ! 160: ipc_table_fill(ipc_table_entries, ipc_table_entries_size - 1, ! 161: 4, sizeof(struct ipc_entry)); ! 162: ! 163: /* the last two elements should have the same size */ ! 164: ! 165: ipc_table_entries[ipc_table_entries_size - 1].its_size = ! 166: ipc_table_entries[ipc_table_entries_size - 2].its_size; ! 167: ! 168: ! 169: ipc_table_dnrequests = (ipc_table_size_t) ! 170: kalloc(sizeof(struct ipc_table_size) * ! 171: ipc_table_dnrequests_size); ! 172: assert(ipc_table_dnrequests != ITS_NULL); ! 173: ! 174: ipc_table_fill(ipc_table_dnrequests, ipc_table_dnrequests_size - 1, ! 175: 2, sizeof(struct ipc_port_request)); ! 176: ! 177: /* the last element should have zero size */ ! 178: ! 179: ipc_table_dnrequests[ipc_table_dnrequests_size - 1].its_size = 0; ! 180: } ! 181: ! 182: /* ! 183: * Routine: ipc_table_alloc ! 184: * Purpose: ! 185: * Allocate a table. ! 186: * Conditions: ! 187: * May block. ! 188: */ ! 189: ! 190: vm_offset_t ! 191: ipc_table_alloc( ! 192: vm_size_t size) ! 193: { ! 194: vm_offset_t table; ! 195: ! 196: if (size < PAGE_SIZE) ! 197: table = kalloc(size); ! 198: else ! 199: if (kmem_alloc(kalloc_map, &table, size) != KERN_SUCCESS) ! 200: table = 0; ! 201: ! 202: return table; ! 203: } ! 204: ! 205: /* ! 206: * Routine: ipc_table_realloc ! 207: * Purpose: ! 208: * Reallocate a big table. ! 209: * ! 210: * The new table remaps the old table, ! 211: * so copying is not necessary. ! 212: * Conditions: ! 213: * Only works for page-size or bigger tables. ! 214: * May block. ! 215: */ ! 216: ! 217: vm_offset_t ! 218: ipc_table_realloc( ! 219: vm_size_t old_size, ! 220: vm_offset_t old_table, ! 221: vm_size_t new_size) ! 222: { ! 223: vm_offset_t new_table; ! 224: ! 225: if (kmem_realloc(kalloc_map, old_table, old_size, ! 226: &new_table, new_size) != KERN_SUCCESS) ! 227: new_table = 0; ! 228: ! 229: return new_table; ! 230: } ! 231: ! 232: /* ! 233: * Routine: ipc_table_free ! 234: * Purpose: ! 235: * Free a table allocated with ipc_table_alloc or ! 236: * ipc_table_realloc. ! 237: * Conditions: ! 238: * May block. ! 239: */ ! 240: ! 241: void ! 242: ipc_table_free( ! 243: vm_size_t size, ! 244: vm_offset_t table) ! 245: { ! 246: if (size < PAGE_SIZE) ! 247: kfree(table, size); ! 248: else ! 249: kmem_free(kalloc_map, table, size); ! 250: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.