Bug 1882593 [wpt PR 44836] - Add test for unknown, invalid ancillary chunk which...
[gecko.git] / dom / ipc / MemMapSnapshot.h
blob12b4d353ad4fd247ff8131970a42de4b54ceb8b3
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/. */
7 #ifndef dom_ipc_MemMapSnapshot_h
8 #define dom_ipc_MemMapSnapshot_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/Maybe.h"
12 #include "mozilla/RangedPtr.h"
13 #include "mozilla/Result.h"
14 #include "base/shared_memory.h"
15 #include "ErrorList.h"
17 namespace mozilla {
18 namespace loader {
19 class AutoMemMap;
22 namespace ipc {
24 /**
25 * A helper class for creating a read-only snapshot of memory-mapped data.
27 * The Init() method initializes a read-write memory mapped region of the given
28 * size, which can be initialized with arbitrary data. The Finalize() method
29 * remaps that region as read-only (and backs it with a read-only file
30 * descriptor), and initializes an AutoMemMap with the new contents.
32 * The file descriptor for the resulting AutoMemMap can be shared among
33 * processes, to safely access a shared, read-only copy of the data snapshot.
35 class MOZ_RAII MemMapSnapshot {
36 public:
37 Result<Ok, nsresult> Init(size_t aSize);
38 Result<Ok, nsresult> Finalize(loader::AutoMemMap& aMap);
40 template <typename T>
41 RangedPtr<T> Get() {
42 MOZ_ASSERT(mInitialized);
43 return {static_cast<T*>(mMem.memory()), mMem.max_size() / sizeof(T)};
46 private:
47 base::SharedMemory mMem;
48 bool mInitialized = false;
51 } // namespace ipc
52 } // namespace mozilla
54 #endif // dom_ipc_MemMapSnapshot_h