scoped_mach_port.h 2.7 KB

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