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 <memory> | 10 #include <memory> |
11 | 11 |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "mojo/edk/embedder/scoped_platform_handle.h" | 13 #include "mojo/edk/embedder/scoped_platform_handle.h" |
14 #include "mojo/edk/system/system_impl_export.h" | |
15 #include "mojo/public/cpp/system/macros.h" | 14 #include "mojo/public/cpp/system/macros.h" |
16 | 15 |
17 namespace mojo { | 16 namespace mojo { |
18 namespace embedder { | 17 namespace embedder { |
19 | 18 |
20 class PlatformSharedBufferMapping; | 19 class PlatformSharedBufferMapping; |
21 | 20 |
22 // |PlatformSharedBuffer| is an interface for a thread-safe, ref-counted wrapper | 21 // |PlatformSharedBuffer| is an interface for a thread-safe, ref-counted wrapper |
23 // around OS-specific shared memory. It has the following features: | 22 // around OS-specific shared memory. It has the following features: |
24 // - A |PlatformSharedBuffer| simply represents a piece of shared memory that | 23 // - A |PlatformSharedBuffer| simply represents a piece of shared memory that |
25 // *may* be mapped and *may* be shared to another process. | 24 // *may* be mapped and *may* be shared to another process. |
26 // - A single |PlatformSharedBuffer| may be mapped multiple times. The | 25 // - A single |PlatformSharedBuffer| may be mapped multiple times. The |
27 // lifetime of the mapping (owned by |PlatformSharedBufferMapping|) is | 26 // lifetime of the mapping (owned by |PlatformSharedBufferMapping|) is |
28 // separate from the lifetime of the |PlatformSharedBuffer|. | 27 // separate from the lifetime of the |PlatformSharedBuffer|. |
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 PlatformSharedBuffer | 36 class PlatformSharedBuffer |
38 : public base::RefCountedThreadSafe<PlatformSharedBuffer> { | 37 : public base::RefCountedThreadSafe<PlatformSharedBuffer> { |
39 public: | 38 public: |
40 // Gets the size of shared buffer (in number of bytes). | 39 // Gets the size of shared buffer (in number of bytes). |
41 virtual size_t GetNumBytes() const = 0; | 40 virtual size_t GetNumBytes() const = 0; |
42 | 41 |
43 // Maps (some) of the shared buffer into memory; [|offset|, |offset + length|] | 42 // Maps (some) of the shared buffer into memory; [|offset|, |offset + length|] |
44 // must be contained in [0, |num_bytes|], and |length| must be at least 1. | 43 // must be contained in [0, |num_bytes|], and |length| must be at least 1. |
45 // Returns null on failure. | 44 // Returns null on failure. |
46 virtual std::unique_ptr<PlatformSharedBufferMapping> Map(size_t offset, | 45 virtual std::unique_ptr<PlatformSharedBufferMapping> Map(size_t offset, |
47 size_t length) = 0; | 46 size_t length) = 0; |
(...skipping 28 matching lines...) Expand all Loading... |
76 }; | 75 }; |
77 | 76 |
78 // An interface for a mapping of a |PlatformSharedBuffer| (compararable to a | 77 // An interface for a mapping of a |PlatformSharedBuffer| (compararable to a |
79 // "file view" in Windows); see above. Created by (implementations of) | 78 // "file view" in Windows); see above. Created by (implementations of) |
80 // |PlatformSharedBuffer::Map()|. Automatically unmaps memory on destruction. | 79 // |PlatformSharedBuffer::Map()|. Automatically unmaps memory on destruction. |
81 // | 80 // |
82 // Mappings are NOT thread-safe. | 81 // Mappings are NOT thread-safe. |
83 // | 82 // |
84 // Note: This is an entirely separate class (instead of | 83 // Note: This is an entirely separate class (instead of |
85 // |PlatformSharedBuffer::Mapping|) so that it can be forward-declared. | 84 // |PlatformSharedBuffer::Mapping|) so that it can be forward-declared. |
86 class MOJO_SYSTEM_IMPL_EXPORT PlatformSharedBufferMapping { | 85 class PlatformSharedBufferMapping { |
87 public: | 86 public: |
88 // IMPORTANT: Implementations must implement a destructor that unmaps memory. | 87 // IMPORTANT: Implementations must implement a destructor that unmaps memory. |
89 virtual ~PlatformSharedBufferMapping() {} | 88 virtual ~PlatformSharedBufferMapping() {} |
90 | 89 |
91 virtual void* GetBase() const = 0; | 90 virtual void* GetBase() const = 0; |
92 virtual size_t GetLength() const = 0; | 91 virtual size_t GetLength() const = 0; |
93 | 92 |
94 protected: | 93 protected: |
95 PlatformSharedBufferMapping() {} | 94 PlatformSharedBufferMapping() {} |
96 | 95 |
97 private: | 96 private: |
98 MOJO_DISALLOW_COPY_AND_ASSIGN(PlatformSharedBufferMapping); | 97 MOJO_DISALLOW_COPY_AND_ASSIGN(PlatformSharedBufferMapping); |
99 }; | 98 }; |
100 | 99 |
101 } // namespace embedder | 100 } // namespace embedder |
102 } // namespace mojo | 101 } // namespace mojo |
103 | 102 |
104 #endif // MOJO_EDK_EMBEDDER_PLATFORM_SHARED_BUFFER_H_ | 103 #endif // MOJO_EDK_EMBEDDER_PLATFORM_SHARED_BUFFER_H_ |
OLD | NEW |