summaryrefslogtreecommitdiffstats
path: root/xpcom/ds/ArenaAllocatorExtensions.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /xpcom/ds/ArenaAllocatorExtensions.h
parentInitial commit. (diff)
downloadfirefox-upstream/124.0.1.tar.xz
firefox-upstream/124.0.1.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'xpcom/ds/ArenaAllocatorExtensions.h')
-rw-r--r--xpcom/ds/ArenaAllocatorExtensions.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/xpcom/ds/ArenaAllocatorExtensions.h b/xpcom/ds/ArenaAllocatorExtensions.h
new file mode 100644
index 0000000000..60819381d5
--- /dev/null
+++ b/xpcom/ds/ArenaAllocatorExtensions.h
@@ -0,0 +1,76 @@
+/* -*- 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 <typename T, size_t ArenaSize, size_t Alignment>
+T* ArenaStrdup(const T* aStr, ArenaAllocator<ArenaSize, Alignment>& aArena) {
+ return detail::DuplicateString(aStr, nsCharTraits<T>::length(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 <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.
+ */
+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