Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef mozilla_ArenaAllocatorExtensions_h
8 : #define mozilla_ArenaAllocatorExtensions_h
9 :
10 : #include "mozilla/ArenaAllocator.h"
11 : #include "mozilla/CheckedInt.h"
12 : #include "nsAString.h"
13 :
14 : /**
15 : * Extensions to the ArenaAllocator class.
16 : */
17 : namespace mozilla {
18 :
19 : namespace detail {
20 :
21 : template<typename T, size_t ArenaSize, size_t Alignment>
22 : T* DuplicateString(const T* aSrc, const CheckedInt<size_t>& aLen,
23 : ArenaAllocator<ArenaSize, Alignment>& aArena);
24 :
25 : } // namespace detail
26 :
27 : /**
28 : * Makes an arena allocated null-terminated copy of the source string. The
29 : * source string must be null-terminated.
30 : *
31 : * @param aSrc String to copy.
32 : * @param aArena The arena to allocate the string copy out of.
33 : * @return An arena allocated null-terminated string.
34 : */
35 : template<size_t ArenaSize, size_t Alignment>
36 10108 : char* ArenaStrdup(const char* aStr,
37 : ArenaAllocator<ArenaSize, Alignment>& aArena)
38 : {
39 10108 : return detail::DuplicateString(aStr, strlen(aStr), aArena);
40 : }
41 :
42 : /**
43 : * Makes an arena allocated null-terminated copy of the source string.
44 : *
45 : * @param aSrc String to copy.
46 : * @param aArena The arena to allocate the string copy out of.
47 : * @return An arena allocated null-terminated string.
48 : */
49 : template<size_t ArenaSize, size_t Alignment>
50 1300 : nsAString::char_type* ArenaStrdup(
51 : const nsAString& aStr, ArenaAllocator<ArenaSize, Alignment>& aArena)
52 : {
53 1300 : return detail::DuplicateString(aStr.BeginReading(), aStr.Length(), aArena);
54 : }
55 :
56 : /**
57 : * Makes an arena allocated null-terminated copy of the source string.
58 : *
59 : * @param aSrc String to copy.
60 : * @param aArena The arena to allocate the string copy out of.
61 : * @return An arena allocated null-terminated string.
62 : */
63 : template<size_t ArenaSize, size_t Alignment>
64 1300 : nsACString::char_type* ArenaStrdup(
65 : const nsACString& aStr, ArenaAllocator<ArenaSize, Alignment>& aArena)
66 : {
67 1300 : return detail::DuplicateString(aStr.BeginReading(), aStr.Length(), aArena);
68 : }
69 :
70 : /**
71 : * Copies the source string and adds a null terminator. Source string does not
72 : * have to be null terminated.
73 : */
74 : template<typename T, size_t ArenaSize, size_t Alignment>
75 12708 : T* detail::DuplicateString(const T* aSrc, const CheckedInt<size_t>& aLen,
76 : ArenaAllocator<ArenaSize, Alignment>& aArena)
77 : {
78 12708 : const auto byteLen = (aLen + 1) * sizeof(T);
79 12708 : if (!byteLen.isValid()) {
80 0 : return nullptr;
81 : }
82 :
83 12708 : T* p = static_cast<T*>(aArena.Allocate(byteLen.value(), mozilla::fallible));
84 12708 : if (p) {
85 12708 : memcpy(p, aSrc, byteLen.value() - sizeof(T));
86 12708 : p[aLen.value()] = T(0);
87 : }
88 :
89 12708 : return p;
90 : }
91 :
92 : } // namespace mozilla
93 :
94 : #endif // mozilla_ArenaAllocatorExtensions_h
|