no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / media / SharedBuffer.h
blobd8fa87f9ed14454f5c43f63bd8dd321f2b732ac9
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef MOZILLA_SHAREDBUFFER_H_
7 #define MOZILLA_SHAREDBUFFER_H_
9 #include "mozilla/CheckedInt.h"
10 #include "mozilla/mozalloc.h"
11 #include "mozilla/MemoryReporting.h"
12 #include "nsISupportsImpl.h"
14 namespace mozilla {
16 class AudioBlockBuffer;
17 class ThreadSharedFloatArrayBufferList;
19 /**
20 * Base class for objects with a thread-safe refcount and a virtual
21 * destructor.
23 class ThreadSharedObject {
24 public:
25 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ThreadSharedObject)
27 bool IsShared() { return mRefCnt.get() > 1; }
29 virtual AudioBlockBuffer* AsAudioBlockBuffer() { return nullptr; };
30 virtual ThreadSharedFloatArrayBufferList*
31 AsThreadSharedFloatArrayBufferList() {
32 return nullptr;
35 virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
36 return 0;
39 virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
40 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
43 protected:
44 // Protected destructor, to discourage deletion outside of Release():
45 virtual ~ThreadSharedObject() = default;
48 /**
49 * Heap-allocated chunk of arbitrary data with threadsafe refcounting.
50 * Typically you would allocate one of these, fill it in, and then treat it as
51 * immutable while it's shared.
52 * This only guarantees 4-byte alignment of the data. For alignment we simply
53 * assume that the memory from malloc is at least 4-byte aligned and the
54 * refcount's size is large enough that SharedBuffer's size is divisible by 4.
56 class SharedBuffer : public ThreadSharedObject {
57 public:
58 void* Data() { return this + 1; }
60 // Ensure that the caller has a CheckedInt. We have to take one by
61 // non-const reference to do that, because if we take one by const
62 // reference or value or rvalue reference the implicit constructor on
63 // CheckedInt will helpfully synthesize one for us at the callsite
64 // even if the caller passes a raw size_t.
65 static already_AddRefed<SharedBuffer> Create(CheckedInt<size_t>& aSize,
66 const fallible_t&) {
67 CheckedInt<size_t> allocSize = AllocSize(aSize, fallible);
68 if (!allocSize.isValid()) {
69 return nullptr;
71 void* m = operator new(allocSize.value(), fallible);
72 if (!m) {
73 return nullptr;
75 RefPtr<SharedBuffer> p = new (m) SharedBuffer();
76 return p.forget();
79 static already_AddRefed<SharedBuffer> Create(CheckedInt<size_t>& aSize) {
80 void* m = operator new(AllocSize(aSize));
81 RefPtr<SharedBuffer> p = new (m) SharedBuffer();
82 return p.forget();
85 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override {
86 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
89 private:
90 static CheckedInt<size_t> AllocSize(CheckedInt<size_t> aDataSize,
91 const fallible_t&) {
92 CheckedInt<size_t> size = sizeof(SharedBuffer);
93 size += aDataSize;
94 return size;
97 static size_t AllocSize(CheckedInt<size_t> aDataSize) {
98 CheckedInt<size_t> size = AllocSize(aDataSize, fallible);
99 if (!size.isValid()) {
100 MOZ_CRASH();
102 return size.value();
105 SharedBuffer() {
106 NS_ASSERTION(
107 (reinterpret_cast<char*>(this + 1) - reinterpret_cast<char*>(this)) %
108 4 ==
110 "SharedBuffers should be at least 4-byte aligned");
114 } // namespace mozilla
116 #endif /* MOZILLA_SHAREDBUFFER_H_ */