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_EMBEDDER_PLATFORM_SHARED_BUFFER_H_
6 #define MOJO_EMBEDDER_PLATFORM_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 PlatformSharedBufferMapping
;
21 // |PlatformSharedBuffer| is an interface for a thread-safe, ref-counted wrapper
22 // around OS-specific shared memory. It has the following features:
23 // - A |PlatformSharedBuffer| simply represents a piece of shared memory that
24 // *may* be mapped and *may* be shared to another process.
25 // - A single |PlatformSharedBuffer| may be mapped multiple times. The
26 // lifetime of the mapping (owned by |PlatformSharedBufferMapping|) is
27 // separate from the lifetime of the |PlatformSharedBuffer|.
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 PlatformSharedBuffer
37 : public base::RefCountedThreadSafe
<PlatformSharedBuffer
> {
39 // Gets the size of shared buffer (in number of bytes).
40 virtual size_t GetNumBytes() const = 0;
42 // Maps (some) of the shared buffer into memory; [|offset|, |offset + length|]
43 // must be contained in [0, |num_bytes|], and |length| must be at least 1.
44 // Returns null on failure.
45 virtual scoped_ptr
<PlatformSharedBufferMapping
> Map(size_t offset
,
48 // Checks if |offset| and |length| are valid arguments.
49 virtual bool IsValidMap(size_t offset
, size_t length
) = 0;
51 // Like |Map()|, but doesn't check its arguments (which should have been
52 // preflighted using |IsValidMap()|).
53 virtual scoped_ptr
<PlatformSharedBufferMapping
> MapNoCheck(size_t offset
,
56 // Duplicates the underlying platform handle and passes it to the caller.
57 // TODO(vtl): On POSIX, we'll need two FDs to support sharing read-only.
58 virtual ScopedPlatformHandle
DuplicatePlatformHandle() = 0;
60 // Passes the underlying platform handle to the caller. This should only be
61 // called if there's a unique reference to this object (owned by the caller).
62 // After calling this, this object should no longer be used, but should only
64 virtual ScopedPlatformHandle
PassPlatformHandle() = 0;
67 friend class base::RefCountedThreadSafe
<PlatformSharedBuffer
>;
69 PlatformSharedBuffer() {}
70 virtual ~PlatformSharedBuffer() {}
73 DISALLOW_COPY_AND_ASSIGN(PlatformSharedBuffer
);
76 // An interface for a mapping of a |PlatformSharedBuffer| (compararable to a
77 // "file view" in Windows); see above. Created by (implementations of)
78 // |PlatformSharedBuffer::Map()|. Automatically unmaps memory on destruction.
80 // Mappings are NOT thread-safe.
82 // Note: This is an entirely separate class (instead of
83 // |PlatformSharedBuffer::Mapping|) so that it can be forward-declared.
84 class MOJO_SYSTEM_IMPL_EXPORT PlatformSharedBufferMapping
{
86 // IMPORTANT: Implementations must implement a destructor that unmaps memory.
87 virtual ~PlatformSharedBufferMapping() {}
89 virtual void* GetBase() const = 0;
90 virtual size_t GetLength() const = 0;
93 PlatformSharedBufferMapping() {}
96 DISALLOW_COPY_AND_ASSIGN(PlatformSharedBufferMapping
);
99 } // namespace embedder
102 #endif // MOJO_EMBEDDER_PLATFORM_SHARED_BUFFER_H_