Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / mojo / embedder / platform_shared_buffer.h
blobf8f422ea746c6530d4a79a5f8497088df1e22383
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_
8 #include <stddef.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"
16 namespace mojo {
17 namespace embedder {
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
30 // requested.
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> {
38 public:
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,
46 size_t length) = 0;
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,
54 size_t length) = 0;
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
63 // be disposed of.
64 virtual ScopedPlatformHandle PassPlatformHandle() = 0;
66 protected:
67 friend class base::RefCountedThreadSafe<PlatformSharedBuffer>;
69 PlatformSharedBuffer() {}
70 virtual ~PlatformSharedBuffer() {}
72 private:
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 {
85 public:
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;
92 protected:
93 PlatformSharedBufferMapping() {}
95 private:
96 DISALLOW_COPY_AND_ASSIGN(PlatformSharedBufferMapping);
99 } // namespace embedder
100 } // namespace mojo
102 #endif // MOJO_EMBEDDER_PLATFORM_SHARED_BUFFER_H_