MemMapSnapshot.cpp (1124B)
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 #include "MemMapSnapshot.h" 8 9 #include "mozilla/ResultExtensions.h" 10 #include "mozilla/ipc/SharedMemoryHandle.h" 11 #include "nsDebug.h" 12 13 namespace mozilla::ipc { 14 15 Result<Ok, nsresult> MemMapSnapshot::Init(size_t aSize) { 16 MOZ_ASSERT(!mMem); 17 18 auto handle = shared_memory::CreateFreezable(aSize); 19 if (NS_WARN_IF(!handle)) { 20 return Err(NS_ERROR_FAILURE); 21 } 22 23 auto mem = std::move(handle).Map(); 24 if (NS_WARN_IF(!mem)) { 25 return Err(NS_ERROR_FAILURE); 26 } 27 28 mMem = std::move(mem); 29 return Ok(); 30 } 31 32 Result<ReadOnlySharedMemoryHandle, nsresult> MemMapSnapshot::Finalize() { 33 MOZ_ASSERT(mMem); 34 35 auto readOnlyHandle = std::move(mMem).Freeze(); 36 if (NS_WARN_IF(!readOnlyHandle)) { 37 return Err(NS_ERROR_FAILURE); 38 } 39 40 return std::move(readOnlyHandle); 41 } 42 43 } // namespace mozilla::ipc