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_SIMPLE_PLATFORM_SHARED_BUFFER_H_
6 #define MOJO_SYSTEM_SIMPLE_PLATFORM_SHARED_BUFFER_H_
10 #include "base/macros.h"
11 #include "mojo/embedder/platform_shared_buffer.h"
12 #include "mojo/system/system_impl_export.h"
17 // A simple implementation of |PlatformSharedBuffer|.
18 class MOJO_SYSTEM_IMPL_EXPORT SimplePlatformSharedBuffer
19 : public PlatformSharedBuffer {
21 // Creates a shared buffer of size |num_bytes| bytes (initially zero-filled).
22 // |num_bytes| must be nonzero. Returns null on failure.
23 static SimplePlatformSharedBuffer* Create(size_t num_bytes);
25 static SimplePlatformSharedBuffer* CreateFromPlatformHandle(
27 ScopedPlatformHandle platform_handle);
29 // |PlatformSharedBuffer| implementation:
30 virtual size_t GetNumBytes() const OVERRIDE;
31 virtual scoped_ptr<PlatformSharedBufferMapping> Map(size_t offset,
32 size_t length) OVERRIDE;
33 virtual bool IsValidMap(size_t offset, size_t length) OVERRIDE;
34 virtual scoped_ptr<PlatformSharedBufferMapping> MapNoCheck(
36 size_t length) OVERRIDE;
37 virtual ScopedPlatformHandle DuplicatePlatformHandle() OVERRIDE;
38 virtual ScopedPlatformHandle PassPlatformHandle() OVERRIDE;
41 explicit SimplePlatformSharedBuffer(size_t num_bytes);
42 virtual ~SimplePlatformSharedBuffer();
44 // Implemented in simple_platform_shared_buffer_{posix,win}.cc:
46 // This is called by |Create()| before this object is given to anyone.
49 // This is like |Init()|, but for |CreateFromPlatformHandle()|. (Note: It
50 // should verify that |platform_handle| is an appropriate handle for the
51 // claimed |num_bytes_|.)
52 bool InitFromPlatformHandle(ScopedPlatformHandle platform_handle);
54 // The platform-dependent part of |Map()|; doesn't check arguments.
55 scoped_ptr<PlatformSharedBufferMapping> MapImpl(size_t offset, size_t length);
57 const size_t num_bytes_;
59 // This is set in |Init()|/|InitFromPlatformHandle()| and never modified
60 // (except by |PassPlatformHandle()|; see the comments above its declaration),
61 // hence does not need to be protected by a lock.
62 ScopedPlatformHandle handle_;
64 DISALLOW_COPY_AND_ASSIGN(SimplePlatformSharedBuffer);
67 // An implementation of |PlatformSharedBufferMapping|, produced by
68 // |SimplePlatformSharedBuffer|.
69 class MOJO_SYSTEM_IMPL_EXPORT SimplePlatformSharedBufferMapping
70 : public PlatformSharedBufferMapping {
72 virtual ~SimplePlatformSharedBufferMapping();
74 virtual void* GetBase() const OVERRIDE;
75 virtual size_t GetLength() const OVERRIDE;
78 friend class SimplePlatformSharedBuffer;
80 SimplePlatformSharedBufferMapping(void* base,
86 real_base_(real_base),
87 real_length_(real_length) {}
93 void* const real_base_;
94 const size_t real_length_;
96 DISALLOW_COPY_AND_ASSIGN(SimplePlatformSharedBufferMapping);
99 } // namespace embedder
102 #endif // MOJO_SYSTEM_SIMPLE_PLATFORM_SHARED_BUFFER_H_