| 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_SIMPLE_PLATFORM_SHARED_BUFFER_H_ | 5 #ifndef MOJO_EDK_EMBEDDER_SIMPLE_PLATFORM_SHARED_BUFFER_H_ |
| 6 #define MOJO_EDK_EMBEDDER_SIMPLE_PLATFORM_SHARED_BUFFER_H_ | 6 #define MOJO_EDK_EMBEDDER_SIMPLE_PLATFORM_SHARED_BUFFER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/memory/shared_memory.h" |
| 12 #include "base/synchronization/lock.h" |
| 10 #include "mojo/edk/embedder/platform_shared_buffer.h" | 13 #include "mojo/edk/embedder/platform_shared_buffer.h" |
| 11 #include "mojo/edk/system/system_impl_export.h" | 14 #include "mojo/edk/system/system_impl_export.h" |
| 12 #include "mojo/public/cpp/system/macros.h" | 15 #include "mojo/public/cpp/system/macros.h" |
| 13 | 16 |
| 14 namespace mojo { | 17 namespace mojo { |
| 15 namespace edk { | 18 namespace edk { |
| 16 | 19 |
| 17 // A simple implementation of |PlatformSharedBuffer|. | 20 // A simple implementation of |PlatformSharedBuffer| that uses |
| 21 // |base::SharedMemory|. |
| 18 class MOJO_SYSTEM_IMPL_EXPORT SimplePlatformSharedBuffer final | 22 class MOJO_SYSTEM_IMPL_EXPORT SimplePlatformSharedBuffer final |
| 19 : public PlatformSharedBuffer { | 23 : public PlatformSharedBuffer { |
| 20 public: | 24 public: |
| 21 // Creates a shared buffer of size |num_bytes| bytes (initially zero-filled). | 25 // Creates a shared buffer of size |num_bytes| bytes (initially zero-filled). |
| 22 // |num_bytes| must be nonzero. Returns null on failure. | 26 // |num_bytes| must be nonzero. Returns null on failure. |
| 23 static SimplePlatformSharedBuffer* Create(size_t num_bytes); | 27 static SimplePlatformSharedBuffer* Create(size_t num_bytes); |
| 24 | 28 |
| 25 static SimplePlatformSharedBuffer* CreateFromPlatformHandle( | 29 static SimplePlatformSharedBuffer* CreateFromPlatformHandle( |
| 26 size_t num_bytes, | 30 size_t num_bytes, |
| 27 ScopedPlatformHandle platform_handle); | 31 ScopedPlatformHandle platform_handle); |
| 28 | 32 |
| 29 // |PlatformSharedBuffer| implementation: | 33 // |PlatformSharedBuffer| implementation: |
| 30 size_t GetNumBytes() const override; | 34 size_t GetNumBytes() const override; |
| 31 scoped_ptr<PlatformSharedBufferMapping> Map(size_t offset, | 35 scoped_ptr<PlatformSharedBufferMapping> Map(size_t offset, |
| 32 size_t length) override; | 36 size_t length) override; |
| 33 bool IsValidMap(size_t offset, size_t length) override; | 37 bool IsValidMap(size_t offset, size_t length) override; |
| 34 scoped_ptr<PlatformSharedBufferMapping> MapNoCheck(size_t offset, | 38 scoped_ptr<PlatformSharedBufferMapping> MapNoCheck(size_t offset, |
| 35 size_t length) override; | 39 size_t length) override; |
| 36 ScopedPlatformHandle DuplicatePlatformHandle() override; | 40 ScopedPlatformHandle DuplicatePlatformHandle() override; |
| 37 ScopedPlatformHandle PassPlatformHandle() override; | 41 ScopedPlatformHandle PassPlatformHandle() override; |
| 38 | 42 |
| 39 private: | 43 private: |
| 40 explicit SimplePlatformSharedBuffer(size_t num_bytes); | 44 explicit SimplePlatformSharedBuffer(size_t num_bytes); |
| 41 ~SimplePlatformSharedBuffer() override; | 45 ~SimplePlatformSharedBuffer() override; |
| 42 | 46 |
| 43 // Implemented in simple_platform_shared_buffer_{posix,win}.cc: | |
| 44 | |
| 45 // This is called by |Create()| before this object is given to anyone. | 47 // This is called by |Create()| before this object is given to anyone. |
| 46 bool Init(); | 48 bool Init(); |
| 47 | 49 |
| 48 // This is like |Init()|, but for |CreateFromPlatformHandle()|. (Note: It | 50 // This is like |Init()|, but for |CreateFromPlatformHandle()|. (Note: It |
| 49 // should verify that |platform_handle| is an appropriate handle for the | 51 // should verify that |platform_handle| is an appropriate handle for the |
| 50 // claimed |num_bytes_|.) | 52 // claimed |num_bytes_|.) |
| 51 bool InitFromPlatformHandle(ScopedPlatformHandle platform_handle); | 53 bool InitFromPlatformHandle(ScopedPlatformHandle platform_handle); |
| 52 | 54 |
| 53 // The platform-dependent part of |Map()|; doesn't check arguments. | |
| 54 scoped_ptr<PlatformSharedBufferMapping> MapImpl(size_t offset, size_t length); | |
| 55 | |
| 56 const size_t num_bytes_; | 55 const size_t num_bytes_; |
| 57 | 56 |
| 58 // This is set in |Init()|/|InitFromPlatformHandle()| and never modified | 57 base::Lock lock_; |
| 59 // (except by |PassPlatformHandle()|; see the comments above its declaration), | 58 scoped_ptr<base::SharedMemory> shared_memory_; |
| 60 // hence does not need to be protected by a lock. | |
| 61 ScopedPlatformHandle handle_; | |
| 62 | 59 |
| 63 MOJO_DISALLOW_COPY_AND_ASSIGN(SimplePlatformSharedBuffer); | 60 MOJO_DISALLOW_COPY_AND_ASSIGN(SimplePlatformSharedBuffer); |
| 64 }; | 61 }; |
| 65 | 62 |
| 66 // An implementation of |PlatformSharedBufferMapping|, produced by | 63 // An implementation of |PlatformSharedBufferMapping|, produced by |
| 67 // |SimplePlatformSharedBuffer|. | 64 // |SimplePlatformSharedBuffer|. |
| 68 class MOJO_SYSTEM_IMPL_EXPORT SimplePlatformSharedBufferMapping | 65 class MOJO_SYSTEM_IMPL_EXPORT SimplePlatformSharedBufferMapping |
| 69 : public PlatformSharedBufferMapping { | 66 : public PlatformSharedBufferMapping { |
| 70 public: | 67 public: |
| 71 ~SimplePlatformSharedBufferMapping() override; | 68 ~SimplePlatformSharedBufferMapping() override; |
| 72 | 69 |
| 73 void* GetBase() const override; | 70 void* GetBase() const override; |
| 74 size_t GetLength() const override; | 71 size_t GetLength() const override; |
| 75 | 72 |
| 76 private: | 73 private: |
| 77 friend class SimplePlatformSharedBuffer; | 74 friend class SimplePlatformSharedBuffer; |
| 78 | 75 |
| 79 SimplePlatformSharedBufferMapping(void* base, | 76 SimplePlatformSharedBufferMapping(base::SharedMemoryHandle handle, |
| 80 size_t length, | 77 size_t offset, |
| 81 void* real_base, | 78 size_t length) |
| 82 size_t real_length) | 79 : offset_(offset), |
| 83 : base_(base), | |
| 84 length_(length), | 80 length_(length), |
| 85 real_base_(real_base), | 81 base_(nullptr), |
| 86 real_length_(real_length) {} | 82 shared_memory_(handle, false) {} |
| 83 |
| 84 bool Map(); |
| 87 void Unmap(); | 85 void Unmap(); |
| 88 | 86 |
| 89 void* const base_; | 87 const size_t offset_; |
| 90 const size_t length_; | 88 const size_t length_; |
| 89 void* base_; |
| 91 | 90 |
| 92 void* const real_base_; | 91 // Since mapping life cycles are separate from PlatformSharedBuffer and a |
| 93 const size_t real_length_; | 92 // buffer can be mapped multiple times, we have our own SharedMemory object |
| 93 // created from a duplicate handle. |
| 94 base::SharedMemory shared_memory_; |
| 94 | 95 |
| 95 MOJO_DISALLOW_COPY_AND_ASSIGN(SimplePlatformSharedBufferMapping); | 96 MOJO_DISALLOW_COPY_AND_ASSIGN(SimplePlatformSharedBufferMapping); |
| 96 }; | 97 }; |
| 97 | 98 |
| 98 } // namespace edk | 99 } // namespace edk |
| 99 } // namespace mojo | 100 } // namespace mojo |
| 100 | 101 |
| 101 #endif // MOJO_EDK_EMBEDDER_SIMPLE_PLATFORM_SHARED_BUFFER_H_ | 102 #endif // MOJO_EDK_EMBEDDER_SIMPLE_PLATFORM_SHARED_BUFFER_H_ |
| OLD | NEW |