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" |
(...skipping 19 matching lines...) Expand all Loading... |
30 // restricted by page size. However, more memory may actually be mapped than | 30 // restricted by page size. However, more memory may actually be mapped than |
31 // requested. | 31 // requested. |
32 // | 32 // |
33 // It currently does NOT support the following: | 33 // It currently does NOT support the following: |
34 // - Sharing read-only. (This will probably eventually be supported.) | 34 // - Sharing read-only. (This will probably eventually be supported.) |
35 // | 35 // |
36 // TODO(vtl): Rectify this with |base::SharedMemory|. | 36 // TODO(vtl): Rectify this with |base::SharedMemory|. |
37 class MOJO_SYSTEM_IMPL_EXPORT RawSharedBuffer | 37 class MOJO_SYSTEM_IMPL_EXPORT RawSharedBuffer |
38 : public base::RefCountedThreadSafe<RawSharedBuffer> { | 38 : public base::RefCountedThreadSafe<RawSharedBuffer> { |
39 public: | 39 public: |
40 | |
41 // Creates a shared buffer of size |num_bytes| bytes (initially zero-filled). | 40 // Creates a shared buffer of size |num_bytes| bytes (initially zero-filled). |
42 // |num_bytes| must be nonzero. Returns null on failure. | 41 // |num_bytes| must be nonzero. Returns null on failure. |
43 static RawSharedBuffer* Create(size_t num_bytes); | 42 static RawSharedBuffer* Create(size_t num_bytes); |
44 | 43 |
| 44 static RawSharedBuffer* CreateFromPlatformHandle( |
| 45 size_t num_bytes, |
| 46 embedder::ScopedPlatformHandle platform_handle); |
| 47 |
45 // Maps (some) of the shared buffer into memory; [|offset|, |offset + length|] | 48 // 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. | 49 // must be contained in [0, |num_bytes|], and |length| must be at least 1. |
47 // Returns null on failure. | 50 // Returns null on failure. |
48 scoped_ptr<RawSharedBufferMapping> Map(size_t offset, size_t length); | 51 scoped_ptr<RawSharedBufferMapping> Map(size_t offset, size_t length); |
49 | 52 |
50 // Checks if |offset| and |length| are valid arguments. | 53 // Checks if |offset| and |length| are valid arguments. |
51 bool IsValidMap(size_t offset, size_t length); | 54 bool IsValidMap(size_t offset, size_t length); |
52 | 55 |
53 // Like |Map()|, but doesn't check its arguments (which should have been | 56 // Like |Map()|, but doesn't check its arguments (which should have been |
54 // preflighted using |IsValidMap()|). | 57 // preflighted using |IsValidMap()|). |
55 scoped_ptr<RawSharedBufferMapping> MapNoCheck(size_t offset, size_t length); | 58 scoped_ptr<RawSharedBufferMapping> MapNoCheck(size_t offset, size_t length); |
56 | 59 |
| 60 // Duplicates the underlying platform handle and passes it to the caller. |
| 61 embedder::ScopedPlatformHandle DuplicatePlatformHandle(); |
| 62 |
| 63 // Passes the underlying platform handle to the caller. This should only be |
| 64 // called if there's a unique reference to this object (owned by the caller). |
| 65 // After calling this, this object should no longer be used, but should only |
| 66 // be disposed of. |
| 67 embedder::ScopedPlatformHandle PassPlatformHandle(); |
| 68 |
57 size_t num_bytes() const { return num_bytes_; } | 69 size_t num_bytes() const { return num_bytes_; } |
58 | 70 |
59 private: | 71 private: |
60 friend class base::RefCountedThreadSafe<RawSharedBuffer>; | 72 friend class base::RefCountedThreadSafe<RawSharedBuffer>; |
61 | 73 |
62 explicit RawSharedBuffer(size_t num_bytes); | 74 explicit RawSharedBuffer(size_t num_bytes); |
63 ~RawSharedBuffer(); | 75 ~RawSharedBuffer(); |
64 | 76 |
| 77 // Implemented in raw_shared_buffer_{posix,win}.cc: |
| 78 |
65 // This is called by |Create()| before this object is given to anyone (hence | 79 // This is called by |Create()| before this object is given to anyone (hence |
66 // it doesn't need to take |lock_|). | 80 // it doesn't need to take |lock_|). |
67 bool InitNoLock(); | 81 bool InitNoLock(); |
68 | 82 |
| 83 // This is like |InitNoLoock()|, but for |CreateFromPlatformHandle()|. (Note: |
| 84 // It should verify that |platform_handle| is an appropriate handle for the |
| 85 // claimed |num_bytes_|.) |
| 86 bool InitFromPlatformHandleNoLock( |
| 87 embedder::ScopedPlatformHandle platform_handle); |
| 88 |
69 // The platform-dependent part of |Map()|; doesn't check arguments. Called | 89 // The platform-dependent part of |Map()|; doesn't check arguments. Called |
70 // under |lock_|. | 90 // under |lock_|. |
71 scoped_ptr<RawSharedBufferMapping> MapImplNoLock(size_t offset, | 91 scoped_ptr<RawSharedBufferMapping> MapImplNoLock(size_t offset, |
72 size_t length); | 92 size_t length); |
73 | 93 |
74 const size_t num_bytes_; | 94 const size_t num_bytes_; |
75 | 95 |
76 base::Lock lock_; // Protects |handle_|. | 96 base::Lock lock_; // Protects |handle_|. |
77 embedder::ScopedPlatformHandle handle_; | 97 embedder::ScopedPlatformHandle handle_; |
78 | 98 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 void* const real_base_; | 131 void* const real_base_; |
112 const size_t real_length_; | 132 const size_t real_length_; |
113 | 133 |
114 DISALLOW_COPY_AND_ASSIGN(RawSharedBufferMapping); | 134 DISALLOW_COPY_AND_ASSIGN(RawSharedBufferMapping); |
115 }; | 135 }; |
116 | 136 |
117 } // namespace system | 137 } // namespace system |
118 } // namespace mojo | 138 } // namespace mojo |
119 | 139 |
120 #endif // MOJO_SYSTEM_RAW_SHARED_BUFFER_H_ | 140 #endif // MOJO_SYSTEM_RAW_SHARED_BUFFER_H_ |
OLD | NEW |