• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 // This file provides a C++ wrapping around the Mojo C API for shared buffers,
6 // replacing the prefix of "Mojo" with a "mojo" namespace, and using more
7 // strongly-typed representations of |MojoHandle|s.
8 //
9 // Please see "mojo/public/c/system/buffer.h" for complete documentation of the
10 // API.
11 
12 #ifndef MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_
13 #define MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_
14 
15 #include <stdint.h>
16 
17 #include <memory>
18 
19 #include "base/compiler_specific.h"
20 #include "base/logging.h"
21 #include "mojo/public/c/system/buffer.h"
22 #include "mojo/public/cpp/system/handle.h"
23 #include "mojo/public/cpp/system/system_export.h"
24 
25 namespace mojo {
26 namespace internal {
27 
28 struct Unmapper {
operatorUnmapper29   void operator()(void* buffer) {
30     MojoResult result = MojoUnmapBuffer(buffer);
31     DCHECK_EQ(MOJO_RESULT_OK, result);
32   }
33 };
34 
35 }  // namespace internal
36 
37 using ScopedSharedBufferMapping = std::unique_ptr<void, internal::Unmapper>;
38 
39 class SharedBufferHandle;
40 
41 typedef ScopedHandleBase<SharedBufferHandle> ScopedSharedBufferHandle;
42 
43 // A strongly-typed representation of a |MojoHandle| referring to a shared
44 // buffer.
45 class MOJO_CPP_SYSTEM_EXPORT SharedBufferHandle
NON_EXPORTED_BASE(public Handle)46     : NON_EXPORTED_BASE(public Handle) {
47  public:
48   enum class AccessMode {
49     READ_WRITE,
50     READ_ONLY,
51   };
52 
53   SharedBufferHandle() {}
54   explicit SharedBufferHandle(MojoHandle value) : Handle(value) {}
55 
56   // Copying and assignment allowed.
57 
58   // Creates a new SharedBufferHandle. Returns an invalid handle on failure.
59   static ScopedSharedBufferHandle Create(uint64_t num_bytes);
60 
61   // Clones this shared buffer handle. If |access_mode| is READ_ONLY or this is
62   // a read-only handle, the new handle will be read-only. On failure, this will
63   // return an empty result.
64   ScopedSharedBufferHandle Clone(AccessMode = AccessMode::READ_WRITE) const;
65 
66   // Maps |size| bytes of this shared buffer. On failure, this will return a
67   // null mapping.
68   ScopedSharedBufferMapping Map(uint64_t size) const;
69 
70   // Maps |size| bytes of this shared buffer, starting |offset| bytes into the
71   // buffer. On failure, this will return a null mapping.
72   ScopedSharedBufferMapping MapAtOffset(uint64_t size, uint64_t offset) const;
73 };
74 
75 static_assert(sizeof(SharedBufferHandle) == sizeof(Handle),
76               "Bad size for C++ SharedBufferHandle");
77 static_assert(sizeof(ScopedSharedBufferHandle) == sizeof(SharedBufferHandle),
78               "Bad size for C++ ScopedSharedBufferHandle");
79 
80 }  // namespace mojo
81 
82 #endif  // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_
83