Skip to content

Commit

Permalink
Bug 1386103 (part 1, attempt 3) - Specify nsAuto[C]String storage siz…
Browse files Browse the repository at this point in the history
…e via template parameter. r=dbaron.
  • Loading branch information
nnethercote committed Aug 9, 2017
1 parent 1029a41 commit 57ccb9e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 29 deletions.
2 changes: 1 addition & 1 deletion dom/indexedDB/ProfilerHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class MOZ_STACK_CLASS LoggingIdString final
LoggingIdString(const nsID& aID)
{
static_assert(NSID_LENGTH > 1, "NSID_LENGTH is set incorrectly!");
static_assert(NSID_LENGTH <= kDefaultStorageSize,
static_assert(NSID_LENGTH <= kStorageSize,
"nID string won't fit in our storage!");
MOZ_ASSERT(Capacity() > NSID_LENGTH);

Expand Down
1 change: 0 additions & 1 deletion security/manager/ssl/nsNSSCertificate.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

namespace mozilla { namespace pkix { class DERArray; } }

class nsAutoString;
class nsINSSComponent;
class nsIASN1Sequence;

Expand Down
4 changes: 2 additions & 2 deletions storage/mozStorageSQLFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ levenshteinDistance(const nsAString &aStringS,
// we be able to see the current row and the previous one.

// Allocate memory for two rows.
AutoTArray<int, nsAutoString::kDefaultStorageSize> row1;
AutoTArray<int, nsAutoString::kDefaultStorageSize> row2;
AutoTArray<int, nsAutoString::kStorageSize> row1;
AutoTArray<int, nsAutoString::kStorageSize> row2;

// Declare the raw pointers that will actually be used to access the memory.
int *prevRow = row1.AppendElements(sLen + 1);
Expand Down
8 changes: 6 additions & 2 deletions xpcom/string/nsStringFwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ class nsCStringRepr;
} // namespace detail
} // namespace mozilla

static const size_t AutoStringDefaultStorageSize = 64;

// Double-byte (char16_t) string types.
class nsAString;
class nsSubstringTuple;
class nsString;
class nsAutoString;
template<size_t N> class nsAutoStringN;
using nsAutoString = nsAutoStringN<AutoStringDefaultStorageSize>;
class nsDependentString;
class nsDependentSubstring;
class nsPromiseFlatString;
Expand All @@ -39,7 +42,8 @@ class nsDefaultStringComparator;
class nsACString;
class nsCSubstringTuple;
class nsCString;
class nsAutoCString;
template<size_t N> class nsAutoCStringN;
using nsAutoCString = nsAutoCStringN<AutoStringDefaultStorageSize>;
class nsDependentCString;
class nsDependentCSubstring;
class nsPromiseFlatCString;
Expand Down
48 changes: 25 additions & 23 deletions xpcom/string/nsTString.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,63 +560,65 @@ class nsTFixedString_CharT : public nsTString_CharT
* it contains is significantly smaller or any larger than 64 characters.
*
* NAMES:
* nsAutoString for wide characters
* nsAutoCString for narrow characters
* nsAutoStringN / nsAutoString for wide characters
* nsAutoCStringN / nsAutoCString for narrow characters
*/
class MOZ_NON_MEMMOVABLE nsTAutoString_CharT : public nsTFixedString_CharT
template<size_t N>
class MOZ_NON_MEMMOVABLE nsTAutoStringN_CharT : public nsTFixedString_CharT
{
public:

typedef nsTAutoString_CharT self_type;
typedef nsTAutoStringN_CharT<N> self_type;

public:

/**
* constructors
*/

nsTAutoString_CharT()
: fixed_string_type(mStorage, kDefaultStorageSize, 0)
nsTAutoStringN_CharT()
: fixed_string_type(mStorage, N, 0)
{
}

explicit
nsTAutoString_CharT(char_type aChar)
: fixed_string_type(mStorage, kDefaultStorageSize, 0)
nsTAutoStringN_CharT(char_type aChar)
: fixed_string_type(mStorage, N, 0)
{
Assign(aChar);
}

explicit
nsTAutoString_CharT(const char_type* aData, size_type aLength = size_type(-1))
: fixed_string_type(mStorage, kDefaultStorageSize, 0)
nsTAutoStringN_CharT(const char_type* aData,
size_type aLength = size_type(-1))
: fixed_string_type(mStorage, N, 0)
{
Assign(aData, aLength);
}

#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
explicit
nsTAutoString_CharT(char16ptr_t aData, size_type aLength = size_type(-1))
: nsTAutoString_CharT(static_cast<const char16_t*>(aData), aLength)
nsTAutoStringN_CharT(char16ptr_t aData, size_type aLength = size_type(-1))
: nsTAutoStringN_CharT(static_cast<const char16_t*>(aData), aLength)
{
}
#endif

nsTAutoString_CharT(const self_type& aStr)
: fixed_string_type(mStorage, kDefaultStorageSize, 0)
nsTAutoStringN_CharT(const self_type& aStr)
: fixed_string_type(mStorage, N, 0)
{
Assign(aStr);
}

explicit
nsTAutoString_CharT(const substring_type& aStr)
: fixed_string_type(mStorage, kDefaultStorageSize, 0)
nsTAutoStringN_CharT(const substring_type& aStr)
: fixed_string_type(mStorage, N, 0)
{
Assign(aStr);
}

MOZ_IMPLICIT nsTAutoString_CharT(const substring_tuple_type& aTuple)
: fixed_string_type(mStorage, kDefaultStorageSize, 0)
MOZ_IMPLICIT nsTAutoStringN_CharT(const substring_tuple_type& aTuple)
: fixed_string_type(mStorage, N, 0)
{
Assign(aTuple);
}
Expand Down Expand Up @@ -655,16 +657,16 @@ class MOZ_NON_MEMMOVABLE nsTAutoString_CharT : public nsTFixedString_CharT
return *this;
}

enum
{
kDefaultStorageSize = 64
};
static const size_t kStorageSize = N;

private:

char_type mStorage[kDefaultStorageSize];
char_type mStorage[N];
};

