1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/parser/html/nsHtml5OwningUTF16Buffer.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,87 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "nsHtml5OwningUTF16Buffer.h" 1.9 + 1.10 +nsHtml5OwningUTF16Buffer::nsHtml5OwningUTF16Buffer(char16_t* aBuffer) 1.11 + : nsHtml5UTF16Buffer(aBuffer, 0), 1.12 + next(nullptr), 1.13 + key(nullptr) 1.14 +{ 1.15 + MOZ_COUNT_CTOR(nsHtml5OwningUTF16Buffer); 1.16 +} 1.17 + 1.18 +nsHtml5OwningUTF16Buffer::nsHtml5OwningUTF16Buffer(void* aKey) 1.19 + : nsHtml5UTF16Buffer(nullptr, 0), 1.20 + next(nullptr), 1.21 + key(aKey) 1.22 +{ 1.23 + MOZ_COUNT_CTOR(nsHtml5OwningUTF16Buffer); 1.24 +} 1.25 + 1.26 +nsHtml5OwningUTF16Buffer::~nsHtml5OwningUTF16Buffer() 1.27 +{ 1.28 + MOZ_COUNT_DTOR(nsHtml5OwningUTF16Buffer); 1.29 + DeleteBuffer(); 1.30 + 1.31 + // This is to avoid dtor recursion on 'next', bug 706932. 1.32 + nsRefPtr<nsHtml5OwningUTF16Buffer> tail; 1.33 + tail.swap(next); 1.34 + while (tail && tail->mRefCnt == 1) { 1.35 + nsRefPtr<nsHtml5OwningUTF16Buffer> tmp; 1.36 + tmp.swap(tail->next); 1.37 + tail.swap(tmp); 1.38 + } 1.39 +} 1.40 + 1.41 +// static 1.42 +already_AddRefed<nsHtml5OwningUTF16Buffer> 1.43 +nsHtml5OwningUTF16Buffer::FalliblyCreate(int32_t aLength) 1.44 +{ 1.45 + const mozilla::fallible_t fallible = mozilla::fallible_t(); 1.46 + char16_t* newBuf = new (fallible) char16_t[aLength]; 1.47 + if (!newBuf) { 1.48 + return nullptr; 1.49 + } 1.50 + nsRefPtr<nsHtml5OwningUTF16Buffer> newObj = 1.51 + new (fallible) nsHtml5OwningUTF16Buffer(newBuf); 1.52 + if (!newObj) { 1.53 + delete[] newBuf; 1.54 + return nullptr; 1.55 + } 1.56 + return newObj.forget(); 1.57 +} 1.58 + 1.59 +void 1.60 +nsHtml5OwningUTF16Buffer::Swap(nsHtml5OwningUTF16Buffer* aOther) 1.61 +{ 1.62 + nsHtml5UTF16Buffer::Swap(aOther); 1.63 +} 1.64 + 1.65 + 1.66 +// Not using macros for AddRef and Release in order to be able to refcount on 1.67 +// and create on different threads. 1.68 + 1.69 +nsrefcnt 1.70 +nsHtml5OwningUTF16Buffer::AddRef() 1.71 +{ 1.72 + NS_PRECONDITION(int32_t(mRefCnt) >= 0, "Illegal refcount."); 1.73 + ++mRefCnt; 1.74 + NS_LOG_ADDREF(this, mRefCnt, "nsHtml5OwningUTF16Buffer", sizeof(*this)); 1.75 + return mRefCnt; 1.76 +} 1.77 + 1.78 +nsrefcnt 1.79 +nsHtml5OwningUTF16Buffer::Release() 1.80 +{ 1.81 + NS_PRECONDITION(0 != mRefCnt, "Release without AddRef."); 1.82 + --mRefCnt; 1.83 + NS_LOG_RELEASE(this, mRefCnt, "nsHtml5OwningUTF16Buffer"); 1.84 + if (mRefCnt == 0) { 1.85 + mRefCnt = 1; /* stabilize */ 1.86 + delete this; 1.87 + return 0; 1.88 + } 1.89 + return mRefCnt; 1.90 +}