1/*
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
25 */
26/*
27 * (pre-GNU) HISTORY
28 *
29 * Revision 2.4 91/05/14 17:53:15 mrt
30 * Correcting copyright
31 *
32 * Revision 2.3 91/02/14 14:17:43 mrt
33 * Added new Mach copyright
34 * [91/02/13 12:44:15 mrt]
35 *
36 * Revision 2.2 90/08/06 17:24:22 rpd
37 * Created.
38 *
39 */
40
41#include <libc-pointer-arith.h>
42#if 1
43#include <mach.h>
44#else
45/* This is what CMU did, but that fails to declare some used functions. */
46#include <mach/port.h>
47#include <mach/message.h>
48#include <mach_init.h>
49#endif
50
51static void mach_msg_destroy_port(mach_port_t, mach_msg_type_name_t);
52static void mach_msg_destroy_memory(vm_offset_t, vm_size_t);
53
54/*
55 * Routine: mach_msg_destroy
56 * Purpose:
57 * Deallocates all port rights and out-of-line memory
58 * found in a received message.
59 */
60
61void
62__mach_msg_destroy (mach_msg_header_t *msg)
63{
64 mach_msg_bits_t mbits = msg->msgh_bits;
65
66 /*
67 * The msgh_local_port field doesn't hold a port right.
68 * The receive operation consumes the destination port right.
69 */
70
71 mach_msg_destroy_port(msg->msgh_remote_port, MACH_MSGH_BITS_REMOTE(mbits));
72
73 if (mbits & MACH_MSGH_BITS_COMPLEX) {
74 vm_offset_t saddr;
75 vm_offset_t eaddr;
76
77 saddr = (vm_offset_t) (msg + 1);
78 eaddr = (vm_offset_t) msg + msg->msgh_size;
79
80 while (saddr < eaddr) {
81 mach_msg_type_long_t *type;
82 mach_msg_type_name_t name;
83 mach_msg_type_size_t size;
84 mach_msg_type_number_t number;
85 boolean_t is_inline;
86 vm_size_t length;
87 vm_offset_t addr;
88
89 type = (mach_msg_type_long_t *) saddr;
90 is_inline = type->msgtl_header.msgt_inline;
91 if (type->msgtl_header.msgt_longform) {
92 name = type->msgtl_name;
93 size = type->msgtl_size;
94 number = type->msgtl_number;
95 saddr += sizeof(mach_msg_type_long_t);
96 } else {
97 name = type->msgtl_header.msgt_name;
98 size = type->msgtl_header.msgt_size;
99 number = type->msgtl_header.msgt_number;
100 saddr += sizeof(mach_msg_type_t);
101 }
102
103 /* Calculate length of data in bytes... */
104 length = ((number * size) + 7) >> 3;
105 /* ... and round up using uintptr_t alignment */
106 length = ALIGN_UP (length, __alignof__ (uintptr_t));
107
108 addr = is_inline ? saddr : * (vm_offset_t *) saddr;
109
110 if (MACH_MSG_TYPE_PORT_ANY(name)) {
111 mach_msg_type_number_t i;
112
113 if (is_inline) {
114 mach_port_name_inlined_t *inlined_ports = (mach_port_name_inlined_t *)addr;
115 for (i = 0; i < number; i++)
116 mach_msg_destroy_port(inlined_ports[i].name, name);
117 } else {
118 mach_port_t *ports = (mach_port_t *) addr;
119 for (i = 0; i < number; i++)
120 mach_msg_destroy_port(ports[i], name);
121 }
122 }
123
124 if (is_inline) {
125 saddr += length;
126 } else {
127 mach_msg_destroy_memory(addr, length);
128 saddr += sizeof(vm_offset_t);
129 }
130 }
131 }
132}
133
134weak_alias (__mach_msg_destroy, mach_msg_destroy)
135libc_hidden_def (__mach_msg_destroy)
136
137static void
138mach_msg_destroy_port (mach_port_t port, mach_msg_type_name_t type)
139{
140 if (MACH_PORT_VALID(port)) switch (type) {
141 case MACH_MSG_TYPE_MOVE_SEND:
142 case MACH_MSG_TYPE_MOVE_SEND_ONCE:
143 /* destroy the send/send-once right */
144 (void) __mach_port_deallocate(mach_task_self(), port);
145 break;
146
147 case MACH_MSG_TYPE_MOVE_RECEIVE:
148 /* destroy the receive right */
149 (void) __mach_port_mod_refs(mach_task_self(), port,
150 MACH_PORT_RIGHT_RECEIVE, -1);
151 break;
152
153 case MACH_MSG_TYPE_MAKE_SEND:
154 /* create a send right and then destroy it */
155 (void) __mach_port_insert_right(mach_task_self(), port,
156 port, MACH_MSG_TYPE_MAKE_SEND);
157 (void) __mach_port_deallocate(mach_task_self(), port);
158 break;
159
160 case MACH_MSG_TYPE_MAKE_SEND_ONCE:
161 /* create a send-once right and then destroy it */
162 (void) __mach_port_extract_right(mach_task_self(), port,
163 MACH_MSG_TYPE_MAKE_SEND_ONCE,
164 &port, &type);
165 (void) __mach_port_deallocate(mach_task_self(), port);
166 break;
167 }
168}
169
170static void
171mach_msg_destroy_memory (vm_offset_t addr, vm_size_t size)
172{
173 if (size > 0)
174 (void) __vm_deallocate(__mach_task_self(), addr, size);
175}
176

source code of glibc/mach/msg-destroy.c