File: PodOperations.h

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (176 lines) | stat: -rw-r--r-- 6,290 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* -*- 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/. */

/*
 * Operations for zeroing POD types, arrays, and so on.
 *
 * These operations are preferable to memset, memcmp, and the like because they
 * don't require remembering to multiply by sizeof(T), array lengths, and so on
 * everywhere.
 */

#ifndef mozilla_PodOperations_h
#define mozilla_PodOperations_h

#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"

#include <cstdint>
#include <cstring>
#include <limits>
#include <type_traits>

namespace mozilla {

template <typename T, size_t Length>
class Array;

template <typename T>
class NotNull;

/** Set the contents of |aT| to 0. */
template <typename T>
static MOZ_ALWAYS_INLINE void PodZero(T* aT) {
  static_assert(std::is_trivially_copyable_v<T>,
                "PodZero requires trivially copyable types");
  memset(aT, 0, sizeof(T));
}

/** Set the contents of |aNElem| elements starting at |aT| to 0. */
template <typename T>
static MOZ_ALWAYS_INLINE void PodZero(T* aT, size_t aNElem) {
  static_assert(std::is_trivially_copyable_v<T>,
                "PodZero requires trivially copyable types");
  /*
   * NB: If the caller uses a constant size, both GCC and Clang inline the
   * memset call if they find it profitable.
   *
   * If the value is dynamic, some might think that it's more profitable to
   * perform an explicit loop over the aNElem. It turns out Clang rolls back the
   * loop anyway, so even if GCC doesn't, keep the codebase simple and clearly
   * convey the intent instead of trying to outsmart the compiler.
   */
  MOZ_ASSERT(aNElem <= std::numeric_limits<size_t>::max() / sizeof(T),
             "trying to zero an impossible number of elements");
  memset(aT, 0, sizeof(T) * aNElem);
}

/** Set the contents of |aNElem| elements starting at |aT| to 0. */
template <typename T>
static MOZ_ALWAYS_INLINE void PodZero(NotNull<T*> aT, size_t aNElem) {
  PodZero(aT.get(), aNElem);
}

/*
 * Arrays implicitly convert to pointers to their first element, which is
 * dangerous when combined with the above PodZero definitions.  Adding an
 * overload for arrays is ambiguous, so we need another identifier.  The
 * ambiguous overload is left to catch mistaken uses of PodZero; if you get a
 * compile error involving PodZero and array types, use PodArrayZero instead.
 */
template <typename T, size_t N>
static void PodZero(T (&aT)[N]) = delete;
template <typename T, size_t N>
static void PodZero(T (&aT)[N], size_t aNElem) = delete;

/** Set the contents of the array |aT| to zero. */
template <class T, size_t N>
static MOZ_ALWAYS_INLINE void PodArrayZero(T (&aT)[N]) {
  static_assert(std::is_trivially_copyable_v<T>,
                "PodArrayZero requires trivially copyable types");
  static_assert(N < std::numeric_limits<size_t>::max() / sizeof(T));
  memset(aT, 0, N * sizeof(T));
}

template <typename T, size_t N>
static MOZ_ALWAYS_INLINE void PodArrayZero(Array<T, N>& aArr) {
  static_assert(std::is_trivially_copyable_v<T>,
                "PodArrayZero requires trivially copyable types");
  static_assert(N < std::numeric_limits<size_t>::max() / sizeof(T));
  memset(&aArr[0], 0, N * sizeof(T));
}

/**
 * Copy |aNElem| T elements from |aSrc| to |aDst|.  The two memory ranges must
 * not overlap!
 */
template <typename T>
static MOZ_ALWAYS_INLINE void PodCopy(T* aDst, const T* aSrc, size_t aNElem) {
  static_assert(std::is_trivially_copyable_v<T>,
                "PodCopy requires trivially copyable types");
  MOZ_ASSERT(aDst + aNElem <= aSrc || aSrc + aNElem <= aDst,
             "destination and source must not overlap");
  MOZ_ASSERT(aNElem <= std::numeric_limits<size_t>::max() / sizeof(T),
             "trying to copy an impossible number of elements");

// Linux memcpy for small sizes seems slower than on other
// platforms. So we use a loop for small sizes there only.
//
// See Bug 1967062 for details.
#if defined(XP_LINUX)
  if (aNElem < 128) {
    for (const T* srcend = aSrc + aNElem; aSrc < srcend; aSrc++, aDst++) {
      *aDst = *aSrc;
    }
    return;
  }
#endif

  memcpy(aDst, aSrc, aNElem * sizeof(T));
}

template <typename T>
static MOZ_ALWAYS_INLINE void PodCopy(volatile T* aDst, const volatile T* aSrc,
                                      size_t aNElem) {
  static_assert(std::is_trivially_copyable_v<T>,
                "PodCopy requires trivially copyable types");
  MOZ_ASSERT(aDst + aNElem <= aSrc || aSrc + aNElem <= aDst,
             "destination and source must not overlap");

  /*
   * Volatile |aDst| requires extra work, because it's undefined behavior to
   * modify volatile objects using the mem* functions.  Just write out the
   * loops manually, using operator= rather than memcpy for the same reason,
   * and let the compiler optimize to the extent it can.
   */
  for (const volatile T* srcend = aSrc + aNElem; aSrc < srcend;
       aSrc++, aDst++) {
    *aDst = *aSrc;
  }
}

/*
 * Copy the contents of the array |aSrc| into the array |aDst|, both of size N.
 * The arrays must not overlap!
 */
template <class T, size_t N>
static MOZ_ALWAYS_INLINE void PodArrayCopy(T (&aDst)[N], const T (&aSrc)[N]) {
  PodCopy(aDst, aSrc, N);
}

/**
 * Copy the memory for |aNElem| T elements from |aSrc| to |aDst|.  If the two
 * memory ranges overlap, then the effect is as if the |aNElem| elements are
 * first copied from |aSrc| to a temporary array, and then from the temporary
 * array to |aDst|.
 */
template <typename T>
static MOZ_ALWAYS_INLINE void PodMove(T* aDst, const T* aSrc, size_t aNElem) {
  static_assert(std::is_trivially_copyable_v<T>,
                "PodMove requires trivially copyable types");
  MOZ_ASSERT(aNElem <= std::numeric_limits<size_t>::max() / sizeof(T),
             "trying to move an impossible number of elements");
  memmove(aDst, aSrc, aNElem * sizeof(T));
}

/**
 * Looking for a PodEqual? Use ArrayEqual from ArrayUtils.h.
 * Note that we *cannot* use memcmp for this, due to padding bytes, etc..
 */

}  // namespace mozilla

#endif /* mozilla_PodOperations_h */