platform_shared_memory_mapping.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #ifndef MOJO_CORE_PLATFORM_SHARED_MEMORY_MAPPING_H_
  5. #define MOJO_CORE_PLATFORM_SHARED_MEMORY_MAPPING_H_
  6. #include <stddef.h>
  7. #include "base/memory/platform_shared_memory_region.h"
  8. #include "base/memory/shared_memory_mapping.h"
  9. #include "mojo/core/system_impl_export.h"
  10. #include "third_party/abseil-cpp/absl/types/variant.h"
  11. namespace mojo {
  12. namespace core {
  13. // A mapping of a |base::subtle::PlatformSharedMemoryRegion|, created
  14. // exclusively by |SharedBufferDispatcher::MapBuffer()|. Automatically maps
  15. // upon construction and unmaps upon destruction.
  16. //
  17. // Mappings are NOT thread-safe.
  18. //
  19. // This may represent either a |base::ReadOnlySharedMemoryMapping| OR a
  20. // |base::WritableSharedMemoryMapping|, and it supports non-page-aligned base
  21. // offsets for convenience.
  22. class MOJO_SYSTEM_IMPL_EXPORT PlatformSharedMemoryMapping {
  23. public:
  24. PlatformSharedMemoryMapping(base::subtle::PlatformSharedMemoryRegion* region,
  25. size_t offset,
  26. size_t length);
  27. PlatformSharedMemoryMapping(const PlatformSharedMemoryMapping&) = delete;
  28. PlatformSharedMemoryMapping& operator=(const PlatformSharedMemoryMapping&) =
  29. delete;
  30. ~PlatformSharedMemoryMapping();
  31. bool IsValid() const;
  32. void* GetBase() const;
  33. size_t GetLength() const;
  34. private:
  35. absl::variant<absl::monostate,
  36. base::ReadOnlySharedMemoryMapping,
  37. base::WritableSharedMemoryMapping>
  38. mapping_;
  39. };
  40. } // namespace core
  41. } // namespace mojo
  42. #endif // MOJO_CORE_PLATFORM_SHARED_MEMORY_MAPPING_H_