xref: /aosp_15_r20/external/cronet/base/apple/scoped_mach_port.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_APPLE_SCOPED_MACH_PORT_H_
6 #define BASE_APPLE_SCOPED_MACH_PORT_H_
7 
8 #include <mach/mach.h>
9 
10 #include <optional>
11 
12 #include "base/base_export.h"
13 #include "base/scoped_generic.h"
14 
15 namespace base::apple {
16 
17 namespace internal {
18 
19 struct BASE_EXPORT SendRightTraits {
InvalidValueSendRightTraits20   static mach_port_t InvalidValue() {
21     return MACH_PORT_NULL;
22   }
23 
24   BASE_EXPORT static void Free(mach_port_t port);
25 };
26 
27 struct BASE_EXPORT ReceiveRightTraits {
InvalidValueReceiveRightTraits28   static mach_port_t InvalidValue() {
29     return MACH_PORT_NULL;
30   }
31 
32   BASE_EXPORT static void Free(mach_port_t port);
33 };
34 
35 struct PortSetTraits {
InvalidValuePortSetTraits36   static mach_port_t InvalidValue() {
37     return MACH_PORT_NULL;
38   }
39 
40   BASE_EXPORT static void Free(mach_port_t port);
41 };
42 
43 }  // namespace internal
44 
45 // A scoper for handling a Mach port that names a send right. Send rights are
46 // reference counted, and this takes ownership of the right on construction
47 // and then removes a reference to the right on destruction. If the reference
48 // is the last one on the right, the right is deallocated.
49 using ScopedMachSendRight =
50     ScopedGeneric<mach_port_t, internal::SendRightTraits>;
51 
52 // A scoper for handling a Mach port's receive right. There is only one
53 // receive right per port. This takes ownership of the receive right on
54 // construction and then destroys the right on destruction, turning all
55 // outstanding send rights into dead names.
56 using ScopedMachReceiveRight =
57     ScopedGeneric<mach_port_t, internal::ReceiveRightTraits>;
58 
59 // A scoper for handling a Mach port set. A port set can have only one
60 // reference. This takes ownership of that single reference on construction and
61 // destroys the port set on destruction. Destroying a port set does not destroy
62 // the receive rights that are members of the port set.
63 using ScopedMachPortSet = ScopedGeneric<mach_port_t, internal::PortSetTraits>;
64 
65 // Constructs a Mach port receive right and places the result in |receive|.
66 // If |send| is non-null, a send right will be created as well and stored
67 // there. If |queue_limit| is specified, the receive right will be constructed
68 // with the specified mpo_qlmit. Returns true on success and false on failure.
69 BASE_EXPORT bool CreateMachPort(
70     ScopedMachReceiveRight* receive,
71     ScopedMachSendRight* send,
72     std::optional<mach_port_msgcount_t> queue_limit = std::nullopt);
73 
74 // Increases the user reference count for MACH_PORT_RIGHT_SEND by 1 and returns
75 // a new scoper to manage the additional right.
76 BASE_EXPORT ScopedMachSendRight RetainMachSendRight(mach_port_t port);
77 
78 }  // namespace base::apple
79 
80 #endif  // BASE_APPLE_SCOPED_MACH_PORT_H_
81