MemMapSnapshot.h (1503B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef dom_ipc_MemMapSnapshot_h 8 #define dom_ipc_MemMapSnapshot_h 9 10 #include "ErrorList.h" 11 #include "mozilla/Attributes.h" 12 #include "mozilla/RangedPtr.h" 13 #include "mozilla/Result.h" 14 #include "mozilla/ipc/SharedMemoryMapping.h" 15 16 namespace mozilla::ipc { 17 18 /** 19 * A helper class for creating a read-only snapshot of memory-mapped data. 20 * 21 * The Init() method initializes a read-write memory mapped region of the given 22 * size, which can be initialized with arbitrary data. The Finalize() method 23 * remaps that region as read-only (and backs it with a read-only file 24 * descriptor), and returns a handle to it. 25 * 26 * The file descriptor for the resulting AutoMemMap can be shared among 27 * processes, to safely access a shared, read-only copy of the data snapshot. 28 */ 29 class MOZ_RAII MemMapSnapshot { 30 public: 31 Result<Ok, nsresult> Init(size_t aSize); 32 Result<ReadOnlySharedMemoryHandle, nsresult> Finalize(); 33 34 template <typename T> 35 RangedPtr<T> Get() { 36 MOZ_ASSERT(mMem); 37 auto span = mMem.DataAsSpan<T>(); 38 return {span.data(), span.size()}; 39 } 40 41 private: 42 FreezableSharedMemoryMapping mMem; 43 }; 44 45 } // namespace mozilla::ipc 46 47 #endif // dom_ipc_MemMapSnapshot_h