Chromium Code Reviews| 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..b9336afa688dea6e99532f974aef29e30f83f030 |
| --- /dev/null |
| +++ b/base/memory/shared_memory_handle.h |
| @@ -0,0 +1,90 @@ |
| +// 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 { |
| + |
| +class Pickle; |
| + |
| +// 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 Type { |
| + // Indicates that the SharedMemoryHandle is backed by a POSIX fd. |
| + POSIX, |
| + // Indicates that the SharedMemoryHandle is backed by the Mach primitive |
| + // "memory object". |
| + MACH, |
| + }; |
| + |
| + // The default constructor returns an invalid SharedMemoryHandle. |
| + SharedMemoryHandle(); |
| + |
| + // Constructs a SharedMemoryHandle backed by |file_descriptor|. |
|
Tom Sepez
2015/06/17 18:51:55
nit: comment about who owns the FD. Must the call
erikchen
2015/06/18 17:58:33
I've added a long comment indicating the ownership
|
| + 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); |
| + |
| + // Duplicates the underlying OS resources. |
| + SharedMemoryHandle Duplicate() const; |
| + |
| + // Comparison operators. |
| + bool operator==(const SharedMemoryHandle& handle) const; |
| + bool operator!=(const SharedMemoryHandle& handle) const; |
| + |
| + // Getter and setter for a property. |
| + Type GetType() const; |
| + void SetType(Type type); |
|
Tom Sepez
2015/06/17 18:51:55
Can the type change after the SMH is created? I'd
erikchen
2015/06/18 17:58:33
Yes, since code that uses base::SharedMemoryHandle
|
| + |
| + // 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; |
| + |
| + private: |
| + Type type_; |
|
Tom Sepez
2015/06/17 18:51:55
const Type type_; per above.
erikchen
2015/06/18 17:58:33
That won't work, since SharedMemoryHandles are pas
|
| + FileDescriptor file_descriptor_; |
| +}; |
| +#elif defined(OS_POSIX) |
|
Tom Sepez
2015/06/17 18:51:55
nit: I might re-arrange this so that win/posix cas
erikchen
2015/06/18 17:58:33
Good suggestion, done.
|
| +typedef FileDescriptor SharedMemoryHandle; |
| +#endif |
| + |
| +} // namespace base |
| + |
| +#endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ |