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"
16 class AudioBlockBuffer
;
17 class ThreadSharedFloatArrayBufferList
;
20 * Base class for objects with a thread-safe refcount and a virtual
23 class ThreadSharedObject
{
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() {
35 virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf
) const {
39 virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const {
40 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf
);
44 // Protected destructor, to discourage deletion outside of Release():
45 virtual ~ThreadSharedObject() = default;
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
{
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
,
67 CheckedInt
<size_t> allocSize
= AllocSize(aSize
, fallible
);
68 if (!allocSize
.isValid()) {
71 void* m
= operator new(allocSize
.value(), fallible
);
75 RefPtr
<SharedBuffer
> p
= new (m
) SharedBuffer();
79 static already_AddRefed
<SharedBuffer
> Create(CheckedInt
<size_t>& aSize
) {
80 void* m
= operator new(AllocSize(aSize
));
81 RefPtr
<SharedBuffer
> p
= new (m
) SharedBuffer();
85 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const override
{
86 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf
);
90 static CheckedInt
<size_t> AllocSize(CheckedInt
<size_t> aDataSize
,
92 CheckedInt
<size_t> size
= sizeof(SharedBuffer
);
97 static size_t AllocSize(CheckedInt
<size_t> aDataSize
) {
98 CheckedInt
<size_t> size
= AllocSize(aDataSize
, fallible
);
99 if (!size
.isValid()) {
107 (reinterpret_cast<char*>(this + 1) - reinterpret_cast<char*>(this)) %
110 "SharedBuffers should be at least 4-byte aligned");
114 } // namespace mozilla
116 #endif /* MOZILLA_SHAREDBUFFER_H_ */