Backed out changeset bcbab342eed8 (bug 1889658) for causing wpt reftest failures...
[gecko.git] / parser / html / nsHtml5OwningUTF16Buffer.cpp
blobeec07a29acec51d3768bdbdbd306e2c4b20ddaed
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/. */
5 #include "nsHtml5OwningUTF16Buffer.h"
7 #include "mozilla/Span.h"
9 using namespace mozilla;
11 nsHtml5OwningUTF16Buffer::nsHtml5OwningUTF16Buffer(char16_t* aBuffer)
12 : nsHtml5UTF16Buffer(aBuffer, 0), next(nullptr), key(nullptr) {}
14 nsHtml5OwningUTF16Buffer::nsHtml5OwningUTF16Buffer(void* aKey)
15 : nsHtml5UTF16Buffer(nullptr, 0), next(nullptr), key(aKey) {}
17 nsHtml5OwningUTF16Buffer::~nsHtml5OwningUTF16Buffer() {
18 DeleteBuffer();
20 // This is to avoid dtor recursion on 'next', bug 706932.
21 RefPtr<nsHtml5OwningUTF16Buffer> tail;
22 tail.swap(next);
23 while (tail && tail->mRefCnt == 1) {
24 RefPtr<nsHtml5OwningUTF16Buffer> tmp;
25 tmp.swap(tail->next);
26 tail.swap(tmp);
30 // static
31 already_AddRefed<nsHtml5OwningUTF16Buffer>
32 nsHtml5OwningUTF16Buffer::FalliblyCreate(int32_t aLength) {
33 char16_t* newBuf = new (mozilla::fallible) char16_t[aLength];
34 if (!newBuf) {
35 return nullptr;
37 RefPtr<nsHtml5OwningUTF16Buffer> newObj =
38 new (mozilla::fallible) nsHtml5OwningUTF16Buffer(newBuf);
39 if (!newObj) {
40 delete[] newBuf;
41 return nullptr;
43 return newObj.forget();
46 void nsHtml5OwningUTF16Buffer::Swap(nsHtml5OwningUTF16Buffer* aOther) {
47 nsHtml5UTF16Buffer::Swap(aOther);
50 Span<char16_t> nsHtml5OwningUTF16Buffer::TailAsSpan(int32_t aBufferSize) {
51 MOZ_ASSERT(aBufferSize >= getEnd());
52 return {getBuffer() + getEnd(), static_cast<size_t>(aBufferSize - getEnd())};
55 void nsHtml5OwningUTF16Buffer::AdvanceEnd(int32_t aNumberOfCodeUnits) {
56 setEnd(getEnd() + aNumberOfCodeUnits);