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