Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: mojo/system/raw_shared_buffer.h

Issue 219023010: Mojo: RawSharedBuffer::Mapping -> RawSharedBufferMapping. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: win Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/system/dispatcher_unittest.cc ('k') | mojo/system/raw_shared_buffer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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" 13 #include "base/synchronization/lock.h"
14 #include "mojo/embedder/scoped_platform_handle.h" 14 #include "mojo/embedder/scoped_platform_handle.h"
15 #include "mojo/system/system_impl_export.h" 15 #include "mojo/system/system_impl_export.h"
16 16
17 namespace mojo { 17 namespace mojo {
18 namespace system { 18 namespace system {
19 19
20 class RawSharedBufferMapping;
21
20 // |RawSharedBuffer| is a thread-safe, ref-counted wrapper around OS-specific 22 // |RawSharedBuffer| is a thread-safe, ref-counted wrapper around OS-specific
21 // shared memory. It has the following features: 23 // shared memory. It has the following features:
22 // - A |RawSharedBuffer| simply represents a piece of shared memory that *may* 24 // - A |RawSharedBuffer| simply represents a piece of shared memory that *may*
23 // be mapped and *may* be shared to another process. 25 // be mapped and *may* be shared to another process.
24 // - A single |RawSharedBuffer| may be mapped multiple times. The lifetime of 26 // - A single |RawSharedBuffer| may be mapped multiple times. The lifetime of
25 // the mapping (owned by |RawSharedBuffer::Mapping|) is separate from the 27 // the mapping (owned by |RawSharedBufferMapping|) is separate from the
26 // lifetime of the |RawSharedBuffer|. 28 // lifetime of the |RawSharedBuffer|.
27 // - Sizes/offsets (of the shared memory and mappings) are arbitrary, and not 29 // - Sizes/offsets (of the shared memory and mappings) are arbitrary, and not
28 // 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
29 // requested. 31 // requested.
30 // 32 //
31 // It currently does NOT support the following: 33 // It currently does NOT support the following:
32 // - Sharing read-only. (This will probably eventually be supported.) 34 // - Sharing read-only. (This will probably eventually be supported.)
33 // 35 //
34 // TODO(vtl): Rectify this with |base::SharedMemory|. 36 // TODO(vtl): Rectify this with |base::SharedMemory|.
35 class MOJO_SYSTEM_IMPL_EXPORT RawSharedBuffer 37 class MOJO_SYSTEM_IMPL_EXPORT RawSharedBuffer
36 : public base::RefCountedThreadSafe<RawSharedBuffer> { 38 : public base::RefCountedThreadSafe<RawSharedBuffer> {
37 public: 39 public:
38 // A mapping of a |RawSharedBuffer| (compararable to a "file view" in
39 // Windows); see above. Created by |RawSharedBuffer::Map()|. Automatically
40 // unmaps memory on destruction.
41 //
42 // Mappings are NOT thread-safe.
43 class MOJO_SYSTEM_IMPL_EXPORT Mapping {
44 public:
45 ~Mapping() { Unmap(); }
46
47 void* base() const { return base_; }
48 size_t length() const { return length_; }
49
50 private:
51 friend class RawSharedBuffer;
52
53 Mapping(void* base, size_t length, void* real_base, size_t real_length)
54 : base_(base), length_(length),
55 real_base_(real_base), real_length_(real_length) {}
56 void Unmap();
57
58 void* const base_;
59 const size_t length_;
60
61 void* const real_base_;
62 const size_t real_length_;
63
64 DISALLOW_COPY_AND_ASSIGN(Mapping);
65 };
66 40
67 // Creates a shared buffer of size |num_bytes| bytes (initially zero-filled). 41 // Creates a shared buffer of size |num_bytes| bytes (initially zero-filled).
68 // |num_bytes| must be nonzero. Returns null on failure. 42 // |num_bytes| must be nonzero. Returns null on failure.
69 static RawSharedBuffer* Create(size_t num_bytes); 43 static RawSharedBuffer* Create(size_t num_bytes);
70 44
71 // Maps (some) of the shared buffer into memory; [|offset|, |offset + length|] 45 // Maps (some) of the shared buffer into memory; [|offset|, |offset + length|]
72 // must be contained in [0, |num_bytes|], and |length| must be at least 1. 46 // must be contained in [0, |num_bytes|], and |length| must be at least 1.
73 // Returns null on failure. 47 // Returns null on failure.
74 scoped_ptr<Mapping> Map(size_t offset, size_t length); 48 scoped_ptr<RawSharedBufferMapping> Map(size_t offset, size_t length);
75 49
76 // Checks if |offset| and |length| are valid arguments. 50 // Checks if |offset| and |length| are valid arguments.
77 bool IsValidMap(size_t offset, size_t length); 51 bool IsValidMap(size_t offset, size_t length);
78 52
79 // Like |Map()|, but doesn't check its arguments (which should have been 53 // Like |Map()|, but doesn't check its arguments (which should have been
80 // preflighted using |IsValidMap()|). 54 // preflighted using |IsValidMap()|).
81 scoped_ptr<Mapping> MapNoCheck(size_t offset, size_t length); 55 scoped_ptr<RawSharedBufferMapping> MapNoCheck(size_t offset, size_t length);
82 56
83 size_t num_bytes() const { return num_bytes_; } 57 size_t num_bytes() const { return num_bytes_; }
84 58
85 private: 59 private:
86 friend class base::RefCountedThreadSafe<RawSharedBuffer>; 60 friend class base::RefCountedThreadSafe<RawSharedBuffer>;
87 61
88 explicit RawSharedBuffer(size_t num_bytes); 62 explicit RawSharedBuffer(size_t num_bytes);
89 ~RawSharedBuffer(); 63 ~RawSharedBuffer();
90 64
91 // This is called by |Create()| before this object is given to anyone (hence 65 // This is called by |Create()| before this object is given to anyone (hence
92 // it doesn't need to take |lock_|). 66 // it doesn't need to take |lock_|).
93 bool InitNoLock(); 67 bool InitNoLock();
94 68
95 // The platform-dependent part of |Map()|; doesn't check arguments. Called 69 // The platform-dependent part of |Map()|; doesn't check arguments. Called
96 // under |lock_|. 70 // under |lock_|.
97 scoped_ptr<Mapping> MapImplNoLock(size_t offset, size_t length); 71 scoped_ptr<RawSharedBufferMapping> MapImplNoLock(size_t offset,
72 size_t length);
98 73
99 const size_t num_bytes_; 74 const size_t num_bytes_;
100 75
101 base::Lock lock_; // Protects |handle_|. 76 base::Lock lock_; // Protects |handle_|.
102 embedder::ScopedPlatformHandle handle_; 77 embedder::ScopedPlatformHandle handle_;
103 78
104 DISALLOW_COPY_AND_ASSIGN(RawSharedBuffer); 79 DISALLOW_COPY_AND_ASSIGN(RawSharedBuffer);
105 }; 80 };
106 81
82 // A mapping of a |RawSharedBuffer| (compararable to a "file view" in Windows);
83 // see above. Created by |RawSharedBuffer::Map()|. Automatically unmaps memory
84 // on destruction.
85 //
86 // Mappings are NOT thread-safe.
87 //
88 // Note: This is an entirely separate class (instead of
89 // |RawSharedBuffer::Mapping|) so that it can be forward-declared.
90 class MOJO_SYSTEM_IMPL_EXPORT RawSharedBufferMapping {
91 public:
92 ~RawSharedBufferMapping() { Unmap(); }
93
94 void* base() const { return base_; }
95 size_t length() const { return length_; }
96
97 private:
98 friend class RawSharedBuffer;
99
100 RawSharedBufferMapping(void* base,
101 size_t length,
102 void* real_base,
103 size_t real_length)
104 : base_(base), length_(length),
105 real_base_(real_base), real_length_(real_length) {}
106 void Unmap();
107
108 void* const base_;
109 const size_t length_;
110
111 void* const real_base_;
112 const size_t real_length_;
113
114 DISALLOW_COPY_AND_ASSIGN(RawSharedBufferMapping);
115 };
116
107 } // namespace system 117 } // namespace system
108 } // namespace mojo 118 } // namespace mojo
109 119
110 #endif // MOJO_SYSTEM_RAW_SHARED_BUFFER_H_ 120 #endif // MOJO_SYSTEM_RAW_SHARED_BUFFER_H_
OLDNEW
« no previous file with comments | « mojo/system/dispatcher_unittest.cc ('k') | mojo/system/raw_shared_buffer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698