michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_SHAREDBUFFER_H_ michael@0: #define MOZILLA_SHAREDBUFFER_H_ michael@0: michael@0: #include "mozilla/CheckedInt.h" michael@0: #include "mozilla/mozalloc.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsAutoPtr.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: /** michael@0: * Base class for objects with a thread-safe refcount and a virtual michael@0: * destructor. michael@0: */ michael@0: class ThreadSharedObject { michael@0: public: michael@0: NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ThreadSharedObject) michael@0: michael@0: bool IsShared() { return mRefCnt.get() > 1; } michael@0: michael@0: virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: return 0; michael@0: } michael@0: michael@0: virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); michael@0: } michael@0: protected: michael@0: // Protected destructor, to discourage deletion outside of Release(): michael@0: virtual ~ThreadSharedObject() {} michael@0: }; michael@0: michael@0: /** michael@0: * Heap-allocated chunk of arbitrary data with threadsafe refcounting. michael@0: * Typically you would allocate one of these, fill it in, and then treat it as michael@0: * immutable while it's shared. michael@0: * This only guarantees 4-byte alignment of the data. For alignment we michael@0: * simply assume that the refcount is at least 4-byte aligned and its size michael@0: * is divisible by 4. michael@0: */ michael@0: class SharedBuffer : public ThreadSharedObject { michael@0: public: michael@0: void* Data() { return this + 1; } michael@0: michael@0: static already_AddRefed Create(size_t aSize) michael@0: { michael@0: CheckedInt size = sizeof(SharedBuffer); michael@0: size += aSize; michael@0: if (!size.isValid()) { michael@0: MOZ_CRASH(); michael@0: } michael@0: void* m = moz_xmalloc(size.value()); michael@0: nsRefPtr p = new (m) SharedBuffer(); michael@0: NS_ASSERTION((reinterpret_cast(p.get() + 1) - reinterpret_cast(p.get())) % 4 == 0, michael@0: "SharedBuffers should be at least 4-byte aligned"); michael@0: return p.forget(); michael@0: } michael@0: michael@0: virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE michael@0: { michael@0: return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); michael@0: } michael@0: michael@0: private: michael@0: SharedBuffer() {} michael@0: }; michael@0: michael@0: } michael@0: michael@0: #endif /* MOZILLA_SHAREDBUFFER_H_ */