Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/ArenaAllocatorExtensions.h
Line
Count
Source (jump to first uncovered line)
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<typename T, size_t ArenaSize, size_t Alignment>
36
T* ArenaStrdup(const T* aStr,
37
               ArenaAllocator<ArenaSize, Alignment>& aArena)
38
7.14k
{
39
7.14k
  return detail::DuplicateString(aStr, nsCharTraits<T>::length(aStr), aArena);
40
7.14k
}
char* mozilla::ArenaStrdup<char, 8192ul, 8ul>(char const*, mozilla::ArenaAllocator<8192ul, 8ul>&)
Line
Count
Source
38
255
{
39
255
  return detail::DuplicateString(aStr, nsCharTraits<T>::length(aStr), aArena);
40
255
}
char* mozilla::ArenaStrdup<char, 4096ul, 1ul>(char const*, mozilla::ArenaAllocator<4096ul, 1ul>&)
Line
Count
Source
38
6.89k
{
39
6.89k
  return detail::DuplicateString(aStr, nsCharTraits<T>::length(aStr), aArena);
40
6.89k
}
Unexecuted instantiation: char* mozilla::ArenaStrdup<char, 4096ul, 8ul>(char const*, mozilla::ArenaAllocator<4096ul, 8ul>&)
Unexecuted instantiation: char16_t* mozilla::ArenaStrdup<char16_t, 4096ul, 8ul>(char16_t const*, mozilla::ArenaAllocator<4096ul, 8ul>&)
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<typename T, size_t ArenaSize, size_t Alignment>
50
T* ArenaStrdup(const detail::nsTStringRepr<T>& aStr,
51
               ArenaAllocator<ArenaSize, Alignment>& aArena)
52
0
{
53
0
  return detail::DuplicateString(aStr.BeginReading(), aStr.Length(), aArena);
54
0
}
Unexecuted instantiation: char* mozilla::ArenaStrdup<char, 2048ul, 4ul>(mozilla::detail::nsTStringRepr<char> const&, mozilla::ArenaAllocator<2048ul, 4ul>&)
Unexecuted instantiation: char16_t* mozilla::ArenaStrdup<char16_t, 2048ul, 4ul>(mozilla::detail::nsTStringRepr<char16_t> const&, mozilla::ArenaAllocator<2048ul, 4ul>&)
Unexecuted instantiation: char16_t* mozilla::ArenaStrdup<char16_t, 4096ul, 8ul>(mozilla::detail::nsTStringRepr<char16_t> const&, mozilla::ArenaAllocator<4096ul, 8ul>&)
Unexecuted instantiation: char* mozilla::ArenaStrdup<char, 4096ul, 8ul>(mozilla::detail::nsTStringRepr<char> const&, mozilla::ArenaAllocator<4096ul, 8ul>&)
55
56
/**
57
 * Copies the source string and adds a null terminator. Source string does not
58
 * have to be null terminated.
59
 */
60
template<typename T, size_t ArenaSize, size_t Alignment>
61
T* detail::DuplicateString(const T* aSrc, const CheckedInt<size_t>& aLen,
62
                           ArenaAllocator<ArenaSize, Alignment>& aArena)
63
7.14k
{
64
7.14k
  const auto byteLen = (aLen + 1) * sizeof(T);
65
7.14k
  if (!byteLen.isValid()) {
66
0
    return nullptr;
67
0
  }
68
7.14k
69
7.14k
  T* p = static_cast<T*>(aArena.Allocate(byteLen.value(), mozilla::fallible));
70
7.14k
  if (p) {
71
7.14k
    memcpy(p, aSrc, byteLen.value() - sizeof(T));
72
7.14k
    p[aLen.value()] = T(0);
73
7.14k
  }
74
7.14k
75
7.14k
  return p;
76
7.14k
}
Unexecuted instantiation: char* mozilla::detail::DuplicateString<char, 2048ul, 4ul>(char const*, mozilla::CheckedInt<unsigned long> const&, mozilla::ArenaAllocator<2048ul, 4ul>&)
Unexecuted instantiation: char16_t* mozilla::detail::DuplicateString<char16_t, 2048ul, 4ul>(char16_t const*, mozilla::CheckedInt<unsigned long> const&, mozilla::ArenaAllocator<2048ul, 4ul>&)
char* mozilla::detail::DuplicateString<char, 8192ul, 8ul>(char const*, mozilla::CheckedInt<unsigned long> const&, mozilla::ArenaAllocator<8192ul, 8ul>&)
Line
Count
Source
63
255
{
64
255
  const auto byteLen = (aLen + 1) * sizeof(T);
65
255
  if (!byteLen.isValid()) {
66
0
    return nullptr;
67
0
  }
68
255
69
255
  T* p = static_cast<T*>(aArena.Allocate(byteLen.value(), mozilla::fallible));
70
255
  if (p) {
71
255
    memcpy(p, aSrc, byteLen.value() - sizeof(T));
72
255
    p[aLen.value()] = T(0);
73
255
  }
74
255
75
255
  return p;
76
255
}
char* mozilla::detail::DuplicateString<char, 4096ul, 1ul>(char const*, mozilla::CheckedInt<unsigned long> const&, mozilla::ArenaAllocator<4096ul, 1ul>&)
Line
Count
Source
63
6.89k
{
64
6.89k
  const auto byteLen = (aLen + 1) * sizeof(T);
65
6.89k
  if (!byteLen.isValid()) {
66
0
    return nullptr;
67
0
  }
68
6.89k
69
6.89k
  T* p = static_cast<T*>(aArena.Allocate(byteLen.value(), mozilla::fallible));
70
6.89k
  if (p) {
71
6.89k
    memcpy(p, aSrc, byteLen.value() - sizeof(T));
72
6.89k
    p[aLen.value()] = T(0);
73
6.89k
  }
74
6.89k
75
6.89k
  return p;
76
6.89k
}
Unexecuted instantiation: char* mozilla::detail::DuplicateString<char, 4096ul, 8ul>(char const*, mozilla::CheckedInt<unsigned long> const&, mozilla::ArenaAllocator<4096ul, 8ul>&)
Unexecuted instantiation: char16_t* mozilla::detail::DuplicateString<char16_t, 4096ul, 8ul>(char16_t const*, mozilla::CheckedInt<unsigned long> const&, mozilla::ArenaAllocator<4096ul, 8ul>&)
77
78
} // namespace mozilla
79
80
#endif // mozilla_ArenaAllocatorExtensions_h