platform_shared_memory_handle.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2022 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_MEMORY_PLATFORM_SHARED_MEMORY_HANDLE_H_
  5. #define BASE_MEMORY_PLATFORM_SHARED_MEMORY_HANDLE_H_
  6. #include "build/build_config.h"
  7. #if BUILDFLAG(IS_APPLE)
  8. #include <mach/mach.h>
  9. #include "base/mac/scoped_mach_port.h"
  10. #elif BUILDFLAG(IS_FUCHSIA)
  11. #include <lib/zx/vmo.h>
  12. #elif BUILDFLAG(IS_WIN)
  13. #include "base/win/scoped_handle.h"
  14. #include "base/win/windows_types.h"
  15. #elif BUILDFLAG(IS_POSIX)
  16. #include <sys/types.h>
  17. #include "base/files/scoped_file.h"
  18. #endif
  19. namespace base::subtle {
  20. #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_ANDROID)
  21. // Helper structs to keep two descriptors on POSIX. It's needed to support
  22. // ConvertToReadOnly().
  23. struct BASE_EXPORT FDPair {
  24. // The main shared memory descriptor that is used for mapping. May be either
  25. // writable or read-only, depending on region's mode.
  26. int fd;
  27. // The read-only descriptor, valid only in kWritable mode. Replaces |fd| when
  28. // a region is converted to read-only.
  29. int readonly_fd;
  30. };
  31. struct BASE_EXPORT ScopedFDPair {
  32. ScopedFDPair();
  33. ScopedFDPair(ScopedFD in_fd, ScopedFD in_readonly_fd);
  34. ScopedFDPair(ScopedFDPair&&);
  35. ScopedFDPair& operator=(ScopedFDPair&&);
  36. ~ScopedFDPair();
  37. FDPair get() const;
  38. ScopedFD fd;
  39. ScopedFD readonly_fd;
  40. };
  41. #endif
  42. // Platform-specific shared memory type used by the shared memory system.
  43. #if BUILDFLAG(IS_APPLE)
  44. using PlatformSharedMemoryHandle = mach_port_t;
  45. using ScopedPlatformSharedMemoryHandle = mac::ScopedMachSendRight;
  46. #elif BUILDFLAG(IS_FUCHSIA)
  47. using PlatformSharedMemoryHandle = zx::unowned_vmo;
  48. using ScopedPlatformSharedMemoryHandle = zx::vmo;
  49. #elif BUILDFLAG(IS_WIN)
  50. using PlatformSharedMemoryHandle = HANDLE;
  51. using ScopedPlatformSharedMemoryHandle = win::ScopedHandle;
  52. #elif BUILDFLAG(IS_ANDROID)
  53. using PlatformSharedMemoryHandle = int;
  54. using ScopedPlatformSharedMemoryHandle = ScopedFD;
  55. #else
  56. using PlatformSharedMemoryHandle = FDPair;
  57. using ScopedPlatformSharedMemoryHandle = ScopedFDPair;
  58. #endif
  59. } // namespace base::subtle
  60. #endif // BASE_MEMORY_PLATFORM_SHARED_MEMORY_HANDLE_H_