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