bug 1391482 add a fallible SharedBuffer::Create() r?padenot
The existing infallible Create() is also changed to use operator new(size_t)
to match the non-array delete when the ref count drops to zero.
MozReview-Commit-ID: HZGjSKnV8I2
--- a/dom/media/SharedBuffer.h
+++ b/dom/media/SharedBuffer.h
@@ -52,34 +52,52 @@ protected:
* This only guarantees 4-byte alignment of the data. For alignment we simply
* assume that the memory from malloc is at least 4-byte aligned and the
* refcount's size is large enough that SharedBuffer's size is divisible by 4.
*/
class SharedBuffer : public ThreadSharedObject {
public:
void* Data() { return this + 1; }
+ static already_AddRefed<SharedBuffer> Create(size_t aSize, const fallible_t&)
+ {
+ void* m = operator new(AllocSize(aSize), fallible);
+ if (!m) {
+ return nullptr;
+ }
+ RefPtr<SharedBuffer> p = new (m) SharedBuffer();
+ return p.forget();
+ }
+
static already_AddRefed<SharedBuffer> Create(size_t aSize)
{
- CheckedInt<size_t> size = sizeof(SharedBuffer);
- size += aSize;
- if (!size.isValid()) {
- MOZ_CRASH();
- }
- void* m = moz_xmalloc(size.value());
+ void* m = operator new(AllocSize(aSize));
RefPtr<SharedBuffer> p = new (m) SharedBuffer();
- NS_ASSERTION((reinterpret_cast<char*>(p.get() + 1) - reinterpret_cast<char*>(p.get())) % 4 == 0,
- "SharedBuffers should be at least 4-byte aligned");
return p.forget();
}
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
{
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
}
private:
- SharedBuffer() {}
+ static size_t
+ AllocSize(size_t aDataSize)
+ {
+ CheckedInt<size_t> size = sizeof(SharedBuffer);
+ size += aDataSize;
+ if (!size.isValid()) {
+ MOZ_CRASH();
+ }
+ return size.value();
+ }
+
+ SharedBuffer()
+ {
+ NS_ASSERTION((reinterpret_cast<char*>(this + 1) - reinterpret_cast<char*>(this)) % 4 == 0,
+ "SharedBuffers should be at least 4-byte aligned");
+ }
};
} // namespace mozilla
#endif /* MOZILLA_SHAREDBUFFER_H_ */