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