// We define this typedef instead of providing a default value for N so that so
// there is a default typename that doesn't require angle brackets.
using nsTAutoString_CharT = nsTAutoStringN_CharT<AutoStringDefaultStorageSize>;

//
// nsAutoString stores pointers into itself which are invalidated when an
Expand Down
1 change: 1 addition & 0 deletions xpcom/string/string-template-def-char.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define nsTString_CharT nsCString
#define nsTStringRepr_CharT nsCStringRepr
#define nsTFixedString_CharT nsFixedCString
#define nsTAutoStringN_CharT nsAutoCStringN
#define nsTAutoString_CharT nsAutoCString
#define nsTSubstring_CharT nsACString
#define PrintfAppend_CharT PrintfAppend_nsACString
Expand Down
1 change: 1 addition & 0 deletions xpcom/string/string-template-def-unichar.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define nsTString_CharT nsString
#define nsTStringRepr_CharT nsStringRepr
#define nsTFixedString_CharT nsFixedString
#define nsTAutoStringN_CharT nsAutoStringN
#define nsTAutoString_CharT nsAutoString
#define nsTSubstring_CharT nsAString
#define PrintfAppend_CharT PrintfAppend_nsAString
Expand Down
1 change: 1 addition & 0 deletions xpcom/string/string-template-undef.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#undef nsTString_CharT
#undef nsTStringRepr_CharT
#undef nsTFixedString_CharT
#undef nsTAutoStringN_CharT
#undef nsTAutoString_CharT
#undef nsTSubstring_CharT
#undef PrintfAppend_CharT
Expand Down

0 comments on commit 57ccb9e

Please sign in to comment.