parser/html/nsHtml5OwningUTF16Buffer.cpp

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

mercurial