Files
palemoon27/xpcom/string/nsSubstring.cpp
T
roytam1 f5a7b27f3d import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1211204 - Remove possible false-negative with D3D9 texture memory reporting. r=mattwoodrow (74adbcad19)
- Bug 1200595 - D3D9 TextureData implementation. r=Bas (2624bd59a3)
- Bug 1200595 - MacIOSurface TextureData implementation. r=mattwoodrow (98130b5d6b)
- Bug 1200595 - EGLImage TextureData implementation. r=mattwoodrow (8b732af2da)
- Bug 1200595 - AndroidSurface TextureData implementation. r=mattwoodrow (79443ff5d7)
- Bug 1200595 - SharedSurface TextureData implementation. r=jgilbert (aadc8c5601)
- Bug 1200595 - DIB TextureData implementation. r=Bas (d7bae178ad)
- Bug 1200595 - Merge TextureClient and ClientTexture back into TextureClient. r=mattwoodrow (890d76ff9b)
- Bug 1200595 - Consolidate the TextureClient's destruction logic. r=mattwoodrow (fa9ac2e414)
- Allow asynchronous D3D11 TextureClients on the main thread. (bug 1217665 part 5, r=nical) (cb77fa88cc)
- Implement the direct bitmap drawing model for plugins. (bug 1217665 part 6, r=mattwoodrow) (82f9015e5a)
- Implement the direct DXGI drawing model for plugins. (bug 1217665 part 7, r=mattwoodrow) (cdeeb52a91)
- Implement the DidComposite NPAPI callback. (bug 1217665 part 8, r=mattwoodrow) (4eb2a790ae)
- Add an NPN_GetValue query to find the browser's DXGI adapter. (bug 1217665 part 10, r=aklotz,mattwoodrow) (84af4e525a)
- Create a D3D11 content device even if D2D is blocked. (bug 1217665 part 11, r=jrmuizel) (142441808e)
- Bug 1229665 - Convert widget clip regions to LayoutDevicePixels. r=botond. (edf746b278)
- Bug 1204715 - Move browser/app/profile/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd} to moz.build. r=mshal (8c6b4709bc)
- Bug 1212773 - Pass a base directory to jar maker, instead of a chrome directory. r=gps (1f644706c6)
- Bustage fix for bug 1210687 on a CLOSED TREE. r=me (4cd7000101)
- Bug 1212773 - Extend jar maker syntax for jar file location. r=gps (8119aa2b98)
- Bug 1164039 - moved TelemetryTimestamps.jsm and tests to toolkit/componets/telemetry/. r=dexter (942145656e)
- Bug 1184705 - Search A/B testing cohort identifier should be recorded in FHR, r=rnewman. (b5cf397cf0)
- Bug 1230047 (part 2) - Make several PaintWindow() functions use LayoutDevice coordinates. r=kats. (246e1321ca)
- Bug 1218454 - part 1 - move DialogValueHolder::Get out-of-line; r=bz (2d65438b37)
- Bug 1218454 - part 2 - don't #include nsContentUtils.h from CallbackObject.h; r=bz (59e3b60749)
- Bug 1205945 part.1 Remove unnecessary member of IMEInputHandler, mLastDispatchedCompositionString r=smichaud (2223df8aec)
- Bug 1205945 part.2 Add DispatchCompositionStartEvent() and move the code of OnStartIMEComposition() into it r=smichaud (25449c2bca)
- Bug 1205945 part.3 Move the code of OnUpdateIMEComposition() into DispatchCompositionChangeEvent() r=smichaud (e85a65f2f7)
- Bug 1205945 part.4 Move the code of OnEndIMEComposition() into DispatchCompositionCommitEvent() r=smichaud (21e6218c06)
- Bug 1205945 part.5 Emulate mSelectedRange at dispatching compositionchange or compositioncommit event until OnSelectionChange() is called r=smichaud (f82baaea8e)
- Bug 1205945 part.6 IMEInputHandler::GetAttributedSubstringFromRange() should return stored composition string if the range is in the composition string r=smichaud (53eabe028b)
- Bug 1205399 - Follow-up to fix build bustage for platforms without OS X 10.10 and -Wswitch enabled. r=mstange (c2e8eda9b5)
- Bug 1220337 - Don't show alternate notification actions on OS X 10.8. r=MattN (5ef8449dcc)
- Bug 1224738 - Fix alternate action index getter name on OS X. r=MattN (a6f83ce706)
- Bug 1225908 - AsmSimdTypeToLaneType. r=bbouvier (f850c10bff)
- Bug 1224389 - Odin: refactor types/signatures/values (r=bbouvier) (31bc615781)
- Bug 1224389 - Odin: simplify AsmJSModule global data allocation (r=bbouvier) (3ba518183a)
- Bug 1222684 - IonMonkey: MIPS: Implement callWithPatch and patchCall. r=luke (0ede7a61c3)
- Bug 1224814 - "TraceLogger: Remove redundant checks in BaselineJIT". r=hv1989 (b3865fbdf0)
- Bug 1228369: Rename CodeOffsetLabel into CodeOffset; r=luke (1d0aba3710)
- Bug 1224389 - Odin: refactor stubs, func-ptrs, and masm use (r=bbouvier) (2834725644)
- Bug 1228340: Get rid of the js_ prefix for CodeSpec, CodeName, NumCodeSpecs; r=jorendorff (8ff2b3bcd5)
- Bug 1176214 - Part 2: Preliminary adjustments. r=waldo (f9e2adca8a)
- Bug 1176214 - Part 3: VM core changes. r=waldo (cf5b24fe18)
- Bug 1176214 - Part 4: VM built-in lib changes. r=waldo (603335d2aa)
- Bug 1176214 - Part 5: Ion changes. r=h4writer (b589713e1c)
- Bug 1176214 - Part 6: Odin changes. r=luke (f1dcb025d2)
- Bug 1176214 - Part 7: Ctypes, shell, xpconnect, etc. r=waldo (bdc78e0558)
- Bug 1199578 - test case. r=waldo (6a94fd455a)
- Bug 1211409 - load/store exclusive for ARM-32. r=jolesen (38efc4882e)
- Bug 1205390 - guard against asm.js compilation not being available. r=me (aeefe98cfd)
- Make test runnable on non-Nightly (no bug) r=me (27930210ed)
- Bug 1176214 - Part 8: jit-test changes. r=bbouvier (31c4f42eea)
- Bug 1176214 - Part 9: tests changes. r=bbouvier (a245687f43)
- Bug 1176214 - Part 10: jsapi-tests changes. r=bbouvier (610e0002a2)
- bug 1198656 remove unnecessary reinterpret_casts r=padenot (bc5a67d521)
- bug 1198656 refactor acquiring the content into an object method r=padenot (b7b062fbf1)
- bug 1198656 clear references in mJSChannels on successful content acquire r=padenot (65e5ee1856)
- bug 1198656 delay AudioBuffer allocation until required r=padenot (57d0fd0d01)
- bug 1199559 remove now unused SetRawChannelContents r=padenot (40685ef783)
- Bug 1203616 - Properly scale the input buffer of a WaveShaperNode before processing it with the curve. r=karlt (12e9592a9e)
- Bug 1186343: Throw an InvalidStateError when we set the curve attribute of a WaveShaperNode with a Float32Array of length less than 2; r=padenot,smaug (df83b21fd0)
- bug 1188244 throw in SetCurve() on OOM r=padenot (17967b7b58)
- Bug 1176214 - Part 11: Changes to DOM, except for WebGL. r=bz, r=clb (c40e5c2a68)
- Bug 1176214 - Part 12: Changes to WebGL. r=bz, r=clb (228c90da3c)
- Bug 1176214 - Part 13: Changes to ipc. r=mrbkap (4b11d4e509)
- Bug 1176214 - Part 14: Changes to netwerk. r=jduell (91568c8444)
- Bug 1176214 - Part 15: Changes to xpcom. r=nfroyd (df8d080070)
- Revert "Bug 1176214 - Part 14: Changes to netwerk. r=jduell" (d0aedbac94)
- improved backport of PM because of newer JS_GetArrayBufferData (9d8188ff5e)
- bug 1199559 write decodeAudioData buffer in a format suitable for direct use by AudioBuffer r=padenot (da00bab1a1)
- bug 1225003 null-check mBuffer in SizeOfExcludingThis() r=padenot (c01d389f10)
- Bug 1225365 - Fix assertion in the nsScriptNameSpaceManager memory reporter. r=bz. (a5605a8923)
- Bug 1229458 - Remove SizeOfIncludingThisMustBeUnshared() from string classes. r=mccr8. (26abcea276)
- Bug 1214506. Ensure OggReader sets proper IDs for its tracks. r=jya (a270b02301)
- bits of Bug 1188812 - Obtain CDM can render capability and store into MediaInfo (321388180e)
- Bug 1226450 - Report audio/video codecs used in HTMLMediaElement and WebAudio via telemetry. r=jya (e722b409b7)
2023-04-11 11:15:41 +08:00

