2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
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.
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.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 * File: ipc/ipc_table.c
57 * Functions to manipulate tables of IPC capabilities.
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>
69 * Forward declarations
78 * We borrow the kalloc map, rather than creating
79 * yet another submap of the kernel map.
82 extern vm_map_t kalloc_map
;
84 ipc_table_size_t ipc_table_entries
;
85 unsigned int ipc_table_entries_size
= 512;
87 ipc_table_size_t ipc_table_dnrequests
;
88 unsigned int ipc_table_dnrequests_size
= 64;
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 */
98 vm_size_t minsize
= min
* elemsize
;
102 /* first use powers of two, up to the page size */
104 for (index
= 0, size
= 1;
105 (index
< num
) && (size
< PAGE_SIZE
);
107 if (size
>= minsize
) {
108 its
[index
].its_size
= size
/ elemsize
;
113 /* then increments of a page, then two pages, etc. */
115 for (incrsize
= PAGE_SIZE
; index
< num
;) {
119 (period
< 15) && (index
< num
);
120 period
++, size
+= incrsize
) {
121 if (size
>= minsize
) {
122 its
[index
].its_size
= size
/ elemsize
;
126 if (incrsize
< (PAGE_SIZE
<< 3))
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
);
139 ipc_table_fill(ipc_table_entries
, ipc_table_entries_size
- 1,
140 16, sizeof(struct ipc_entry
));
142 /* the last two elements should have the same size */
144 ipc_table_entries
[ipc_table_entries_size
- 1].its_size
=
145 ipc_table_entries
[ipc_table_entries_size
- 2].its_size
;
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
);
153 ipc_table_fill(ipc_table_dnrequests
, ipc_table_dnrequests_size
- 1,
154 2, sizeof(struct ipc_port_request
));
156 /* the last element should have zero size */
158 ipc_table_dnrequests
[ipc_table_dnrequests_size
- 1].its_size
= 0;
162 * Routine: ipc_table_alloc
175 if (size
< PAGE_SIZE
)
178 if (kmem_alloc(kalloc_map
, &table
, size
) != KERN_SUCCESS
)
181 return (void *)table
;
185 * Routine: ipc_table_realloc
187 * Reallocate a big table.
189 * The new table remaps the old table,
190 * so copying is not necessary.
192 * Only works for page-size or bigger tables.
202 vm_offset_t new_table
;
204 if (kmem_realloc(kalloc_map
,
205 (vm_offset_t
) old_table
, old_size
,
206 &new_table
, new_size
) != KERN_SUCCESS
)
209 return (void *)new_table
;
213 * Routine: ipc_table_free
215 * Free a table allocated with ipc_table_alloc or
226 if (size
< PAGE_SIZE
)
229 kmem_free(kalloc_map
, (vm_offset_t
)table
, size
);