Bug 1351732 - Part 1: Add an ArenaAllocator strdup extension. r=froydnj
authorEric Rahm <erahm@mozilla.com>
Thu, 30 Mar 2017 16:46:56 -0700
changeset 350572 b7e322f24106a81673135d9e1dd839609de35688
parent 350571 f09969186032a73b96488bddc5e218666e97dd8b
child 350573 a2c7254cb8293082c7caa0d9419a71eacf5f85d2
push id88658
push usererahm@mozilla.com
push dateThu, 30 Mar 2017 23:47:04 +0000
treeherdermozilla-inbound@a2c7254cb829 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1351732
milestone55.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 1351732 - Part 1: Add an ArenaAllocator strdup extension. r=froydnj This adds an extension to ArenaAllocator that provides strdup-like functionality for various string types. MozReview-Commit-ID: 87SHTs6flHY
xpcom/ds/ArenaAllocatorExtensions.h
xpcom/ds/moz.build
xpcom/tests/gtest/TestArenaAllocator.cpp
new file mode 100644
--- /dev/null
+++ b/xpcom/ds/ArenaAllocatorExtensions.h
@@ -0,0 +1,94 @@
+/* -*- 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/. */
+
+#ifndef mozilla_ArenaAllocatorExtensions_h
+#define mozilla_ArenaAllocatorExtensions_h
+
+#include "mozilla/ArenaAllocator.h"
+#include "mozilla/CheckedInt.h"
+#include "nsAString.h"
+
+/**
+ * Extensions to the ArenaAllocator class.
+ */
+namespace mozilla {
+
+namespace detail {
+
+template<typename T, size_t ArenaSize, size_t Alignment>
+T* DuplicateString(const T* aSrc, const CheckedInt<size_t>& aLen,
+                   ArenaAllocator<ArenaSize, Alignment>& aArena);
+
+} // namespace detail
+
+/**
+ * Makes an arena allocated null-terminated copy of the source string. The
+ * source string must be null-terminated.
+ *
+ * @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>
+char* ArenaStrdup(const char* aStr,
+                  ArenaAllocator<ArenaSize, Alignment>& aArena)
+{
+  return detail::DuplicateString(aStr, strlen(aStr), 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>
+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)
+{
+  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.
+ */
+template<typename T, size_t ArenaSize, size_t Alignment>
+T* detail::DuplicateString(const T* aSrc, const CheckedInt<size_t>& aLen,
+                           ArenaAllocator<ArenaSize, Alignment>& aArena)
+{
+  const auto byteLen = (aLen + 1) * sizeof(T);
+  if (!byteLen.isValid()) {
+    return nullptr;
+  }
+
+  T* p = static_cast<T*>(aArena.Allocate(byteLen.value(), mozilla::fallible));
+  if (p) {
+    memcpy(p, aSrc, byteLen.value() - sizeof(T));
+    p[aLen.value()] = T(0);
+  }
+
+  return p;
+}
+
+} // namespace mozilla
+
+#endif // mozilla_ArenaAllocatorExtensions_h
--- a/xpcom/ds/moz.build
+++ b/xpcom/ds/moz.build
@@ -75,16 +75,17 @@ EXPORTS += [
     'nsTPriorityQueue.h',
     'nsVariant.h',
     'nsWhitespaceTokenizer.h',
     'PLDHashTable.h',
 ]
 
 EXPORTS.mozilla += [
     'ArenaAllocator.h',
+    'ArenaAllocatorExtensions.h',
     'ArrayIterator.h',
     'IncrementalTokenizer.h',
     'Observer.h',
     'StickyTimeDuration.h',
     'Tokenizer.h',
 ]
 
 UNIFIED_SOURCES += [
--- a/xpcom/tests/gtest/TestArenaAllocator.cpp
+++ b/xpcom/tests/gtest/TestArenaAllocator.cpp
@@ -1,15 +1,16 @@
 /* -*- 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/. */
 
 #include "mozilla/ArenaAllocator.h"
+#include "mozilla/ArenaAllocatorExtensions.h"
 #include "nsIMemoryReporter.h" // MOZ_MALLOC_SIZE_OF
 
 #include "gtest/gtest.h"
 
 using mozilla::ArenaAllocator;
 
 TEST(ArenaAllocator, Constructor)
 {
@@ -272,8 +273,24 @@ TEST(ArenaAllocator, Clear)
   // Allocating should work after clearing an arena with allocations.
   x = a.Allocate(10);
   EXPECT_TRUE(x);
 
   prev_sz = sz;
   sz = a.SizeOfExcludingThis(TestSizeOf);
   EXPECT_GT(sz, prev_sz);
 }
+
+TEST(ArenaAllocator, Extensions)
+{
+  ArenaAllocator<4096, 8> a;
+  const char* const kTestStr = "This is a test string.";
+  char* dup = mozilla::ArenaStrdup(kTestStr, a);
+  EXPECT_STREQ(dup, kTestStr);
+
+  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));
+}