2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-11-30 18:39:55 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2018-06-23 06:30:23 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "MemMapSnapshot.h"
|
|
|
|
|
2019-08-15 01:48:40 +03:00
|
|
|
#include "mozilla/AutoMemMap.h"
|
2018-06-23 06:30:23 +03:00
|
|
|
#include "mozilla/ResultExtensions.h"
|
2020-10-28 15:31:35 +03:00
|
|
|
#include "mozilla/ipc/FileDescriptor.h"
|
2018-06-23 06:30:23 +03:00
|
|
|
|
2020-11-04 11:54:36 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace ipc {
|
2018-06-23 06:30:23 +03:00
|
|
|
|
|
|
|
Result<Ok, nsresult> MemMapSnapshot::Init(size_t aSize) {
|
|
|
|
MOZ_ASSERT(!mInitialized);
|
|
|
|
|
2019-08-15 01:48:40 +03:00
|
|
|
if (NS_WARN_IF(!mMem.CreateFreezeable(aSize))) {
|
2018-06-23 06:30:23 +03:00
|
|
|
return Err(NS_ERROR_FAILURE);
|
|
|
|
}
|
2019-08-15 01:48:40 +03:00
|
|
|
if (NS_WARN_IF(!mMem.Map(aSize))) {
|
2018-06-23 06:30:23 +03:00
|
|
|
return Err(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
2019-08-15 01:48:40 +03:00
|
|
|
mInitialized = true;
|
|
|
|
return Ok();
|
2018-06-23 06:30:23 +03:00
|
|
|
}
|
|
|
|
|
2019-08-15 01:48:40 +03:00
|
|
|
Result<Ok, nsresult> MemMapSnapshot::Finalize(loader::AutoMemMap& aMem) {
|
|
|
|
MOZ_ASSERT(mInitialized);
|
2019-06-20 19:54:57 +03:00
|
|
|
|
2019-08-15 01:48:40 +03:00
|
|
|
if (NS_WARN_IF(!mMem.Freeze())) {
|
2019-06-28 22:42:00 +03:00
|
|
|
return Err(NS_ERROR_FAILURE);
|
|
|
|
}
|
2019-08-15 01:48:40 +03:00
|
|
|
// TakeHandle resets mMem, so call max_size first.
|
|
|
|
size_t size = mMem.max_size();
|
|
|
|
FileDescriptor memHandle(mMem.TakeHandle());
|
|
|
|
MOZ_TRY(aMem.initWithHandle(memHandle, size));
|
2019-06-28 22:42:00 +03:00
|
|
|
|
2019-08-15 01:48:40 +03:00
|
|
|
mInitialized = false;
|
2018-06-23 06:30:23 +03:00
|
|
|
return Ok();
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:54:36 +03:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|