| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_EDK_EMBEDDER_PLATFORM_SHARED_BUFFER_H_ | 5 #ifndef MOJO_EDK_EMBEDDER_PLATFORM_SHARED_BUFFER_H_ |
| 6 #define MOJO_EDK_EMBEDDER_PLATFORM_SHARED_BUFFER_H_ | 6 #define MOJO_EDK_EMBEDDER_PLATFORM_SHARED_BUFFER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 // |PlatformSharedBuffer| is a thread-safe, ref-counted wrapper around | 24 // |PlatformSharedBuffer| is a thread-safe, ref-counted wrapper around |
| 25 // OS-specific shared memory. It has the following features: | 25 // OS-specific shared memory. It has the following features: |
| 26 // - A |PlatformSharedBuffer| simply represents a piece of shared memory that | 26 // - A |PlatformSharedBuffer| simply represents a piece of shared memory that |
| 27 // *may* be mapped and *may* be shared to another process. | 27 // *may* be mapped and *may* be shared to another process. |
| 28 // - A single |PlatformSharedBuffer| may be mapped multiple times. The | 28 // - A single |PlatformSharedBuffer| may be mapped multiple times. The |
| 29 // lifetime of the mapping (owned by |PlatformSharedBufferMapping|) is | 29 // lifetime of the mapping (owned by |PlatformSharedBufferMapping|) is |
| 30 // separate from the lifetime of the |PlatformSharedBuffer|. | 30 // separate from the lifetime of the |PlatformSharedBuffer|. |
| 31 // - Sizes/offsets (of the shared memory and mappings) are arbitrary, and not | 31 // - Sizes/offsets (of the shared memory and mappings) are arbitrary, and not |
| 32 // restricted by page size. However, more memory may actually be mapped than | 32 // restricted by page size. However, more memory may actually be mapped than |
| 33 // requested. | 33 // requested. |
| 34 // | |
| 35 // It currently does NOT support the following: | |
| 36 // - Sharing read-only. (This will probably eventually be supported.) | |
| 37 class MOJO_SYSTEM_IMPL_EXPORT PlatformSharedBuffer | 34 class MOJO_SYSTEM_IMPL_EXPORT PlatformSharedBuffer |
| 38 : public base::RefCountedThreadSafe<PlatformSharedBuffer> { | 35 : public base::RefCountedThreadSafe<PlatformSharedBuffer> { |
| 39 public: | 36 public: |
| 40 // Creates a shared buffer of size |num_bytes| bytes (initially zero-filled). | 37 // Creates a shared buffer of size |num_bytes| bytes (initially zero-filled). |
| 41 // |num_bytes| must be nonzero. Returns null on failure. | 38 // |num_bytes| must be nonzero. Returns null on failure. |
| 42 static PlatformSharedBuffer* Create(size_t num_bytes); | 39 static PlatformSharedBuffer* Create(size_t num_bytes); |
| 43 | 40 |
| 44 // Creates a shared buffer of size |num_bytes| from the existing platform | 41 // Creates a shared buffer of size |num_bytes| from the existing platform |
| 45 // handle |platform_handle|. Returns null on failure. | 42 // handle |platform_handle|. Returns null on failure. |
| 46 static PlatformSharedBuffer* CreateFromPlatformHandle( | 43 static PlatformSharedBuffer* CreateFromPlatformHandle( |
| 47 size_t num_bytes, | 44 size_t num_bytes, |
| 45 bool read_only, |
| 48 ScopedPlatformHandle platform_handle); | 46 ScopedPlatformHandle platform_handle); |
| 49 | 47 |
| 50 // Creates a shared buffer of size |num_bytes| from the existing shared memory | 48 // Creates a shared buffer of size |num_bytes| from the existing shared memory |
| 51 // handle |handle|. | 49 // handle |handle|. |
| 52 static PlatformSharedBuffer* CreateFromSharedMemoryHandle( | 50 static PlatformSharedBuffer* CreateFromSharedMemoryHandle( |
| 53 size_t num_bytes, | 51 size_t num_bytes, |
| 54 bool read_only, | 52 bool read_only, |
| 55 base::SharedMemoryHandle handle); | 53 base::SharedMemoryHandle handle); |
| 56 | 54 |
| 57 // Gets the size of shared buffer (in number of bytes). | 55 // Gets the size of shared buffer (in number of bytes). |
| 58 size_t GetNumBytes() const; | 56 size_t GetNumBytes() const; |
| 59 | 57 |
| 58 // Returns whether this shared buffer is read-only. |
| 59 bool IsReadOnly() const; |
| 60 |
| 60 // Maps (some) of the shared buffer into memory; [|offset|, |offset + length|] | 61 // Maps (some) of the shared buffer into memory; [|offset|, |offset + length|] |
| 61 // must be contained in [0, |num_bytes|], and |length| must be at least 1. | 62 // must be contained in [0, |num_bytes|], and |length| must be at least 1. |
| 62 // Returns null on failure. | 63 // Returns null on failure. |
| 63 scoped_ptr<PlatformSharedBufferMapping> Map(size_t offset, size_t length); | 64 scoped_ptr<PlatformSharedBufferMapping> Map(size_t offset, size_t length); |
| 64 | 65 |
| 65 // Checks if |offset| and |length| are valid arguments. | 66 // Checks if |offset| and |length| are valid arguments. |
| 66 bool IsValidMap(size_t offset, size_t length); | 67 bool IsValidMap(size_t offset, size_t length); |
| 67 | 68 |
| 68 // Like |Map()|, but doesn't check its arguments (which should have been | 69 // Like |Map()|, but doesn't check its arguments (which should have been |
| 69 // preflighted using |IsValidMap()|). | 70 // preflighted using |IsValidMap()|). |
| 70 scoped_ptr<PlatformSharedBufferMapping> MapNoCheck(size_t offset, | 71 scoped_ptr<PlatformSharedBufferMapping> MapNoCheck(size_t offset, |
| 71 size_t length); | 72 size_t length); |
| 72 | 73 |
| 73 // Duplicates the underlying platform handle and passes it to the caller. | 74 // Duplicates the underlying platform handle and passes it to the caller. |
| 74 // TODO(vtl): On POSIX, we'll need two FDs to support sharing read-only. | |
| 75 ScopedPlatformHandle DuplicatePlatformHandle(); | 75 ScopedPlatformHandle DuplicatePlatformHandle(); |
| 76 | 76 |
| 77 // Duplicates the underlying shared memory handle and passes it to the caller. | 77 // Duplicates the underlying shared memory handle and passes it to the caller. |
| 78 base::SharedMemoryHandle DuplicateSharedMemoryHandle(); | 78 base::SharedMemoryHandle DuplicateSharedMemoryHandle(); |
| 79 | 79 |
| 80 // Passes the underlying platform handle to the caller. This should only be | 80 // Passes the underlying platform handle to the caller. This should only be |
| 81 // called if there's a unique reference to this object (owned by the caller). | 81 // called if there's a unique reference to this object (owned by the caller). |
| 82 // After calling this, this object should no longer be used, but should only | 82 // After calling this, this object should no longer be used, but should only |
| 83 // be disposed of. | 83 // be disposed of. |
| 84 ScopedPlatformHandle PassPlatformHandle(); | 84 ScopedPlatformHandle PassPlatformHandle(); |
| 85 | 85 |
| 86 // Create and return a read-only duplicate of this shared buffer. If this |
| 87 // shared buffer isn't capable of returning a read-only duplicate, then |
| 88 // nullptr will be returned. |
| 89 PlatformSharedBuffer* CreateReadOnlyDuplicate(); |
| 90 |
| 86 private: | 91 private: |
| 87 friend class base::RefCountedThreadSafe<PlatformSharedBuffer>; | 92 friend class base::RefCountedThreadSafe<PlatformSharedBuffer>; |
| 88 | 93 |
| 89 explicit PlatformSharedBuffer(size_t num_bytes); | 94 PlatformSharedBuffer(size_t num_bytes, bool read_only); |
| 90 ~PlatformSharedBuffer(); | 95 ~PlatformSharedBuffer(); |
| 91 | 96 |
| 92 // This is called by |Create()| before this object is given to anyone. | 97 // This is called by |Create()| before this object is given to anyone. |
| 93 bool Init(); | 98 bool Init(); |
| 94 | 99 |
| 95 // This is like |Init()|, but for |CreateFromPlatformHandle()|. (Note: It | 100 // This is like |Init()|, but for |CreateFromPlatformHandle()|. (Note: It |
| 96 // should verify that |platform_handle| is an appropriate handle for the | 101 // should verify that |platform_handle| is an appropriate handle for the |
| 97 // claimed |num_bytes_|.) | 102 // claimed |num_bytes_|.) |
| 98 bool InitFromPlatformHandle(ScopedPlatformHandle platform_handle); | 103 bool InitFromPlatformHandle(ScopedPlatformHandle platform_handle); |
| 99 | 104 |
| 100 void InitFromSharedMemoryHandle(base::SharedMemoryHandle handle); | 105 void InitFromSharedMemoryHandle(base::SharedMemoryHandle handle); |
| 101 | 106 |
| 102 const size_t num_bytes_; | 107 const size_t num_bytes_; |
| 108 const bool read_only_; |
| 103 | 109 |
| 104 base::Lock lock_; | 110 base::Lock lock_; |
| 105 scoped_ptr<base::SharedMemory> shared_memory_; | 111 scoped_ptr<base::SharedMemory> shared_memory_; |
| 106 | 112 |
| 107 DISALLOW_COPY_AND_ASSIGN(PlatformSharedBuffer); | 113 DISALLOW_COPY_AND_ASSIGN(PlatformSharedBuffer); |
| 108 }; | 114 }; |
| 109 | 115 |
| 110 // A mapping of a |PlatformSharedBuffer| (compararable to a "file view" in | 116 // A mapping of a |PlatformSharedBuffer| (compararable to a "file view" in |
| 111 // Windows); see above. Created by |PlatformSharedBuffer::Map()|. Automatically | 117 // Windows); see above. Created by |PlatformSharedBuffer::Map()|. Automatically |
| 112 // unmaps memory on destruction. | 118 // unmaps memory on destruction. |
| 113 // | 119 // |
| 114 // Mappings are NOT thread-safe. | 120 // Mappings are NOT thread-safe. |
| 115 // | 121 // |
| 116 // Note: This is an entirely separate class (instead of | 122 // Note: This is an entirely separate class (instead of |
| 117 // |PlatformSharedBuffer::Mapping|) so that it can be forward-declared. | 123 // |PlatformSharedBuffer::Mapping|) so that it can be forward-declared. |
| 118 class MOJO_SYSTEM_IMPL_EXPORT PlatformSharedBufferMapping { | 124 class MOJO_SYSTEM_IMPL_EXPORT PlatformSharedBufferMapping { |
| 119 public: | 125 public: |
| 120 ~PlatformSharedBufferMapping(); | 126 ~PlatformSharedBufferMapping(); |
| 121 | 127 |
| 122 void* GetBase() const; | 128 void* GetBase() const; |
| 123 size_t GetLength() const; | 129 size_t GetLength() const; |
| 124 | 130 |
| 125 private: | 131 private: |
| 126 friend class PlatformSharedBuffer; | 132 friend class PlatformSharedBuffer; |
| 127 | 133 |
| 128 PlatformSharedBufferMapping(base::SharedMemoryHandle handle, | 134 PlatformSharedBufferMapping(base::SharedMemoryHandle handle, |
| 135 bool read_only, |
| 129 size_t offset, | 136 size_t offset, |
| 130 size_t length) | 137 size_t length) |
| 131 : offset_(offset), | 138 : offset_(offset), |
| 132 length_(length), | 139 length_(length), |
| 133 base_(nullptr), | 140 base_(nullptr), |
| 134 shared_memory_(handle, false) {} | 141 shared_memory_(handle, read_only) {} |
| 135 | 142 |
| 136 bool Map(); | 143 bool Map(); |
| 137 void Unmap(); | 144 void Unmap(); |
| 138 | 145 |
| 139 const size_t offset_; | 146 const size_t offset_; |
| 140 const size_t length_; | 147 const size_t length_; |
| 141 void* base_; | 148 void* base_; |
| 142 | 149 |
| 143 // Since mapping life cycles are separate from PlatformSharedBuffer and a | 150 // Since mapping life cycles are separate from PlatformSharedBuffer and a |
| 144 // buffer can be mapped multiple times, we have our own SharedMemory object | 151 // buffer can be mapped multiple times, we have our own SharedMemory object |
| 145 // created from a duplicate handle. | 152 // created from a duplicate handle. |
| 146 base::SharedMemory shared_memory_; | 153 base::SharedMemory shared_memory_; |
| 147 | 154 |
| 148 DISALLOW_COPY_AND_ASSIGN(PlatformSharedBufferMapping); | 155 DISALLOW_COPY_AND_ASSIGN(PlatformSharedBufferMapping); |
| 149 }; | 156 }; |
| 150 | 157 |
| 151 } // namespace edk | 158 } // namespace edk |
| 152 } // namespace mojo | 159 } // namespace mojo |
| 153 | 160 |
| 154 #endif // MOJO_EDK_EMBEDDER_PLATFORM_SHARED_BUFFER_H_ | 161 #endif // MOJO_EDK_EMBEDDER_PLATFORM_SHARED_BUFFER_H_ |
| OLD | NEW |