scoped_mach_port.h 2.7 KB

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