Bug 1412416 - Part 2: Simplify ArenaStrdup for xpcom strings. r=froydnj
authorEric Rahm <erahm@mozilla.com>
Fri, 27 Oct 2017 13:26:47 -0700
changeset 439997 6bb54c25baf91492fcacdcea8b50256e0e4eb26f
parent 439996 f3aea617d92e1995803f07903f7f8d4d24323bce
child 439998 7b7d6deedf9611764f90b1b0815b407c93189e7d
push id8114
push userjlorenzo@mozilla.com
push dateThu, 02 Nov 2017 16:33:21 +0000
treeherdermozilla-beta@73e0d89a540f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1412416
milestone58.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1412416 - Part 2: Simplify ArenaStrdup for xpcom strings. r=froydnj Now that xpcom strings use templates we can combine their `ArenaStrdup` implementations. `nsTStringRepr` is used so as to allow handling of both `nsTLiteralString` and `nsTSubstring` types.
xpcom/ds/ArenaAllocatorExtensions.h
xpcom/tests/gtest/TestArenaAllocator.cpp
--- a/xpcom/ds/ArenaAllocatorExtensions.h
+++ b/xpcom/ds/ArenaAllocatorExtensions.h
@@ -41,33 +41,19 @@ T* ArenaStrdup(const T* aStr,
 
 /**
  * Makes an arena allocated null-terminated copy of the source string.
  *
  * @param aSrc String to copy.
  * @param aArena The arena to allocate the string copy out of.
  * @return An arena allocated null-terminated string.
  */
-template<size_t ArenaSize, size_t Alignment>
-nsAString::char_type* ArenaStrdup(
-    const nsAString& aStr, ArenaAllocator<ArenaSize, Alignment>& aArena)
-{
-  return detail::DuplicateString(aStr.BeginReading(), aStr.Length(), aArena);
-}
-
-/**
- * Makes an arena allocated null-terminated copy of the source string.
- *
- * @param aSrc String to copy.
- * @param aArena The arena to allocate the string copy out of.
- * @return An arena allocated null-terminated string.
- */
-template<size_t ArenaSize, size_t Alignment>
-nsACString::char_type* ArenaStrdup(
-    const nsACString& aStr, ArenaAllocator<ArenaSize, Alignment>& aArena)
+template<typename T, size_t ArenaSize, size_t Alignment>
+T* ArenaStrdup(const detail::nsTStringRepr<T>& aStr,
+               ArenaAllocator<ArenaSize, Alignment>& aArena)
 {
   return detail::DuplicateString(aStr.BeginReading(), aStr.Length(), aArena);
 }
 
 /**
  * Copies the source string and adds a null terminator. Source string does not
  * have to be null terminated.
  */
--- a/xpcom/tests/gtest/TestArenaAllocator.cpp
+++ b/xpcom/tests/gtest/TestArenaAllocator.cpp
@@ -288,16 +288,27 @@ TEST(ArenaAllocator, Extensions)
   // Test with raw strings.
   const char* const kTestCStr = "This is a test string.";
   char* c_dup = mozilla::ArenaStrdup(kTestCStr, a);
   EXPECT_STREQ(c_dup, kTestCStr);
 
   const char16_t* const kTestStr = u"This is a wide test string.";
   char16_t* dup = mozilla::ArenaStrdup(kTestStr, a);
   EXPECT_TRUE(nsString(dup).Equals(kTestStr));
+
+  // Make sure it works with literal strings.
   NS_NAMED_LITERAL_STRING(wideStr, "A wide string.");
   nsLiteralString::char_type* wide = mozilla::ArenaStrdup(wideStr, a);
   EXPECT_TRUE(wideStr.Equals(wide));
 
   NS_NAMED_LITERAL_CSTRING(cStr, "A c-string.");
   nsLiteralCString::char_type* cstr = mozilla::ArenaStrdup(cStr, a);
   EXPECT_TRUE(cStr.Equals(cstr));
+
+  // Make sure it works with normal strings.
+  nsAutoString x(u"testing wide");
+  nsAutoString::char_type* x_copy = mozilla::ArenaStrdup(x, a);
+  EXPECT_TRUE(x.Equals(x_copy));
+
+  nsAutoCString y("testing c-string");
+  nsAutoCString::char_type* y_copy = mozilla::ArenaStrdup(y, a);
+  EXPECT_TRUE(y.Equals(y_copy));
 }