Line data Source code
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 4 : nsHtml5OwningUTF16Buffer::nsHtml5OwningUTF16Buffer(char16_t* aBuffer)
8 : : nsHtml5UTF16Buffer(aBuffer, 0),
9 : next(nullptr),
10 4 : key(nullptr)
11 4 : {}
12 :
13 2 : nsHtml5OwningUTF16Buffer::nsHtml5OwningUTF16Buffer(void* aKey)
14 : : nsHtml5UTF16Buffer(nullptr, 0),
15 : next(nullptr),
16 2 : key(aKey)
17 2 : {}
18 :
19 6 : nsHtml5OwningUTF16Buffer::~nsHtml5OwningUTF16Buffer()
20 : {
21 3 : DeleteBuffer();
22 :
23 : // This is to avoid dtor recursion on 'next', bug 706932.
24 6 : RefPtr<nsHtml5OwningUTF16Buffer> tail;
25 3 : tail.swap(next);
26 5 : while (tail && tail->mRefCnt == 1) {
27 2 : RefPtr<nsHtml5OwningUTF16Buffer> tmp;
28 1 : tmp.swap(tail->next);
29 1 : tail.swap(tmp);
30 : }
31 3 : }
32 :
33 : // static
34 : already_AddRefed<nsHtml5OwningUTF16Buffer>
35 4 : nsHtml5OwningUTF16Buffer::FalliblyCreate(int32_t aLength)
36 : {
37 8 : char16_t* newBuf = new (mozilla::fallible) char16_t[aLength];
38 4 : if (!newBuf) {
39 0 : return nullptr;
40 : }
41 : RefPtr<nsHtml5OwningUTF16Buffer> newObj =
42 8 : new (mozilla::fallible) nsHtml5OwningUTF16Buffer(newBuf);
43 4 : if (!newObj) {
44 0 : delete[] newBuf;
45 0 : return nullptr;
46 : }
47 4 : return newObj.forget();
48 : }
49 :
50 : void
51 0 : nsHtml5OwningUTF16Buffer::Swap(nsHtml5OwningUTF16Buffer* aOther)
52 : {
53 0 : nsHtml5UTF16Buffer::Swap(aOther);
54 0 : }
55 :
56 : Span<char16_t>
57 6 : nsHtml5OwningUTF16Buffer::TailAsSpan(int32_t aBufferSize)
58 : {
59 6 : MOZ_ASSERT(aBufferSize >= getEnd());
60 6 : return MakeSpan(getBuffer() + getEnd(), aBufferSize - getEnd());
61 : }
62 :
63 : void
64 6 : nsHtml5OwningUTF16Buffer::AdvanceEnd(int32_t aNumberOfCodeUnits)
65 : {
66 6 : setEnd(getEnd() + aNumberOfCodeUnits);
67 6 : }
68 :
69 : // Not using macros for AddRef and Release in order to be able to refcount on
70 : // and create on different threads.
71 :
72 : nsrefcnt
73 15 : nsHtml5OwningUTF16Buffer::AddRef()
74 : {
75 15 : NS_PRECONDITION(int32_t(mRefCnt) >= 0, "Illegal refcount.");
76 15 : ++mRefCnt;
77 15 : NS_LOG_ADDREF(this, mRefCnt, "nsHtml5OwningUTF16Buffer", sizeof(*this));
78 15 : return mRefCnt;
79 : }
80 :
81 : nsrefcnt
82 12 : nsHtml5OwningUTF16Buffer::Release()
83 : {
84 12 : NS_PRECONDITION(0 != mRefCnt, "Release without AddRef.");
85 12 : --mRefCnt;
86 12 : NS_LOG_RELEASE(this, mRefCnt, "nsHtml5OwningUTF16Buffer");
87 12 : if (mRefCnt == 0) {
88 3 : mRefCnt = 1; /* stabilize */
89 3 : delete this;
90 3 : return 0;
91 : }
92 9 : return mRefCnt;
93 : }
|