Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / base / memory / shared_memory_handle.h
blob01a7f75273dd617e95c54a1484c8f24d64acaad6
1 // Copyright 2015 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.
5 #ifndef BASE_MEMORY_SHARED_MEMORY_HANDLE_H_
6 #define BASE_MEMORY_SHARED_MEMORY_HANDLE_H_
8 #include "build/build_config.h"
10 #if defined(OS_WIN)
11 #include <windows.h>
12 #elif defined(OS_MACOSX)
13 #include <sys/types.h>
14 #include "base/base_export.h"
15 #include "base/file_descriptor_posix.h"
16 #include "base/macros.h"
17 #elif defined(OS_POSIX)
18 #include <sys/types.h>
19 #include "base/file_descriptor_posix.h"
20 #endif
22 namespace base {
24 class Pickle;
26 // SharedMemoryHandle is a platform specific type which represents
27 // the underlying OS handle to a shared memory segment.
28 #if defined(OS_WIN)
29 typedef HANDLE SharedMemoryHandle;
30 #elif defined(OS_POSIX) && !defined(OS_MACOSX)
31 typedef FileDescriptor SharedMemoryHandle;
32 #else
33 class BASE_EXPORT SharedMemoryHandle {
34 public:
35 enum Type {
36 // Indicates that the SharedMemoryHandle is backed by a POSIX fd.
37 POSIX,
38 // Indicates that the SharedMemoryHandle is backed by the Mach primitive
39 // "memory object".
40 MACH,
43 // The format that should be used to transmit |Type| over the wire.
44 typedef int TypeWireFormat;
46 // The default constructor returns an invalid SharedMemoryHandle.
47 SharedMemoryHandle();
49 // Constructs a SharedMemoryHandle backed by the components of a
50 // FileDescriptor. The newly created instance has the same ownership semantics
51 // as base::FileDescriptor. This typically means that the SharedMemoryHandle
52 // takes ownership of the |fd| if |auto_close| is true. Unfortunately, it's
53 // common for existing code to make shallow copies of SharedMemoryHandle, and
54 // the one that is finally passed into a base::SharedMemory is the one that
55 // "consumes" the fd.
56 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor);
57 SharedMemoryHandle(int fd, bool auto_close);
59 // Standard copy constructor. The new instance shares the underlying OS
60 // primitives.
61 SharedMemoryHandle(const SharedMemoryHandle& handle);
63 // Standard assignment operator. The updated instance shares the underlying
64 // OS primitives.
65 SharedMemoryHandle& operator=(const SharedMemoryHandle& handle);
67 // Duplicates the underlying OS resources.
68 SharedMemoryHandle Duplicate() const;
70 // Comparison operators.
71 bool operator==(const SharedMemoryHandle& handle) const;
72 bool operator!=(const SharedMemoryHandle& handle) const;
74 // Returns the type.
75 Type GetType() const;
77 // Whether the underlying OS primitive is valid.
78 bool IsValid() const;
80 // Sets the POSIX fd backing the SharedMemoryHandle. Requires that the
81 // SharedMemoryHandle be backed by a POSIX fd.
82 void SetFileHandle(int fd, bool auto_close);
84 // This method assumes that the SharedMemoryHandle is backed by a POSIX fd.
85 // This is eventually no longer going to be true, so please avoid adding new
86 // uses of this method.
87 const FileDescriptor GetFileDescriptor() const;
89 private:
90 Type type_;
91 FileDescriptor file_descriptor_;
93 #endif
95 } // namespace base
97 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_