Bug 1867190 - Add prefs for PHC probablities r=glandium
[gecko.git] / xpcom / base / RLBoxUtils.h
blob4a73affb63cf3d50549c8818633530595ba8bc63
1 /* -*- Mode: C++; tab-width: 20; 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 SECURITY_RLBOX_UTILS_H_
8 #define SECURITY_RLBOX_UTILS_H_
10 #include "mozilla/rlbox/rlbox_types.hpp"
12 namespace mozilla {
14 /* The RLBoxTransferBufferToSandbox class is used to copy (or directly expose in
15 * the noop-sandbox case) buffers into the sandbox that are automatically freed
16 * when the RLBoxTransferBufferToSandbox is out of scope. NOTE: The sandbox
17 * lifetime must outlive all of its RLBoxTransferBufferToSandbox.
19 template <typename T, typename S>
20 class MOZ_STACK_CLASS RLBoxTransferBufferToSandbox {
21 public:
22 RLBoxTransferBufferToSandbox() = delete;
23 RLBoxTransferBufferToSandbox(rlbox::rlbox_sandbox<S>* aSandbox, const T* aBuf,
24 const size_t aLen)
25 : mSandbox(aSandbox), mCopied(false), mBuf(nullptr) {
26 if (aBuf) {
27 mBuf = rlbox::copy_memory_or_grant_access(*mSandbox, aBuf, aLen, false,
28 mCopied);
31 ~RLBoxTransferBufferToSandbox() {
32 if (mCopied) {
33 mSandbox->free_in_sandbox(mBuf);
36 rlbox::tainted<const T*, S> operator*() const { return mBuf; };
38 private:
39 rlbox::rlbox_sandbox<S>* mSandbox;
40 bool mCopied;
41 rlbox::tainted<const T*, S> mBuf;
44 /* The RLBoxAllocateInSandbox class is used to allocate data int sandbox that is
45 * automatically freed when the RLBoxAllocateInSandbox is out of scope. NOTE:
46 * The sandbox lifetime must outlive all of its RLBoxAllocateInSandbox'ations.
48 template <typename T, typename S>
49 class MOZ_STACK_CLASS RLBoxAllocateInSandbox {
50 public:
51 RLBoxAllocateInSandbox() = delete;
52 explicit RLBoxAllocateInSandbox(rlbox::rlbox_sandbox<S>* aSandbox)
53 : mSandbox(aSandbox) {
54 mPtr = mSandbox->template malloc_in_sandbox<T>();
56 ~RLBoxAllocateInSandbox() {
57 if (mPtr) {
58 mSandbox->free_in_sandbox(mPtr);
61 rlbox::tainted<T*, S> get() const { return mPtr; };
63 private:
64 rlbox::rlbox_sandbox<S>* mSandbox;
65 rlbox::tainted<T*, S> mPtr;
68 } // namespace mozilla
70 #endif