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