2012-05-29 19:52:43 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-09-28 16:45:17 +04:00
|
|
|
|
|
|
|
#include "nsHtml5OwningUTF16Buffer.h"
|
|
|
|
|
2017-12-05 00:09:15 +03:00
|
|
|
#include "mozilla/Span.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
nsHtml5OwningUTF16Buffer::nsHtml5OwningUTF16Buffer(char16_t* aBuffer)
|
2018-03-16 18:26:06 +03:00
|
|
|
: nsHtml5UTF16Buffer(aBuffer, 0), next(nullptr), key(nullptr) {}
|
2011-09-28 16:45:17 +04:00
|
|
|
|
|
|
|
nsHtml5OwningUTF16Buffer::nsHtml5OwningUTF16Buffer(void* aKey)
|
2018-03-16 18:26:06 +03:00
|
|
|
: nsHtml5UTF16Buffer(nullptr, 0), next(nullptr), key(aKey) {}
|
2011-09-28 16:45:17 +04:00
|
|
|
|
|
|
|
nsHtml5OwningUTF16Buffer::~nsHtml5OwningUTF16Buffer() {
|
|
|
|
DeleteBuffer();
|
2011-12-18 01:19:20 +04:00
|
|
|
|
|
|
|
// This is to avoid dtor recursion on 'next', bug 706932.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsHtml5OwningUTF16Buffer> tail;
|
2011-12-18 01:19:20 +04:00
|
|
|
tail.swap(next);
|
|
|
|
while (tail && tail->mRefCnt == 1) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsHtml5OwningUTF16Buffer> tmp;
|
2011-12-18 01:19:20 +04:00
|
|
|
tmp.swap(tail->next);
|
|
|
|
tail.swap(tmp);
|
|
|
|
}
|
2011-09-28 16:45:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<nsHtml5OwningUTF16Buffer>
|
2012-08-22 19:56:38 +04:00
|
|
|
nsHtml5OwningUTF16Buffer::FalliblyCreate(int32_t aLength) {
|
2015-01-28 12:00:40 +03:00
|
|
|
char16_t* newBuf = new (mozilla::fallible) char16_t[aLength];
|
2011-09-28 16:45:17 +04:00
|
|
|
if (!newBuf) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-09-28 16:45:17 +04:00
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsHtml5OwningUTF16Buffer> newObj =
|
2015-01-28 12:00:40 +03:00
|
|
|
new (mozilla::fallible) nsHtml5OwningUTF16Buffer(newBuf);
|
2011-09-28 16:45:17 +04:00
|
|
|
if (!newObj) {
|
|
|
|
delete[] newBuf;
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-09-28 16:45:17 +04:00
|
|
|
}
|
|
|
|
return newObj.forget();
|
|
|
|
}
|
|
|
|
|
2011-10-30 00:14:31 +04:00
|
|
|
void nsHtml5OwningUTF16Buffer::Swap(nsHtml5OwningUTF16Buffer* aOther) {
|
|
|
|
nsHtml5UTF16Buffer::Swap(aOther);
|
|
|
|
}
|
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
Span<char16_t> nsHtml5OwningUTF16Buffer::TailAsSpan(int32_t aBufferSize) {
|
|
|
|
MOZ_ASSERT(aBufferSize >= getEnd());
|
|
|
|
return MakeSpan(getBuffer() + getEnd(), aBufferSize - getEnd());
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsHtml5OwningUTF16Buffer::AdvanceEnd(int32_t aNumberOfCodeUnits) {
|
|
|
|
setEnd(getEnd() + aNumberOfCodeUnits);
|
|
|
|
}
|
2011-10-30 00:14:31 +04:00
|
|
|
|
2011-09-28 16:45:17 +04:00
|
|
|
// Not using macros for AddRef and Release in order to be able to refcount on
|
|
|
|
// and create on different threads.
|
|
|
|
|
|
|
|
nsrefcnt nsHtml5OwningUTF16Buffer::AddRef() {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(int32_t(mRefCnt) >= 0, "Illegal refcount.");
|
2011-09-28 16:45:17 +04:00
|
|
|
++mRefCnt;
|
|
|
|
NS_LOG_ADDREF(this, mRefCnt, "nsHtml5OwningUTF16Buffer", sizeof(*this));
|
|
|
|
return mRefCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsrefcnt nsHtml5OwningUTF16Buffer::Release() {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(0 != mRefCnt, "Release without AddRef.");
|
2011-09-28 16:45:17 +04:00
|
|
|
--mRefCnt;
|
|
|
|
NS_LOG_RELEASE(this, mRefCnt, "nsHtml5OwningUTF16Buffer");
|
|
|
|
if (mRefCnt == 0) {
|
|
|
|
mRefCnt = 1; /* stabilize */
|
|
|
|
delete this;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return mRefCnt;
|
|
|
|
}
|