352 lines
8.9 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#ifdef DEBUG
#define ENABLE_STRING_STATS
#endif
#include "mozilla/Atomics.h"
#include "mozilla/MemoryReporting.h"
#ifdef ENABLE_STRING_STATS
#include <stdio.h>
#endif
#include <stdlib.h>
#include "nsSubstring.h"
#include "nsString.h"
#include "nsStringBuffer.h"
#include "nsDependentString.h"
#include "nsMemory.h"
#include "prprf.h"
#include "nsStaticAtom.h"
#include "nsCOMPtr.h"
#include "mozilla/IntegerPrintfMacros.h"
#ifdef XP_WIN
#include <windows.h>
#include <process.h>
#define getpid() _getpid()
#define pthread_self() GetCurrentThreadId()
#else
#include <pthread.h>
#include <unistd.h>
#endif
using mozilla::Atomic;
// ---------------------------------------------------------------------------
static const char16_t gNullChar = 0;
char* const nsCharTraits<char>::sEmptyBuffer =
(char*)const_cast<char16_t*>(&gNullChar);
char16_t* const nsCharTraits<char16_t>::sEmptyBuffer =
const_cast<char16_t*>(&gNullChar);
// ---------------------------------------------------------------------------
#ifdef ENABLE_STRING_STATS
class nsStringStats
{
public:
nsStringStats()
: mAllocCount(0)
, mReallocCount(0)
, mFreeCount(0)
, mShareCount(0)
{
}
~nsStringStats()
{
// this is a hack to suppress duplicate string stats printing
// in seamonkey as a result of the string code being linked
// into seamonkey and libxpcom! :-(
if (!mAllocCount && !mAdoptCount) {
return;
}
printf("nsStringStats\n");
printf(" => mAllocCount: % 10d\n", int(mAllocCount));
printf(" => mReallocCount: % 10d\n", int(mReallocCount));
printf(" => mFreeCount: % 10d", int(mFreeCount));
if (mAllocCount > mFreeCount) {
printf(" -- LEAKED %d !!!\n", mAllocCount - mFreeCount);
} else {
printf("\n");
}
printf(" => mShareCount: % 10d\n", int(mShareCount));
printf(" => mAdoptCount: % 10d\n", int(mAdoptCount));
printf(" => mAdoptFreeCount: % 10d", int(mAdoptFreeCount));
if (mAdoptCount > mAdoptFreeCount) {
printf(" -- LEAKED %d !!!\n", mAdoptCount - mAdoptFreeCount);
} else {
printf("\n");
}
printf(" => Process ID: %" PRIuPTR ", Thread ID: %" PRIuPTR "\n",
uintptr_t(getpid()), uintptr_t(pthread_self()));
}
Atomic<int32_t> mAllocCount;
Atomic<int32_t> mReallocCount;
Atomic<int32_t> mFreeCount;
Atomic<int32_t> mShareCount;
Atomic<int32_t> mAdoptCount;
Atomic<int32_t> mAdoptFreeCount;
};
static nsStringStats gStringStats;
#define STRING_STAT_INCREMENT(_s) (gStringStats.m ## _s ## Count)++
#else
#define STRING_STAT_INCREMENT(_s)
#endif
// ---------------------------------------------------------------------------
void
ReleaseData(void* aData, uint32_t aFlags)
{
if (aFlags & nsSubstring::F_SHARED) {
nsStringBuffer::FromData(aData)->Release();
} else if (aFlags & nsSubstring::F_OWNED) {
moz_free(aData);
STRING_STAT_INCREMENT(AdoptFree);
// Treat this as destruction of a "StringAdopt" object for leak
// tracking purposes.
MOZ_LOG_DTOR(aData, "StringAdopt", 1);
}
// otherwise, nothing to do.
}
// ---------------------------------------------------------------------------
// XXX or we could make nsStringBuffer be a friend of nsTAString
class nsAStringAccessor : public nsAString
{
private:
nsAStringAccessor(); // NOT IMPLEMENTED
public:
char_type* data() const
{
return mData;
}
size_type length() const
{
return mLength;
}
uint32_t flags() const
{
return mFlags;
}
void set(char_type* aData, size_type aLen, uint32_t aFlags)
{
ReleaseData(mData, mFlags);
mData = aData;
mLength = aLen;
mFlags = aFlags;
}
};
class nsACStringAccessor : public nsACString
{
private:
nsACStringAccessor(); // NOT IMPLEMENTED
public:
char_type* data() const
{
return mData;
}
size_type length() const
{
return mLength;
}
uint32_t flags() const
{
return mFlags;
}
void set(char_type* aData, size_type aLen, uint32_t aFlags)
{
ReleaseData(mData, mFlags);
mData = aData;
mLength = aLen;
mFlags = aFlags;
}
};
// ---------------------------------------------------------------------------
void
nsStringBuffer::AddRef()
{
++mRefCount;
STRING_STAT_INCREMENT(Share);
NS_LOG_ADDREF(this, mRefCount, "nsStringBuffer", sizeof(*this));
}
void
nsStringBuffer::Release()
{
int32_t count = --mRefCount;
NS_LOG_RELEASE(this, count, "nsStringBuffer");
if (count == 0) {
STRING_STAT_INCREMENT(Free);
moz_free(this); // we were allocated with |malloc|
}
}
/**
* Alloc returns a pointer to a new string header with set capacity.
*/
already_AddRefed<nsStringBuffer>
nsStringBuffer::Alloc(size_t aSize)
{
NS_ASSERTION(aSize != 0, "zero capacity allocation not allowed");
NS_ASSERTION(sizeof(nsStringBuffer) + aSize <= size_t(uint32_t(-1)) &&
sizeof(nsStringBuffer) + aSize > aSize,
"mStorageSize will truncate");
nsStringBuffer* hdr =
(nsStringBuffer*)moz_xmalloc(sizeof(nsStringBuffer) + aSize);
if (hdr) {
STRING_STAT_INCREMENT(Alloc);
hdr->mRefCount = 1;
hdr->mStorageSize = aSize;
NS_LOG_ADDREF(hdr, 1, "nsStringBuffer", sizeof(*hdr));
}
return dont_AddRef(hdr);
}
nsStringBuffer*
nsStringBuffer::Realloc(nsStringBuffer* aHdr, size_t aSize)
{
STRING_STAT_INCREMENT(Realloc);
NS_ASSERTION(aSize != 0, "zero capacity allocation not allowed");
NS_ASSERTION(sizeof(nsStringBuffer) + aSize <= size_t(uint32_t(-1)) &&
sizeof(nsStringBuffer) + aSize > aSize,
"mStorageSize will truncate");
// no point in trying to save ourselves if we hit this assertion
NS_ASSERTION(!aHdr->IsReadonly(), "|Realloc| attempted on readonly string");
// Treat this as a release and addref for refcounting purposes, since we
// just asserted that the refcount is 1. If we don't do that, refcount
// logging will claim we've leaked all sorts of stuff.
NS_LOG_RELEASE(aHdr, 0, "nsStringBuffer");
aHdr = (nsStringBuffer*)moz_xrealloc(aHdr, sizeof(nsStringBuffer) + aSize);
if (aHdr) {
NS_LOG_ADDREF(aHdr, 1, "nsStringBuffer", sizeof(*aHdr));
aHdr->mStorageSize = aSize;
}
return aHdr;
}
nsStringBuffer*
nsStringBuffer::FromString(const nsAString& aStr)
{
const nsAStringAccessor* accessor =
static_cast<const nsAStringAccessor*>(&aStr);
if (!(accessor->flags() & nsSubstring::F_SHARED)) {
return nullptr;
}
return FromData(accessor->data());
}
nsStringBuffer*
nsStringBuffer::FromString(const nsACString& aStr)
{
const nsACStringAccessor* accessor =
static_cast<const nsACStringAccessor*>(&aStr);
if (!(accessor->flags() & nsCSubstring::F_SHARED)) {
return nullptr;
}
return FromData(accessor->data());
}
void
nsStringBuffer::ToString(uint32_t aLen, nsAString& aStr,
bool aMoveOwnership)
{
char16_t* data = static_cast<char16_t*>(Data());
nsAStringAccessor* accessor = static_cast<nsAStringAccessor*>(&aStr);
NS_ASSERTION(data[aLen] == char16_t(0), "data should be null terminated");
// preserve class flags
uint32_t flags = accessor->flags();
flags = (flags & 0xFFFF0000) | nsSubstring::F_SHARED | nsSubstring::F_TERMINATED;
if (!aMoveOwnership) {
AddRef();
}
accessor->set(data, aLen, flags);
}
void
nsStringBuffer::ToString(uint32_t aLen, nsACString& aStr,
bool aMoveOwnership)
{
char* data = static_cast<char*>(Data());
nsACStringAccessor* accessor = static_cast<nsACStringAccessor*>(&aStr);
NS_ASSERTION(data[aLen] == char(0), "data should be null terminated");
// preserve class flags
uint32_t flags = accessor->flags();
flags = (flags & 0xFFFF0000) | nsCSubstring::F_SHARED | nsCSubstring::F_TERMINATED;
if (!aMoveOwnership) {
AddRef();
}
accessor->set(data, aLen, flags);
}
size_t
nsStringBuffer::SizeOfIncludingThisIfUnshared(mozilla::MallocSizeOf aMallocSizeOf) const
{
return IsReadonly() ? 0 : aMallocSizeOf(this);
}
size_t
nsStringBuffer::SizeOfIncludingThisEvenIfShared(mozilla::MallocSizeOf aMallocSizeOf) const
{
return aMallocSizeOf(this);
}
// ---------------------------------------------------------------------------
// define nsSubstring
#include "string-template-def-unichar.h"
#include "nsTSubstring.cpp"
#include "string-template-undef.h"
// define nsCSubstring
#include "string-template-def-char.h"
#include "nsTSubstring.cpp"
#include "string-template-undef.h"
// Check that internal and external strings have the same size.
// See https://bugzilla.mozilla.org/show_bug.cgi?id=430581
#include "mozilla/Logging.h"
#include "nsXPCOMStrings.h"
static_assert(sizeof(nsStringContainer_base) == sizeof(nsSubstring),
"internal and external strings must have the same size");