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"
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"
26 // SharedMemoryHandle is a platform specific type which represents
27 // the underlying OS handle to a shared memory segment.
29 typedef HANDLE SharedMemoryHandle
;
30 #elif defined(OS_POSIX) && !defined(OS_MACOSX)
31 typedef FileDescriptor SharedMemoryHandle
;
33 class BASE_EXPORT SharedMemoryHandle
{
36 // Indicates that the SharedMemoryHandle is backed by a POSIX fd.
38 // Indicates that the SharedMemoryHandle is backed by the Mach primitive
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.
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
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
61 SharedMemoryHandle(const SharedMemoryHandle
& handle
);
63 // Standard assignment operator. The updated instance shares the underlying
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;
77 // Whether the underlying OS primitive is valid.
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;
91 FileDescriptor file_descriptor_
;
97 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_