1 // Copyright 2018 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 #ifndef MOJO_CORE_PLATFORM_SHARED_MEMORY_MAPPING_H_
6 #define MOJO_CORE_PLATFORM_SHARED_MEMORY_MAPPING_H_
7 
8 #include <stddef.h>
9 
10 #include <memory>
11 
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "base/memory/platform_shared_memory_region.h"
15 #include "base/memory/shared_memory_mapping.h"
16 #include "mojo/core/system_impl_export.h"
17 
18 namespace mojo {
19 namespace core {
20 
21 // A mapping of a |base::subtle::PlatformSharedMemoryRegion|, created
22 // exclusively by |SharedBufferDispatcher::MapBuffer()|. Automatically maps
23 // upon construction and unmaps upon destruction.
24 //
25 // Mappings are NOT thread-safe.
26 //
27 // This may represent either a |base::ReadOnlySharedMemoryMapping| OR a
28 // |base::WritableSharedMemoryMapping|, and it supports non-page-aligned base
29 // offsets for convenience.
30 class MOJO_SYSTEM_IMPL_EXPORT PlatformSharedMemoryMapping {
31  public:
32   enum class Type {
33     kReadOnly,
34     kWritable,
35   };
36 
37   PlatformSharedMemoryMapping(base::subtle::PlatformSharedMemoryRegion* region,
38                               size_t offset,
39                               size_t length);
40   ~PlatformSharedMemoryMapping();
41 
42   bool IsValid() const;
43 
44   void* GetBase() const;
45   size_t GetLength() const;
46 
47  private:
48   const Type type_;
49   const size_t offset_;
50   const size_t length_;
51   void* base_ = nullptr;
52   std::unique_ptr<base::SharedMemoryMapping> mapping_;
53 
54   DISALLOW_COPY_AND_ASSIGN(PlatformSharedMemoryMapping);
55 };
56 
57 }  // namespace core
58 }  // namespace mojo
59 
60 #endif  // MOJO_CORE_PLATFORM_SHARED_MEMORY_MAPPING_H_
61