Index: base/memory/shared_memory_handle.h |
diff --git a/base/memory/shared_memory_handle.h b/base/memory/shared_memory_handle.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4db2004f1587ff7e9b51e39aabcac1d89252b990 |
--- /dev/null |
+++ b/base/memory/shared_memory_handle.h |
@@ -0,0 +1,98 @@ |
+// 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> |
+#elif defined(OS_MACOSX) && !defined(OS_IOS) |
+#include <sys/types.h> |
+#include "base/base_export.h" |
+#include "base/file_descriptor_posix.h" |
+#include "base/macros.h" |
+#elif defined(OS_POSIX) |
+#include <sys/types.h> |
+#include "base/file_descriptor_posix.h" |
+#endif |
+ |
+namespace base { |
+ |
+// SharedMemoryHandle is a platform specific type which represents |
+// the underlying OS handle to a shared memory segment. |
+#if defined(OS_WIN) |
+typedef HANDLE SharedMemoryHandle; |
+#elif defined(OS_MACOSX) && !defined(OS_IOS) |
+ |
+class BASE_EXPORT SharedMemoryHandle { |
+ public: |
+ enum Mechanism { |
+ // Indicates that the SharedMemoryHandle is backed by a POSIX fd. |
+ POSIX_FD = 0, |
Robert Sesek
2015/06/05 19:27:14
I think just POSIX and MACH are better names.
erikchen
2015/06/06 01:47:00
Done.
|
+ // Indicates that the SharedMemoryHandle is backed by the Mach primitive |
+ // "memory object". |
+ MACH_PRIMITIVE = 1, |
+ }; |
+ |
+ // The default constructor returns an invalid SharedMemoryHandle. |
+ SharedMemoryHandle(); |
+ |
+ // Constructs a SharedMemoryHandle backed by |file_descriptor|. |
+ explicit SharedMemoryHandle(const FileDescriptor& file_descriptor); |
+ |
+ // Constructs a SharedMemoryHandle backed by the components of a |
+ // FileDescriptor. |
+ SharedMemoryHandle(int fd, bool auto_close); |
+ |
+ // 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; |
+ |
+ // Returns the mechanism that backs the SharedMemoryHandle for wire transfer. |
+ uint32_t GetMechanismForWire() const; |
Robert Sesek
2015/06/05 19:27:14
These names are kind of awkward. Why not just have
erikchen
2015/06/06 01:47:00
I moved the mechanism logic into this class, but t
|
+ |
+ // Sets the mechanism used to back the SharedMemoryHandle. |
+ // Returns whether |mechanism| was a valid input. |
+ bool SetMechanismFromWire(uint32_t mechanism); |
+ |
+ // Whether the instance is backed by a POSIX fd. |
+ bool IsBackedByPOSIXFd() const; |
+ |
+ // Returns the POSIX fd backing the SharedMemoryHandle. Requires that the |
+ // SharedMemoryHandle be backed by a POSIX fd. |
+ int GetFileHandle() 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); |
+ |
+ // Returns the address of the POSIX FileDescriptor backing the |
+ // SharedMemoryHandle. |
+ FileDescriptor* GetFileDescriptor(); |
+ const FileDescriptor* GetFileDescriptor() const; |
+ |
+ // Duplicates the underlying OS resources. |
+ SharedMemoryHandle Duplicate() const; |
+ |
+ private: |
+ Mechanism mechanism_; |
+ FileDescriptor file_descriptor_; |
+}; |
+#elif defined(OS_POSIX) |
+typedef FileDescriptor SharedMemoryHandle; |
+#endif |
+ |
+} // namespace base |
+ |
+#endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ |