Skip to content

Commit

Permalink
Bug 1892257 - Move nsStringBuffer to mfbt. r=nika,xpcom-reviewers,gla…
Browse files Browse the repository at this point in the history
…ndium

Inline Create() and Realloc() so that we don't get negative leaks, since
were that code end up in mozglue, it wouldn't have access to the logging
machinery.

Differential Revision: https://phabricator.services.mozilla.com/D209663
  • Loading branch information
emilio committed May 14, 2024
1 parent d691a9d commit d55796b
Show file tree
Hide file tree
Showing 45 changed files with 293 additions and 316 deletions.
1 change: 0 additions & 1 deletion caps/nsJSPrincipals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "nsString.h"
#include "nsJSPrincipals.h"
#include "nsCOMPtr.h"
#include "nsStringBuffer.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/dom/StructuredCloneTags.h"
Expand Down
49 changes: 25 additions & 24 deletions dom/base/nsAttrValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ bool MiscContainer::GetString(nsAString& aString) const {
return false;
}
if (isString) {
auto* buffer = static_cast<nsStringBuffer*>(ptr);
auto* buffer = static_cast<mozilla::StringBuffer*>(ptr);
aString.Assign(buffer, buffer->StorageSize() / sizeof(char16_t) - 1);
} else {
static_cast<nsAtom*>(ptr)->ToString(aString);
Expand Down Expand Up @@ -280,7 +280,7 @@ void nsAttrValue::Shutdown() {
void nsAttrValue::Reset() {
switch (BaseType()) {
case eStringBase: {
if (auto* str = static_cast<nsStringBuffer*>(GetPtr())) {
if (auto* str = static_cast<mozilla::StringBuffer*>(GetPtr())) {
str->Release();
}
break;
Expand Down Expand Up @@ -318,7 +318,7 @@ void nsAttrValue::SetTo(const nsAttrValue& aOther) {
switch (aOther.BaseType()) {
case eStringBase: {
ResetIfSet();
if (auto* str = static_cast<nsStringBuffer*>(aOther.GetPtr())) {
if (auto* str = static_cast<mozilla::StringBuffer*>(aOther.GetPtr())) {
str->AddRef();
SetPtrValueAndType(str, eStringBase);
}
Expand Down Expand Up @@ -395,7 +395,7 @@ void nsAttrValue::SetTo(const nsAttrValue& aOther) {
bool isString;
if (void* otherPtr = otherCont->GetStringOrAtomPtr(isString)) {
if (isString) {
static_cast<nsStringBuffer*>(otherPtr)->AddRef();
static_cast<mozilla::StringBuffer*>(otherPtr)->AddRef();
} else {
static_cast<nsAtom*>(otherPtr)->AddRef();
}
Expand All @@ -408,7 +408,7 @@ void nsAttrValue::SetTo(const nsAttrValue& aOther) {

void nsAttrValue::SetTo(const nsAString& aValue) {
ResetIfSet();
nsStringBuffer* buf = GetStringBuffer(aValue).take();
mozilla::StringBuffer* buf = GetStringBuffer(aValue).take();
if (buf) {
SetPtrValueAndType(buf, eStringBase);
}
Expand Down Expand Up @@ -589,7 +589,7 @@ void nsAttrValue::RemoveDuplicatesFromAtomArray() {
if (void* otherPtr = oldCont->GetStringOrAtomPtr(isString)) {
stringBits = oldCont->mStringBits;
if (isString) {
static_cast<nsStringBuffer*>(otherPtr)->AddRef();
static_cast<mozilla::StringBuffer*>(otherPtr)->AddRef();
} else {
static_cast<nsAtom*>(otherPtr)->AddRef();
}
Expand Down Expand Up @@ -620,7 +620,7 @@ void nsAttrValue::ToString(nsAString& aResult) const {

switch (Type()) {
case eString: {
if (auto* str = static_cast<nsStringBuffer*>(GetPtr())) {
if (auto* str = static_cast<mozilla::StringBuffer*>(GetPtr())) {
aResult.Assign(str, str->StorageSize() / sizeof(char16_t) - 1);
} else {
aResult.Truncate();
Expand Down Expand Up @@ -770,7 +770,7 @@ already_AddRefed<nsAtom> nsAttrValue::GetAsAtom() const {
const nsCheapString nsAttrValue::GetStringValue() const {
MOZ_ASSERT(Type() == eString, "wrong type");

return nsCheapString(static_cast<nsStringBuffer*>(GetPtr()));
return nsCheapString(static_cast<mozilla::StringBuffer*>(GetPtr()));
}

bool nsAttrValue::GetColorValue(nscolor& aColor) const {
Expand Down Expand Up @@ -890,7 +890,7 @@ nsAtom* nsAttrValue::AtomAt(int32_t aIndex) const {
uint32_t nsAttrValue::HashValue() const {
switch (BaseType()) {
case eStringBase: {
if (auto* str = static_cast<nsStringBuffer*>(GetPtr())) {
if (auto* str = static_cast<mozilla::StringBuffer*>(GetPtr())) {
uint32_t len = str->StorageSize() / sizeof(char16_t) - 1;
return HashString(static_cast<char16_t*>(str->Data()), len);
}
Expand Down Expand Up @@ -1058,9 +1058,9 @@ bool nsAttrValue::Equals(const nsAttrValue& aOther) const {
(static_cast<ValueBaseType>(otherCont->mStringBits &
NS_ATTRVALUE_BASETYPE_MASK) ==
eStringBase)) {
return nsCheapString(reinterpret_cast<nsStringBuffer*>(
return nsCheapString(reinterpret_cast<mozilla::StringBuffer*>(
static_cast<uintptr_t>(thisCont->mStringBits)))
.Equals(nsCheapString(reinterpret_cast<nsStringBuffer*>(
.Equals(nsCheapString(reinterpret_cast<mozilla::StringBuffer*>(
static_cast<uintptr_t>(otherCont->mStringBits))));
}
}
Expand All @@ -1071,7 +1071,7 @@ bool nsAttrValue::Equals(const nsAString& aValue,
nsCaseTreatment aCaseSensitive) const {
switch (BaseType()) {
case eStringBase: {
if (auto* str = static_cast<nsStringBuffer*>(GetPtr())) {
if (auto* str = static_cast<mozilla::StringBuffer*>(GetPtr())) {
nsDependentString dep(static_cast<char16_t*>(str->Data()),
str->StorageSize() / sizeof(char16_t) - 1);
return aCaseSensitive == eCaseMatters
Expand Down Expand Up @@ -1117,7 +1117,7 @@ bool nsAttrValue::Equals(const nsAtom* aValue,
nsDependentAtomString(atom), nsDependentAtomString(aValue));
}
case eStringBase: {
if (auto* str = static_cast<nsStringBuffer*>(GetPtr())) {
if (auto* str = static_cast<mozilla::StringBuffer*>(GetPtr())) {
size_t strLen = str->StorageSize() / sizeof(char16_t) - 1;
if (aValue->GetLength() != strLen) {
return false;
Expand Down Expand Up @@ -1202,7 +1202,7 @@ bool nsAttrValue::SubstringCheck(const nsAString& aValue,
nsCaseTreatment aCaseSensitive) const {
switch (BaseType()) {
case eStringBase: {
if (auto* str = static_cast<nsStringBuffer*>(GetPtr())) {
if (auto* str = static_cast<mozilla::StringBuffer*>(GetPtr())) {
return F::Check(static_cast<char16_t*>(str->Data()),
str->StorageSize() / sizeof(char16_t) - 1, aValue,
aCaseSensitive);
Expand Down Expand Up @@ -1518,9 +1518,9 @@ nsAtom* nsAttrValue::GetStoredAtom() const {
return nullptr;
}

nsStringBuffer* nsAttrValue::GetStoredStringBuffer() const {
mozilla::StringBuffer* nsAttrValue::GetStoredStringBuffer() const {
if (BaseType() == eStringBase) {
return static_cast<nsStringBuffer*>(GetPtr());
return static_cast<mozilla::StringBuffer*>(GetPtr());
}
if (BaseType() == eOtherBase) {
return GetMiscContainer()->GetStoredStringBuffer();
Expand Down Expand Up @@ -1829,7 +1829,7 @@ bool nsAttrValue::ParsePositiveIntValue(const nsAString& aString) {
}

void nsAttrValue::SetColorValue(nscolor aColor, const nsAString& aString) {
nsStringBuffer* buf = GetStringBuffer(aString).take();
mozilla::StringBuffer* buf = GetStringBuffer(aString).take();
if (!buf) {
return;
}
Expand Down Expand Up @@ -1988,7 +1988,7 @@ void nsAttrValue::SetMiscAtomOrString(const nsAString* aValue) {
atom->Release();
}
} else {
nsStringBuffer* buffer = GetStringBuffer(*aValue).take();
mozilla::StringBuffer* buffer = GetStringBuffer(*aValue).take();
NS_ENSURE_TRUE_VOID(buffer);
uintptr_t bits = reinterpret_cast<uintptr_t>(buffer) | eStringBase;

Expand All @@ -2009,7 +2009,7 @@ void nsAttrValue::ResetMiscAtomOrString() {
bool isString;
if (void* ptr = cont->GetStringOrAtomPtr(isString)) {
if (isString) {
static_cast<nsStringBuffer*>(ptr)->Release();
static_cast<mozilla::StringBuffer*>(ptr)->Release();
} else {
static_cast<nsAtom*>(ptr)->Release();
}
Expand Down Expand Up @@ -2094,27 +2094,28 @@ MiscContainer* nsAttrValue::EnsureEmptyMiscContainer() {
return cont;
}

already_AddRefed<nsStringBuffer> nsAttrValue::GetStringBuffer(
already_AddRefed<mozilla::StringBuffer> nsAttrValue::GetStringBuffer(
const nsAString& aValue) const {
uint32_t len = aValue.Length();
if (!len) {
return nullptr;
}
if (nsStringBuffer* buf = aValue.GetStringBuffer();
if (mozilla::StringBuffer* buf = aValue.GetStringBuffer();
buf && (buf->StorageSize() / sizeof(char16_t) - 1) == len) {
// We can only reuse the buffer if it's exactly sized, since we rely on
// StorageSize() to get the string length in ToString().
return do_AddRef(buf);
}
return nsStringBuffer::Create(aValue.Data(), aValue.Length());
return mozilla::StringBuffer::Create(aValue.Data(), aValue.Length());
}

size_t nsAttrValue::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
size_t n = 0;

switch (BaseType()) {
case eStringBase: {
nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr());
mozilla::StringBuffer* str =
static_cast<mozilla::StringBuffer*>(GetPtr());
n += str ? str->SizeOfIncludingThisIfUnshared(aMallocSizeOf) : 0;
break;
}
Expand All @@ -2134,7 +2135,7 @@ size_t nsAttrValue::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {

// We only count the size of the object pointed by otherPtr if it's a
// string. When it's an atom, it's counted separatly.
if (nsStringBuffer* buf = container->GetStoredStringBuffer()) {
if (mozilla::StringBuffer* buf = container->GetStoredStringBuffer()) {
n += buf->SizeOfIncludingThisIfUnshared(aMallocSizeOf);
}

Expand Down
20 changes: 10 additions & 10 deletions dom/base/nsAttrValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include "nscore.h"
#include "nsString.h"
#include "nsStringBuffer.h"
#include "mozilla/StringBuffer.h"
#include "nsColor.h"
#include "nsCaseTreatment.h"
#include "nsMargin.h"
Expand Down Expand Up @@ -95,19 +95,19 @@ const uintptr_t NS_ATTRVALUE_BASETYPE_MASK = 3;
~NS_ATTRVALUE_ENUMTABLE_VALUE_NEEDS_TO_UPPER)))

/**
* A class used to construct a nsString from a nsStringBuffer (we might
* A class used to construct a nsString from a mozilla::StringBuffer (we might
* want to move this to nsString at some point).
*
* WARNING: Note that nsCheapString doesn't take an explicit length -- it
* assumes the string is maximally large, given the nsStringBuffer's storage
* size. This means the given string buffer *must* be sized exactly correctly
* for the string it contains (including one byte for a null terminator). If
* it has any unused storage space, then that will result in bogus characters
* at the end of our nsCheapString.
* assumes the string is maximally large, given the mozilla::StringBuffer's
* storage size. This means the given string buffer *must* be sized exactly
* correctly for the string it contains (including one byte for a null
* terminator). If it has any unused storage space, then that will result in
* bogus characters at the end of our nsCheapString.
*/
class nsCheapString : public nsString {
public:
explicit nsCheapString(nsStringBuffer* aBuf) {
explicit nsCheapString(mozilla::StringBuffer* aBuf) {
if (aBuf) {
Assign(aBuf, aBuf->StorageSize() / sizeof(char16_t) - 1);
}
Expand Down Expand Up @@ -486,7 +486,7 @@ class nsAttrValue {
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;

nsAtom* GetStoredAtom() const;
nsStringBuffer* GetStoredStringBuffer() const;
mozilla::StringBuffer* GetStoredStringBuffer() const;

private:
// These have to be the same as in ValueType
Expand Down Expand Up @@ -532,7 +532,7 @@ class nsAttrValue {
// Like ClearMiscContainer, except allocates a new container if one does not
// exist already.
MiscContainer* EnsureEmptyMiscContainer();
already_AddRefed<nsStringBuffer> GetStringBuffer(
already_AddRefed<mozilla::StringBuffer> GetStringBuffer(
const nsAString& aValue) const;
// Given an enum table and a particular entry in that table, return
// the actual integer value we should store.
Expand Down
14 changes: 6 additions & 8 deletions dom/base/nsAttrValueInlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ struct MiscContainer final {
using ValueType = nsAttrValue::ValueType;

ValueType mType;
// mStringBits points to either nsAtom* or nsStringBuffer* and is used when
// mType isn't eCSSDeclaration.
// Note eStringBase and eAtomBase is used also to handle the type of
// mStringBits.
// mStringBits points to either nsAtom* or mozilla::StringBuffer* and is used
// when mType isn't eCSSDeclaration. Note eStringBase and eAtomBase is used
// also to handle the type of mStringBits.
//
// Note that we use an atomic here so that we can use Compare-And-Swap
// to cache the serialization during the parallel servo traversal. This case
Expand Down Expand Up @@ -102,10 +101,10 @@ struct MiscContainer final {
return isString ? nullptr : static_cast<nsAtom*>(ptr);
}

nsStringBuffer* GetStoredStringBuffer() const {
mozilla::StringBuffer* GetStoredStringBuffer() const {
bool isString = false;
void* ptr = GetStringOrAtomPtr(isString);
return isString ? static_cast<nsStringBuffer*>(ptr) : nullptr;
return isString ? static_cast<mozilla::StringBuffer*>(ptr) : nullptr;
}

void SetStringBitsMainThread(uintptr_t aBits) {
Expand Down Expand Up @@ -247,8 +246,7 @@ inline nsAtom* nsAttrValue::GetAtomValue() const {
inline void nsAttrValue::ToString(mozilla::dom::DOMString& aResult) const {
switch (Type()) {
case eString: {
nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr());
if (str) {
if (auto* str = static_cast<mozilla::StringBuffer*>(GetPtr())) {
aResult.SetKnownLiveStringBuffer(
str, str->StorageSize() / sizeof(char16_t) - 1);
}
Expand Down
1 change: 0 additions & 1 deletion dom/base/nsContentUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@
#include "nsServiceManagerUtils.h"
#include "nsStreamUtils.h"
#include "nsString.h"
#include "nsStringBuffer.h"
#include "nsStringBundle.h"
#include "nsStringFlags.h"
#include "nsStringFwd.h"
Expand Down
1 change: 1 addition & 0 deletions dom/base/nsContentUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class HTMLEditor;
class LazyLogModule;
class LogModule;
class PresShell;
class StringBuffer;
class TextEditor;
class WidgetDragEvent;
class WidgetKeyboardEvent;
Expand Down
18 changes: 9 additions & 9 deletions dom/base/nsTextFragment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ nsTextFragment& nsTextFragment::operator=(const nsTextFragment& aOther) {
memcpy(const_cast<char*>(m1b), aOther.m1b, aOther.mState.mLength);
} else {
// allocate a buffer for a single REPLACEMENT CHARACTER
m2b = nsStringBuffer::Alloc(sizeof(char16_t) * 2).take();
m2b = StringBuffer::Alloc(sizeof(char16_t) * 2).take();
if (!m2b) {
MOZ_CRASH("OOM!");
}
Expand Down Expand Up @@ -202,7 +202,7 @@ bool nsTextFragment::SetTo(const char16_t* aBuffer, uint32_t aLength,
uint32_t neededSize = aLength * sizeof(char16_t);
if (!neededSize) {
if (storageSize < AutoStringDefaultStorageSize) {
// If we're storing small enough nsStringBuffer, let's preserve it.
// If we're storing small enough StringBuffer, let's preserve it.

static_cast<char16_t*>(m2b->Data())[0] = char16_t(0);
mState.mLength = 0;
Expand All @@ -212,7 +212,7 @@ bool nsTextFragment::SetTo(const char16_t* aBuffer, uint32_t aLength,
} else if ((neededSize < storageSize) &&
((storageSize / 2) <
(neededSize + AutoStringDefaultStorageSize))) {
// Don't try to reuse the existing nsStringBuffer, if it would have
// Don't try to reuse the existing StringBuffer, if it would have
// lots of unused space.

memcpy(m2b->Data(), aBuffer, neededSize);
Expand Down Expand Up @@ -296,7 +296,7 @@ bool nsTextFragment::SetTo(const char16_t* aBuffer, uint32_t aLength,
return false;
}

m2b = nsStringBuffer::Alloc(m2bSize.value()).take();
m2b = StringBuffer::Alloc(m2bSize.value()).take();
if (!m2b) {
return false;
}
Expand Down Expand Up @@ -373,18 +373,18 @@ bool nsTextFragment::Append(const char16_t* aBuffer, uint32_t aLength,
size *= sizeof(char16_t);

// Already a 2-byte string so the result will be too
nsStringBuffer* buff = nullptr;
nsStringBuffer* bufferToRelease = nullptr;
StringBuffer* buff = nullptr;
StringBuffer* bufferToRelease = nullptr;
if (m2b->IsReadonly()) {
buff = nsStringBuffer::Alloc(size).take();
buff = StringBuffer::Alloc(size).take();
if (!buff) {
return false;
}
bufferToRelease = m2b;
memcpy(static_cast<char16_t*>(buff->Data()), m2b->Data(),
mState.mLength * sizeof(char16_t));
} else {
buff = nsStringBuffer::Realloc(m2b, size);
buff = StringBuffer::Realloc(m2b, size);
if (!buff) {
return false;
}
Expand Down Expand Up @@ -418,7 +418,7 @@ bool nsTextFragment::Append(const char16_t* aBuffer, uint32_t aLength,

// The old data was 1-byte, but the new is not so we have to expand it
// all to 2-byte
nsStringBuffer* buff = nsStringBuffer::Alloc(size).take();
StringBuffer* buff = StringBuffer::Alloc(size).take();
if (!buff) {
return false;
}
Expand Down
Loading

0 comments on commit d55796b

Please sign in to comment.