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