1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_MEMORY_SHARED_MEMORY_HANDLE_H_
#define BASE_MEMORY_SHARED_MEMORY_HANDLE_H_
#include "build/build_config.h"
#if defined(OS_WIN)
#include <windows.h>
#include "base/process/process_handle.h"
#elif defined(OS_MACOSX) && !defined(OS_IOS)
#include <mach/mach.h>
#include <sys/types.h>
#include "base/base_export.h"
#include "base/file_descriptor_posix.h"
#include "base/macros.h"
#include "base/process/process_handle.h"
#elif defined(OS_POSIX)
#include <sys/types.h>
#include "base/file_descriptor_posix.h"
#endif
namespace base {
class Pickle;
// SharedMemoryHandle is a platform specific type which represents
// the underlying OS handle to a shared memory segment.
#if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS))
typedef FileDescriptor SharedMemoryHandle;
#elif defined(OS_WIN)
class BASE_EXPORT SharedMemoryHandle {
public:
// The default constructor returns an invalid SharedMemoryHandle.
SharedMemoryHandle();
SharedMemoryHandle(HANDLE h, base::ProcessId pid);
// Standard copy constructor. The new instance shares the underlying OS
// primitives.
SharedMemoryHandle(const SharedMemoryHandle& handle);
// Standard assignment operator. The updated instance shares the underlying
// OS primitives.
SharedMemoryHandle& operator=(const SharedMemoryHandle& handle);
// Comparison operators.
bool operator==(const SharedMemoryHandle& handle) const;
bool operator!=(const SharedMemoryHandle& handle) const;
// Closes the underlying OS resources.
void Close() const;
// Whether the underlying OS primitive is valid.
bool IsValid() const;
// Whether |pid_| is the same as the current process's id.
bool BelongsToCurrentProcess() const;
// Whether handle_ needs to be duplicated into the destination process when
// an instance of this class is passed over a Chrome IPC channel.
bool NeedsBrokering() const;
HANDLE GetHandle() const;
base::ProcessId GetPID() const;
private:
HANDLE handle_;
// The process in which |handle_| is valid and can be used. If |handle_| is
// invalid, this will be kNullProcessId.
base::ProcessId pid_;
};
#else
class BASE_EXPORT SharedMemoryHandle {
public:
enum Type {
// The SharedMemoryHandle is backed by a POSIX fd.
POSIX,
// The SharedMemoryHandle is backed by the Mach primitive "memory object".
MACH,
};
// The format that should be used to transmit |Type| over the wire.
typedef int TypeWireFormat;
// The default constructor returns an invalid SharedMemoryHandle.
SharedMemoryHandle();
// Constructs a SharedMemoryHandle backed by the components of a
// FileDescriptor. The newly created instance has the same ownership semantics
// as base::FileDescriptor. This typically means that the SharedMemoryHandle
// takes ownership of the |fd| if |auto_close| is true. Unfortunately, it's
// common for existing code to make shallow copies of SharedMemoryHandle, and
// the one that is finally passed into a base::SharedMemory is the one that
// "consumes" the fd.
explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor);
SharedMemoryHandle(int fd, bool auto_close);
// Makes a Mach-based SharedMemoryHandle of the given size. On error,
// subsequent calls to IsValid() return false.
explicit SharedMemoryHandle(mach_vm_size_t size);
// Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry
// in the task with process id |pid|. The memory region has size |size|.
SharedMemoryHandle(mach_port_t memory_object,
mach_vm_size_t size,
base::ProcessId pid);
// Standard copy constructor. The new instance shares the underlying OS
// primitives.
SharedMemoryHandle(const SharedMemoryHandle& handle);
// Standard assignment operator. The updated instance shares the underlying
// OS primitives.
SharedMemoryHandle& operator=(const SharedMemoryHandle& handle);
// Duplicates the underlying OS resources.
SharedMemoryHandle Duplicate() const;
// Comparison operators.
bool operator==(const SharedMemoryHandle& handle) const;
bool operator!=(const SharedMemoryHandle& handle) const;
// Returns the type.
Type GetType() const;
// Whether the underlying OS primitive is valid. Once the SharedMemoryHandle
// is backed by a valid OS primitive, it becomes immutable.
bool IsValid() const;
// Sets the POSIX fd backing the SharedMemoryHandle. Requires that the
// SharedMemoryHandle be backed by a POSIX fd.
void SetFileHandle(int fd, bool auto_close);
// This method assumes that the SharedMemoryHandle is backed by a POSIX fd.
// This is eventually no longer going to be true, so please avoid adding new
// uses of this method.
const FileDescriptor GetFileDescriptor() const;
// Exposed so that the SharedMemoryHandle can be transported between
// processes.
mach_port_t GetMemoryObject() const;
// Returns false on a failure to determine the size. On success, populates the
// output variable |size|.
bool GetSize(size_t* size) const;
// The SharedMemoryHandle must be valid.
// Returns whether the SharedMemoryHandle was successfully mapped into memory.
// On success, |memory| is an output variable that contains the start of the
// mapped memory.
bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only);
// Closes the underlying OS primitive.
void Close() const;
private:
// Shared code between copy constructor and operator=.
void CopyRelevantData(const SharedMemoryHandle& handle);
Type type_;
// Each instance of a SharedMemoryHandle is backed either by a POSIX fd or a
// mach port. |type_| determines the backing member.
union {
FileDescriptor file_descriptor_;
struct {
mach_port_t memory_object_;
// The size of the shared memory region when |type_| is MACH. Only
// relevant if |memory_object_| is not |MACH_PORT_NULL|.
mach_vm_size_t size_;
// The pid of the process in which |memory_object_| is usable. Only
// relevant if |memory_object_| is not |MACH_PORT_NULL|.
base::ProcessId pid_;
};
};
};
#endif
} // namespace base
#endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_
|