| 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_SYSTEM_RAW_SHARED_BUFFER_H_ | 5 #ifndef MOJO_SYSTEM_RAW_SHARED_BUFFER_H_ |
| 6 #define MOJO_SYSTEM_RAW_SHARED_BUFFER_H_ | 6 #define MOJO_SYSTEM_RAW_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" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/synchronization/lock.h" | |
| 14 #include "mojo/embedder/scoped_platform_handle.h" | 13 #include "mojo/embedder/scoped_platform_handle.h" |
| 15 #include "mojo/system/system_impl_export.h" | 14 #include "mojo/system/system_impl_export.h" |
| 16 | 15 |
| 17 namespace mojo { | 16 namespace mojo { |
| 18 namespace system { | 17 namespace system { |
| 19 | 18 |
| 20 class RawSharedBufferMapping; | 19 class RawSharedBufferMapping; |
| 21 | 20 |
| 22 // |RawSharedBuffer| is a thread-safe, ref-counted wrapper around OS-specific | 21 // |RawSharedBuffer| is a thread-safe, ref-counted wrapper around OS-specific |
| 23 // shared memory. It has the following features: | 22 // shared memory. It has the following features: |
| 24 // - A |RawSharedBuffer| simply represents a piece of shared memory that *may* | 23 // - A |RawSharedBuffer| simply represents a piece of shared memory that *may* |
| 25 // be mapped and *may* be shared to another process. | 24 // be mapped and *may* be shared to another process. |
| 26 // - A single |RawSharedBuffer| may be mapped multiple times. The lifetime of | 25 // - A single |RawSharedBuffer| may be mapped multiple times. The lifetime of |
| 27 // the mapping (owned by |RawSharedBufferMapping|) is separate from the | 26 // the mapping (owned by |RawSharedBufferMapping|) is separate from the |
| 28 // lifetime of the |RawSharedBuffer|. | 27 // lifetime of the |RawSharedBuffer|. |
| 29 // - Sizes/offsets (of the shared memory and mappings) are arbitrary, and not | 28 // - Sizes/offsets (of the shared memory and mappings) are arbitrary, and not |
| 30 // restricted by page size. However, more memory may actually be mapped than | 29 // restricted by page size. However, more memory may actually be mapped than |
| 31 // requested. | 30 // requested. |
| 32 // | 31 // |
| 33 // It currently does NOT support the following: | 32 // It currently does NOT support the following: |
| 34 // - Sharing read-only. (This will probably eventually be supported.) | 33 // - Sharing read-only. (This will probably eventually be supported.) |
| 35 // | 34 // |
| 36 // TODO(vtl): Rectify this with |base::SharedMemory|. | 35 // TODO(vtl): Rectify this with |base::SharedMemory|. |
| 37 class MOJO_SYSTEM_IMPL_EXPORT RawSharedBuffer | 36 class MOJO_SYSTEM_IMPL_EXPORT RawSharedBuffer |
| 38 : public base::RefCountedThreadSafe<RawSharedBuffer> { | 37 : public base::RefCountedThreadSafe<RawSharedBuffer> { |
| 39 public: | 38 public: |
| 40 | |
| 41 // Creates a shared buffer of size |num_bytes| bytes (initially zero-filled). | 39 // Creates a shared buffer of size |num_bytes| bytes (initially zero-filled). |
| 42 // |num_bytes| must be nonzero. Returns null on failure. | 40 // |num_bytes| must be nonzero. Returns null on failure. |
| 43 static RawSharedBuffer* Create(size_t num_bytes); | 41 static RawSharedBuffer* Create(size_t num_bytes); |
| 44 | 42 |
| 43 static RawSharedBuffer* CreateFromPlatformHandle( |
| 44 size_t num_bytes, |
| 45 embedder::ScopedPlatformHandle platform_handle); |
| 46 |
| 45 // Maps (some) of the shared buffer into memory; [|offset|, |offset + length|] | 47 // Maps (some) of the shared buffer into memory; [|offset|, |offset + length|] |
| 46 // must be contained in [0, |num_bytes|], and |length| must be at least 1. | 48 // must be contained in [0, |num_bytes|], and |length| must be at least 1. |
| 47 // Returns null on failure. | 49 // Returns null on failure. |
| 48 scoped_ptr<RawSharedBufferMapping> Map(size_t offset, size_t length); | 50 scoped_ptr<RawSharedBufferMapping> Map(size_t offset, size_t length); |
| 49 | 51 |
| 50 // Checks if |offset| and |length| are valid arguments. | 52 // Checks if |offset| and |length| are valid arguments. |
| 51 bool IsValidMap(size_t offset, size_t length); | 53 bool IsValidMap(size_t offset, size_t length); |
| 52 | 54 |
| 53 // Like |Map()|, but doesn't check its arguments (which should have been | 55 // Like |Map()|, but doesn't check its arguments (which should have been |
| 54 // preflighted using |IsValidMap()|). | 56 // preflighted using |IsValidMap()|). |
| 55 scoped_ptr<RawSharedBufferMapping> MapNoCheck(size_t offset, size_t length); | 57 scoped_ptr<RawSharedBufferMapping> MapNoCheck(size_t offset, size_t length); |
| 56 | 58 |
| 59 // Duplicates the underlying platform handle and passes it to the caller. |
| 60 embedder::ScopedPlatformHandle DuplicatePlatformHandle(); |
| 61 |
| 62 // Passes the underlying platform handle to the caller. This should only be |
| 63 // called if there's a unique reference to this object (owned by the caller). |
| 64 // After calling this, this object should no longer be used, but should only |
| 65 // be disposed of. |
| 66 embedder::ScopedPlatformHandle PassPlatformHandle(); |
| 67 |
| 57 size_t num_bytes() const { return num_bytes_; } | 68 size_t num_bytes() const { return num_bytes_; } |
| 58 | 69 |
| 59 private: | 70 private: |
| 60 friend class base::RefCountedThreadSafe<RawSharedBuffer>; | 71 friend class base::RefCountedThreadSafe<RawSharedBuffer>; |
| 61 | 72 |
| 62 explicit RawSharedBuffer(size_t num_bytes); | 73 explicit RawSharedBuffer(size_t num_bytes); |
| 63 ~RawSharedBuffer(); | 74 ~RawSharedBuffer(); |
| 64 | 75 |
| 65 // This is called by |Create()| before this object is given to anyone (hence | 76 // Implemented in raw_shared_buffer_{posix,win}.cc: |
| 66 // it doesn't need to take |lock_|). | |
| 67 bool InitNoLock(); | |
| 68 | 77 |
| 69 // The platform-dependent part of |Map()|; doesn't check arguments. Called | 78 // This is called by |Create()| before this object is given to anyone. |
| 70 // under |lock_|. | 79 bool Init(); |
| 71 scoped_ptr<RawSharedBufferMapping> MapImplNoLock(size_t offset, | 80 |
| 72 size_t length); | 81 // This is like |Init()|, but for |CreateFromPlatformHandle()|. (Note: It |
| 82 // should verify that |platform_handle| is an appropriate handle for the |
| 83 // claimed |num_bytes_|.) |
| 84 bool InitFromPlatformHandle(embedder::ScopedPlatformHandle platform_handle); |
| 85 |
| 86 // The platform-dependent part of |Map()|; doesn't check arguments. |
| 87 scoped_ptr<RawSharedBufferMapping> MapImpl(size_t offset, size_t length); |
| 73 | 88 |
| 74 const size_t num_bytes_; | 89 const size_t num_bytes_; |
| 75 | 90 |
| 76 base::Lock lock_; // Protects |handle_|. | 91 // This is set in |Init()|/|InitFromPlatformHandle()| and never modified |
| 92 // (except by |PassPlatformHandle()|; see the comments above its declaration), |
| 93 // hence does not need to be protected by a lock. |
| 77 embedder::ScopedPlatformHandle handle_; | 94 embedder::ScopedPlatformHandle handle_; |
| 78 | 95 |
| 79 DISALLOW_COPY_AND_ASSIGN(RawSharedBuffer); | 96 DISALLOW_COPY_AND_ASSIGN(RawSharedBuffer); |
| 80 }; | 97 }; |
| 81 | 98 |
| 82 // A mapping of a |RawSharedBuffer| (compararable to a "file view" in Windows); | 99 // A mapping of a |RawSharedBuffer| (compararable to a "file view" in Windows); |
| 83 // see above. Created by |RawSharedBuffer::Map()|. Automatically unmaps memory | 100 // see above. Created by |RawSharedBuffer::Map()|. Automatically unmaps memory |
| 84 // on destruction. | 101 // on destruction. |
| 85 // | 102 // |
| 86 // Mappings are NOT thread-safe. | 103 // Mappings are NOT thread-safe. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 111 void* const real_base_; | 128 void* const real_base_; |
| 112 const size_t real_length_; | 129 const size_t real_length_; |
| 113 | 130 |
| 114 DISALLOW_COPY_AND_ASSIGN(RawSharedBufferMapping); | 131 DISALLOW_COPY_AND_ASSIGN(RawSharedBufferMapping); |
| 115 }; | 132 }; |
| 116 | 133 |
| 117 } // namespace system | 134 } // namespace system |
| 118 } // namespace mojo | 135 } // namespace mojo |
| 119 | 136 |
| 120 #endif // MOJO_SYSTEM_RAW_SHARED_BUFFER_H_ | 137 #endif // MOJO_SYSTEM_RAW_SHARED_BUFFER_H_ |
| OLD | NEW |