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
3 // found in the LICENSE file.
5 #ifndef MOJO_SYSTEM_RAW_SHARED_BUFFER_H_
6 #define MOJO_SYSTEM_RAW_SHARED_BUFFER_H_
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "mojo/embedder/scoped_platform_handle.h"
14 #include "mojo/system/system_impl_export.h"
19 class RawSharedBufferMapping;
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
32 // It currently does NOT support the following:
33 // - Sharing read-only. (This will probably eventually be supported.)
35 // TODO(vtl): Rectify this with |base::SharedMemory|.
36 class MOJO_SYSTEM_IMPL_EXPORT RawSharedBuffer
37 : public base::RefCountedThreadSafe<RawSharedBuffer> {
39 // Creates a shared buffer of size |num_bytes| bytes (initially zero-filled).
40 // |num_bytes| must be nonzero. Returns null on failure.
41 static RawSharedBuffer* Create(size_t num_bytes);
43 static RawSharedBuffer* CreateFromPlatformHandle(
45 embedder::ScopedPlatformHandle platform_handle);
47 // Maps (some) of the shared buffer into memory; [|offset|, |offset + length|]
48 // must be contained in [0, |num_bytes|], and |length| must be at least 1.
49 // Returns null on failure.
50 scoped_ptr<RawSharedBufferMapping> Map(size_t offset, size_t length);
52 // Checks if |offset| and |length| are valid arguments.
53 bool IsValidMap(size_t offset, size_t length);
55 // Like |Map()|, but doesn't check its arguments (which should have been
56 // preflighted using |IsValidMap()|).
57 scoped_ptr<RawSharedBufferMapping> MapNoCheck(size_t offset, size_t length);
59 // Duplicates the underlying platform handle and passes it to the caller.
60 embedder::ScopedPlatformHandle DuplicatePlatformHandle();
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
66 embedder::ScopedPlatformHandle PassPlatformHandle();
68 size_t num_bytes() const { return num_bytes_; }
71 friend class base::RefCountedThreadSafe<RawSharedBuffer>;
73 explicit RawSharedBuffer(size_t num_bytes);
76 // Implemented in raw_shared_buffer_{posix,win}.cc:
78 // This is called by |Create()| before this object is given to anyone.
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);
86 // The platform-dependent part of |Map()|; doesn't check arguments.
87 scoped_ptr<RawSharedBufferMapping> MapImpl(size_t offset, size_t length);
89 const size_t num_bytes_;
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.
94 embedder::ScopedPlatformHandle handle_;
96 DISALLOW_COPY_AND_ASSIGN(RawSharedBuffer);
99 // A mapping of a |RawSharedBuffer| (compararable to a "file view" in Windows);
100 // see above. Created by |RawSharedBuffer::Map()|. Automatically unmaps memory
103 // Mappings are NOT thread-safe.
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 {
109 ~RawSharedBufferMapping() { Unmap(); }
111 void* base() const { return base_; }
112 size_t length() const { return length_; }
115 friend class RawSharedBuffer;
117 RawSharedBufferMapping(void* base,
121 : base_(base), length_(length),
122 real_base_(real_base), real_length_(real_length) {}
126 const size_t length_;
128 void* const real_base_;
129 const size_t real_length_;
131 DISALLOW_COPY_AND_ASSIGN(RawSharedBufferMapping);
134 } // namespace system
137 #endif // MOJO_SYSTEM_RAW_SHARED_BUFFER_H_