Home | History | Annotate | Download | only in system
      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 {
     29   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 : public Handle {
     46  public:
     47   enum class AccessMode {
     48     READ_WRITE,
     49     READ_ONLY,
     50   };
     51 
     52   SharedBufferHandle() {}
     53   explicit SharedBufferHandle(MojoHandle value) : Handle(value) {}
     54 
     55   // Copying and assignment allowed.
     56 
     57   // Creates a new SharedBufferHandle. Returns an invalid handle on failure.
     58   static ScopedSharedBufferHandle Create(uint64_t num_bytes);
     59 
     60   // Clones this shared buffer handle. If |access_mode| is READ_ONLY or this is
     61   // a read-only handle, the new handle will be read-only. On failure, this will
     62   // return an empty result.
     63   ScopedSharedBufferHandle Clone(AccessMode access_mode) const;
     64 
     65   // Maps |size| bytes of this shared buffer. On failure, this will return a
     66   // null mapping.
     67   ScopedSharedBufferMapping Map(uint64_t size) const;
     68 
     69   // Maps |size| bytes of this shared buffer, starting |offset| bytes into the
     70   // buffer. On failure, this will return a null mapping.
     71   ScopedSharedBufferMapping MapAtOffset(uint64_t size, uint64_t offset) const;
     72 
     73   // Get the size of this shared buffer.
     74   uint64_t GetSize() const;
     75 };
     76 
     77 static_assert(sizeof(SharedBufferHandle) == sizeof(Handle),
     78               "Bad size for C++ SharedBufferHandle");
     79 static_assert(sizeof(ScopedSharedBufferHandle) == sizeof(SharedBufferHandle),
     80               "Bad size for C++ ScopedSharedBufferHandle");
     81 
     82 }  // namespace mojo
     83 
     84 #endif  // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_
     85