content/media/SharedBuffer.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef MOZILLA_SHAREDBUFFER_H_
michael@0 7 #define MOZILLA_SHAREDBUFFER_H_
michael@0 8
michael@0 9 #include "mozilla/CheckedInt.h"
michael@0 10 #include "mozilla/mozalloc.h"
michael@0 11 #include "nsCOMPtr.h"
michael@0 12 #include "nsAutoPtr.h"
michael@0 13
michael@0 14 namespace mozilla {
michael@0 15
michael@0 16 /**
michael@0 17 * Base class for objects with a thread-safe refcount and a virtual
michael@0 18 * destructor.
michael@0 19 */
michael@0 20 class ThreadSharedObject {
michael@0 21 public:
michael@0 22 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ThreadSharedObject)
michael@0 23
michael@0 24 bool IsShared() { return mRefCnt.get() > 1; }
michael@0 25
michael@0 26 virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
michael@0 27 {
michael@0 28 return 0;
michael@0 29 }
michael@0 30
michael@0 31 virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
michael@0 32 {
michael@0 33 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
michael@0 34 }
michael@0 35 protected:
michael@0 36 // Protected destructor, to discourage deletion outside of Release():
michael@0 37 virtual ~ThreadSharedObject() {}
michael@0 38 };
michael@0 39
michael@0 40 /**
michael@0 41 * Heap-allocated chunk of arbitrary data with threadsafe refcounting.
michael@0 42 * Typically you would allocate one of these, fill it in, and then treat it as
michael@0 43 * immutable while it's shared.
michael@0 44 * This only guarantees 4-byte alignment of the data. For alignment we
michael@0 45 * simply assume that the refcount is at least 4-byte aligned and its size
michael@0 46 * is divisible by 4.
michael@0 47 */
michael@0 48 class SharedBuffer : public ThreadSharedObject {
michael@0 49 public:
michael@0 50 void* Data() { return this + 1; }
michael@0 51
michael@0 52 static already_AddRefed<SharedBuffer> Create(size_t aSize)
michael@0 53 {
michael@0 54 CheckedInt<size_t> size = sizeof(SharedBuffer);
michael@0 55 size += aSize;
michael@0 56 if (!size.isValid()) {
michael@0 57 MOZ_CRASH();
michael@0 58 }
michael@0 59 void* m = moz_xmalloc(size.value());
michael@0 60 nsRefPtr<SharedBuffer> p = new (m) SharedBuffer();
michael@0 61 NS_ASSERTION((reinterpret_cast<char*>(p.get() + 1) - reinterpret_cast<char*>(p.get())) % 4 == 0,
michael@0 62 "SharedBuffers should be at least 4-byte aligned");
michael@0 63 return p.forget();
michael@0 64 }
michael@0 65
michael@0 66 virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
michael@0 67 {
michael@0 68 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
michael@0 69 }
michael@0 70
michael@0 71 private:
michael@0 72 SharedBuffer() {}
michael@0 73 };
michael@0 74
michael@0 75 }
michael@0 76
michael@0 77 #endif /* MOZILLA_SHAREDBUFFER_H_ */

mercurial