Tizen 2.1 base
[platform/upstream/gcd.git] / dispatch-1.0 / src / shims / mach.c
1 /*
2  * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
3  *
4  * @APPLE_APACHE_LICENSE_HEADER_START@
5  * 
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  * 
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  * 
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * 
18  * @APPLE_APACHE_LICENSE_HEADER_END@
19  */
20
21 #include "internal.h"
22
23 #if HAVE_MACH
24 void *
25 dispatch_mach_msg_get_context(mach_msg_header_t *msg)
26 {
27         mach_msg_context_trailer_t *tp;
28         void *context = NULL;
29         
30         tp = (mach_msg_context_trailer_t *)((uint8_t *)msg + round_msg(msg->msgh_size));
31         if (tp->msgh_trailer_size >= (mach_msg_size_t)sizeof(mach_msg_context_trailer_t)) {
32                 context = (void *)(uintptr_t)tp->msgh_context;
33         }
34         
35         return context;
36 }
37
38 #ifndef DISPATCH_NO_LEGACY
39 /*
40  * Raw Mach message support
41  */
42 boolean_t
43 _dispatch_machport_callback(mach_msg_header_t *msg, mach_msg_header_t *reply,
44   void (*callback)(mach_msg_header_t *))
45 {
46         mig_reply_setup(msg, reply);
47         ((mig_reply_error_t*)reply)->RetCode = MIG_NO_REPLY;
48
49         callback(msg);
50
51         return TRUE;
52 }
53
54 /*
55  * CFMachPort compatibility
56  */
57 boolean_t
58 _dispatch_CFMachPortCallBack(mach_msg_header_t *msg, mach_msg_header_t *reply,
59   void (*callback)(struct __CFMachPort *, void *msg, signed long size, void *))
60 {
61         mig_reply_setup(msg, reply);
62         ((mig_reply_error_t*)reply)->RetCode = MIG_NO_REPLY;
63         
64         callback(NULL, msg, msg->msgh_size, dispatch_mach_msg_get_context(msg));
65
66         return TRUE;
67 }
68 #endif
69 #endif /* HAVE_MACH */