parser/html/nsHtml5OwningUTF16Buffer.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "nsHtml5OwningUTF16Buffer.h"
michael@0 6
michael@0 7 nsHtml5OwningUTF16Buffer::nsHtml5OwningUTF16Buffer(char16_t* aBuffer)
michael@0 8 : nsHtml5UTF16Buffer(aBuffer, 0),
michael@0 9 next(nullptr),
michael@0 10 key(nullptr)
michael@0 11 {
michael@0 12 MOZ_COUNT_CTOR(nsHtml5OwningUTF16Buffer);
michael@0 13 }
michael@0 14
michael@0 15 nsHtml5OwningUTF16Buffer::nsHtml5OwningUTF16Buffer(void* aKey)
michael@0 16 : nsHtml5UTF16Buffer(nullptr, 0),
michael@0 17 next(nullptr),
michael@0 18 key(aKey)
michael@0 19 {
michael@0 20 MOZ_COUNT_CTOR(nsHtml5OwningUTF16Buffer);
michael@0 21 }
michael@0 22
michael@0 23 nsHtml5OwningUTF16Buffer::~nsHtml5OwningUTF16Buffer()
michael@0 24 {
michael@0 25 MOZ_COUNT_DTOR(nsHtml5OwningUTF16Buffer);
michael@0 26 DeleteBuffer();
michael@0 27
michael@0 28 // This is to avoid dtor recursion on 'next', bug 706932.
michael@0 29 nsRefPtr<nsHtml5OwningUTF16Buffer> tail;
michael@0 30 tail.swap(next);
michael@0 31 while (tail && tail->mRefCnt == 1) {
michael@0 32 nsRefPtr<nsHtml5OwningUTF16Buffer> tmp;
michael@0 33 tmp.swap(tail->next);
michael@0 34 tail.swap(tmp);
michael@0 35 }
michael@0 36 }
michael@0 37
michael@0 38 // static
michael@0 39 already_AddRefed<nsHtml5OwningUTF16Buffer>
michael@0 40 nsHtml5OwningUTF16Buffer::FalliblyCreate(int32_t aLength)
michael@0 41 {
michael@0 42 const mozilla::fallible_t fallible = mozilla::fallible_t();
michael@0 43 char16_t* newBuf = new (fallible) char16_t[aLength];
michael@0 44 if (!newBuf) {
michael@0 45 return nullptr;
michael@0 46 }
michael@0 47 nsRefPtr<nsHtml5OwningUTF16Buffer> newObj =
michael@0 48 new (fallible) nsHtml5OwningUTF16Buffer(newBuf);
michael@0 49 if (!newObj) {
michael@0 50 delete[] newBuf;
michael@0 51 return nullptr;
michael@0 52 }
michael@0 53 return newObj.forget();
michael@0 54 }
michael@0 55
michael@0 56 void
michael@0 57 nsHtml5OwningUTF16Buffer::Swap(nsHtml5OwningUTF16Buffer* aOther)
michael@0 58 {
michael@0 59 nsHtml5UTF16Buffer::Swap(aOther);
michael@0 60 }
michael@0 61
michael@0 62
michael@0 63 // Not using macros for AddRef and Release in order to be able to refcount on
michael@0 64 // and create on different threads.
michael@0 65
michael@0 66 nsrefcnt
michael@0 67 nsHtml5OwningUTF16Buffer::AddRef()
michael@0 68 {
michael@0 69 NS_PRECONDITION(int32_t(mRefCnt) >= 0, "Illegal refcount.");
michael@0 70 ++mRefCnt;
michael@0 71 NS_LOG_ADDREF(this, mRefCnt, "nsHtml5OwningUTF16Buffer", sizeof(*this));
michael@0 72 return mRefCnt;
michael@0 73 }
michael@0 74
michael@0 75 nsrefcnt
michael@0 76 nsHtml5OwningUTF16Buffer::Release()
michael@0 77 {
michael@0 78 NS_PRECONDITION(0 != mRefCnt, "Release without AddRef.");
michael@0 79 --mRefCnt;
michael@0 80 NS_LOG_RELEASE(this, mRefCnt, "nsHtml5OwningUTF16Buffer");
michael@0 81 if (mRefCnt == 0) {
michael@0 82 mRefCnt = 1; /* stabilize */
michael@0 83 delete this;
michael@0 84 return 0;
michael@0 85 }
michael@0 86 return mRefCnt;
michael@0 87 }

mercurial