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 michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsHtml5OwningUTF16Buffer.h" michael@0: michael@0: nsHtml5OwningUTF16Buffer::nsHtml5OwningUTF16Buffer(char16_t* aBuffer) michael@0: : nsHtml5UTF16Buffer(aBuffer, 0), michael@0: next(nullptr), michael@0: key(nullptr) michael@0: { michael@0: MOZ_COUNT_CTOR(nsHtml5OwningUTF16Buffer); michael@0: } michael@0: michael@0: nsHtml5OwningUTF16Buffer::nsHtml5OwningUTF16Buffer(void* aKey) michael@0: : nsHtml5UTF16Buffer(nullptr, 0), michael@0: next(nullptr), michael@0: key(aKey) michael@0: { michael@0: MOZ_COUNT_CTOR(nsHtml5OwningUTF16Buffer); michael@0: } michael@0: michael@0: nsHtml5OwningUTF16Buffer::~nsHtml5OwningUTF16Buffer() michael@0: { michael@0: MOZ_COUNT_DTOR(nsHtml5OwningUTF16Buffer); michael@0: DeleteBuffer(); michael@0: michael@0: // This is to avoid dtor recursion on 'next', bug 706932. michael@0: nsRefPtr tail; michael@0: tail.swap(next); michael@0: while (tail && tail->mRefCnt == 1) { michael@0: nsRefPtr tmp; michael@0: tmp.swap(tail->next); michael@0: tail.swap(tmp); michael@0: } michael@0: } michael@0: michael@0: // static michael@0: already_AddRefed michael@0: nsHtml5OwningUTF16Buffer::FalliblyCreate(int32_t aLength) michael@0: { michael@0: const mozilla::fallible_t fallible = mozilla::fallible_t(); michael@0: char16_t* newBuf = new (fallible) char16_t[aLength]; michael@0: if (!newBuf) { michael@0: return nullptr; michael@0: } michael@0: nsRefPtr newObj = michael@0: new (fallible) nsHtml5OwningUTF16Buffer(newBuf); michael@0: if (!newObj) { michael@0: delete[] newBuf; michael@0: return nullptr; michael@0: } michael@0: return newObj.forget(); michael@0: } michael@0: michael@0: void michael@0: nsHtml5OwningUTF16Buffer::Swap(nsHtml5OwningUTF16Buffer* aOther) michael@0: { michael@0: nsHtml5UTF16Buffer::Swap(aOther); michael@0: } michael@0: michael@0: michael@0: // Not using macros for AddRef and Release in order to be able to refcount on michael@0: // and create on different threads. michael@0: michael@0: nsrefcnt michael@0: nsHtml5OwningUTF16Buffer::AddRef() michael@0: { michael@0: NS_PRECONDITION(int32_t(mRefCnt) >= 0, "Illegal refcount."); michael@0: ++mRefCnt; michael@0: NS_LOG_ADDREF(this, mRefCnt, "nsHtml5OwningUTF16Buffer", sizeof(*this)); michael@0: return mRefCnt; michael@0: } michael@0: michael@0: nsrefcnt michael@0: nsHtml5OwningUTF16Buffer::Release() michael@0: { michael@0: NS_PRECONDITION(0 != mRefCnt, "Release without AddRef."); michael@0: --mRefCnt; michael@0: NS_LOG_RELEASE(this, mRefCnt, "nsHtml5OwningUTF16Buffer"); michael@0: if (mRefCnt == 0) { michael@0: mRefCnt = 1; /* stabilize */ michael@0: delete this; michael@0: return 0; michael@0: } michael@0: return mRefCnt; michael@0: }