GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/util-inl.h Lines: 110 120 91.7 %
Date: 2017-01-02 Branches: 107 188 56.9 %

Line Branch Exec Source
1
#ifndef SRC_UTIL_INL_H_
2
#define SRC_UTIL_INL_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "util.h"
7
#include <cstring>
8
9
#if defined(_MSC_VER)
10
#include <intrin.h>
11
#define BSWAP_2(x) _byteswap_ushort(x)
12
#define BSWAP_4(x) _byteswap_ulong(x)
13
#define BSWAP_8(x) _byteswap_uint64(x)
14
#else
15
#define BSWAP_2(x) ((x) << 8) | ((x) >> 8)
16
#define BSWAP_4(x)                                                            \
17
  (((x) & 0xFF) << 24) |                                                      \
18
  (((x) & 0xFF00) << 8) |                                                     \
19
  (((x) >> 8) & 0xFF00) |                                                     \
20
  (((x) >> 24) & 0xFF)
21
#define BSWAP_8(x)                                                            \
22
  (((x) & 0xFF00000000000000ull) >> 56) |                                     \
23
  (((x) & 0x00FF000000000000ull) >> 40) |                                     \
24
  (((x) & 0x0000FF0000000000ull) >> 24) |                                     \
25
  (((x) & 0x000000FF00000000ull) >> 8) |                                      \
26
  (((x) & 0x00000000FF000000ull) << 8) |                                      \
27
  (((x) & 0x0000000000FF0000ull) << 24) |                                     \
28
  (((x) & 0x000000000000FF00ull) << 40) |                                     \
29
  (((x) & 0x00000000000000FFull) << 56)
30
#endif
31
32
namespace node {
33
34
template <typename T>
35
65516
ListNode<T>::ListNode() : prev_(this), next_(this) {}
36
37
template <typename T>
38
ListNode<T>::~ListNode() {
39
60800
  Remove();
40
60800
}
41
42
template <typename T>
43
void ListNode<T>::Remove() {
44
70721
  prev_->next_ = next_;
45
70721
  next_->prev_ = prev_;
46
69813
  prev_ = this;
47
69813
  next_ = this;
48
}
49
50
template <typename T>
51
bool ListNode<T>::IsEmpty() const {
52
29680
  return prev_ == this;
53
}
54
55
template <typename T, ListNode<T> (T::*M)>
56
ListHead<T, M>::Iterator::Iterator(ListNode<T>* node) : node_(node) {}
57
58
template <typename T, ListNode<T> (T::*M)>
59
T* ListHead<T, M>::Iterator::operator*() const {
60
64
  return ContainerOf(M, node_);
61
}
62
63
template <typename T, ListNode<T> (T::*M)>
64
const typename ListHead<T, M>::Iterator&
65
ListHead<T, M>::Iterator::operator++() {
66
32
  node_ = node_->next_;
67
  return *this;
68
}
69
70
template <typename T, ListNode<T> (T::*M)>
71
bool ListHead<T, M>::Iterator::operator!=(const Iterator& that) const {
72
  return node_ != that.node_;
73
}
74
75
template <typename T, ListNode<T> (T::*M)>
76
ListHead<T, M>::~ListHead() {
77


8506
  while (IsEmpty() == false)
78
1298
    head_.next_->Remove();
79
14416
}
80
81
template <typename T, ListNode<T> (T::*M)>
82
void ListHead<T, M>::MoveBack(ListHead* that) {
83


2452
  if (IsEmpty())
84
    return;
85
1818
  ListNode<T>* to = &that->head_;
86
1818
  head_.next_->prev_ = to->prev_;
87
1818
  to->prev_->next_ = head_.next_;
88
1818
  head_.prev_->next_ = to;
89
1818
  to->prev_ = head_.prev_;
90
1818
  head_.prev_ = &head_;
91
1818
  head_.next_ = &head_;
92
}
93
94
template <typename T, ListNode<T> (T::*M)>
95
void ListHead<T, M>::PushBack(T* element) {
96
56316
  ListNode<T>* that = &(element->*M);
97
56316
  head_.prev_->next_ = that;
98
56316
  that->prev_ = head_.prev_;
99
56316
  that->next_ = &head_;
100
56316
  head_.prev_ = that;
101
}
102
103
template <typename T, ListNode<T> (T::*M)>
104
void ListHead<T, M>::PushFront(T* element) {
105
  ListNode<T>* that = &(element->*M);
106
  head_.next_->prev_ = that;
107
  that->prev_ = &head_;
108
  that->next_ = head_.next_;
109
  head_.next_ = that;
110
}
111
112
template <typename T, ListNode<T> (T::*M)>
113
bool ListHead<T, M>::IsEmpty() const {
114
60268
  return head_.IsEmpty();
115
}
116
117
template <typename T, ListNode<T> (T::*M)>
118
T* ListHead<T, M>::PopFront() {
119

12625
  if (IsEmpty())
120
    return nullptr;
121
8623
  ListNode<T>* node = head_.next_;
122
8623
  node->Remove();
123
8623
  return ContainerOf(M, node);
124
}
125
126
template <typename T, ListNode<T> (T::*M)>
127
typename ListHead<T, M>::Iterator ListHead<T, M>::begin() const {
128
4
  return Iterator(head_.next_);
129
}
130
131
template <typename T, ListNode<T> (T::*M)>
132
typename ListHead<T, M>::Iterator ListHead<T, M>::end() const {
133
4
  return Iterator(const_cast<ListNode<T>*>(&head_));
134
}
135
136
template <typename Inner, typename Outer>
137
ContainerOfHelper<Inner, Outer>::ContainerOfHelper(Inner Outer::*field,
138
                                                   Inner* pointer)
139
    : pointer_(reinterpret_cast<Outer*>(
140
          reinterpret_cast<uintptr_t>(pointer) -
141
137962
          reinterpret_cast<uintptr_t>(&(static_cast<Outer*>(0)->*field)))) {
142
}
143
144
template <typename Inner, typename Outer>
145
template <typename TypeName>
146
ContainerOfHelper<Inner, Outer>::operator TypeName*() const {
147
  return static_cast<TypeName*>(pointer_);
148
}
149
150
template <typename Inner, typename Outer>
151
inline ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
152
                                                   Inner* pointer) {
153
137967
  return ContainerOfHelper<Inner, Outer>(field, pointer);
154
}
155
156
template <class TypeName>
157
520941
inline v8::Local<TypeName> PersistentToLocal(
158
    v8::Isolate* isolate,
159
    const v8::Persistent<TypeName>& persistent) {
160

1041882
  if (persistent.IsWeak()) {
161
    return WeakPersistentToLocal(isolate, persistent);
162
  } else {
163
    return StrongPersistentToLocal(persistent);
164
  }
165
}
166
167
template <class TypeName>
168
inline v8::Local<TypeName> StrongPersistentToLocal(
169
    const v8::Persistent<TypeName>& persistent) {
170
  return *reinterpret_cast<v8::Local<TypeName>*>(
171
1888144
      const_cast<v8::Persistent<TypeName>*>(&persistent));
172
}
173
174
template <class TypeName>
175
inline v8::Local<TypeName> WeakPersistentToLocal(
176
    v8::Isolate* isolate,
177
    const v8::Persistent<TypeName>& persistent) {
178
28706
  return v8::Local<TypeName>::New(isolate, persistent);
179
}
180
181
955899
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
182
                                           const char* data,
183
                                           int length) {
184
955899
  return v8::String::NewFromOneByte(isolate,
185
                                    reinterpret_cast<const uint8_t*>(data),
186
                                    v8::NewStringType::kNormal,
187
955899
                                    length).ToLocalChecked();
188
}
189
190
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
191
                                           const signed char* data,
192
                                           int length) {
193
  return v8::String::NewFromOneByte(isolate,
194
                                    reinterpret_cast<const uint8_t*>(data),
195
                                    v8::NewStringType::kNormal,
196
                                    length).ToLocalChecked();
197
}
198
199
311
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
200
                                           const unsigned char* data,
201
                                           int length) {
202
311
  return v8::String::NewFromOneByte(isolate,
203
                                    reinterpret_cast<const uint8_t*>(data),
204
                                    v8::NewStringType::kNormal,
205
311
                                    length).ToLocalChecked();
206
}
207
208
template <typename TypeName>
209
175048
void Wrap(v8::Local<v8::Object> object, TypeName* pointer) {
210






175048
  CHECK_EQ(false, object.IsEmpty());
211






175048
  CHECK_GT(object->InternalFieldCount(), 0);
212
175048
  object->SetAlignedPointerInInternalField(0, pointer);
213
175048
}
214
215
void ClearWrap(v8::Local<v8::Object> object) {
216
45034
  Wrap<void>(object, nullptr);
217
}
218
219
template <typename TypeName>
220
223956
TypeName* Unwrap(v8::Local<v8::Object> object) {
221





223956
  CHECK_EQ(false, object.IsEmpty());
222





223956
  CHECK_GT(object->InternalFieldCount(), 0);
223
447912
  void* pointer = object->GetAlignedPointerFromInternalField(0);
224
223956
  return static_cast<TypeName*>(pointer);
225
}
226
227
2
void SwapBytes16(char* data, size_t nbytes) {
228
2
  CHECK_EQ(nbytes % 2, 0);
229
230
#if defined(_MSC_VER)
231
  int align = reinterpret_cast<uintptr_t>(data) % sizeof(uint16_t);
232
  if (align == 0) {
233
    // MSVC has no strict aliasing, and is able to highly optimize this case.
234
    uint16_t* data16 = reinterpret_cast<uint16_t*>(data);
235
    size_t len16 = nbytes / sizeof(*data16);
236
    for (size_t i = 0; i < len16; i++) {
237
      data16[i] = BSWAP_2(data16[i]);
238
    }
239
    return;
240
  }
241
#endif
242
243
  uint16_t temp;
244
3072
  for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
245
1535
    memcpy(&temp, &data[i], sizeof(temp));
246
1535
    temp = BSWAP_2(temp);
247
3070
    memcpy(&data[i], &temp, sizeof(temp));
248
  }
249
2
}
250
251
2
void SwapBytes32(char* data, size_t nbytes) {
252
2
  CHECK_EQ(nbytes % 4, 0);
253
254
#if defined(_MSC_VER)
255
  int align = reinterpret_cast<uintptr_t>(data) % sizeof(uint32_t);
256
  // MSVC has no strict aliasing, and is able to highly optimize this case.
257
  if (align == 0) {
258
    uint32_t* data32 = reinterpret_cast<uint32_t*>(data);
259
    size_t len32 = nbytes / sizeof(*data32);
260
    for (size_t i = 0; i < len32; i++) {
261
      data32[i] = BSWAP_4(data32[i]);
262
    }
263
    return;
264
  }
265
#endif
266
267
  uint32_t temp;
268
1536
  for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
269
767
    memcpy(&temp, &data[i], sizeof(temp));
270
767
    temp = BSWAP_4(temp);
271
1534
    memcpy(&data[i], &temp, sizeof(temp));
272
  }
273
2
}
274
275
2
void SwapBytes64(char* data, size_t nbytes) {
276
2
  CHECK_EQ(nbytes % 8, 0);
277
278
#if defined(_MSC_VER)
279
  int align = reinterpret_cast<uintptr_t>(data) % sizeof(uint64_t);
280
  if (align == 0) {
281
    // MSVC has no strict aliasing, and is able to highly optimize this case.
282
    uint64_t* data64 = reinterpret_cast<uint64_t*>(data);
283
    size_t len64 = nbytes / sizeof(*data64);
284
    for (size_t i = 0; i < len64; i++) {
285
      data64[i] = BSWAP_8(data64[i]);
286
    }
287
    return;
288
  }
289
#endif
290
291
  uint64_t temp;
292
1024
  for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
293
511
    memcpy(&temp, &data[i], sizeof(temp));
294
511
    temp = BSWAP_8(temp);
295
1022
    memcpy(&data[i], &temp, sizeof(temp));
296
  }
297
2
}
298
299
char ToLower(char c) {
300

70676
  return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c;
301
}
302
303
30019
bool StringEqualNoCase(const char* a, const char* b) {
304
35338
  do {
305
37896
    if (*a == '\0')
306
2557
      return *b == '\0';
307
35339
    if (*b == '\0')
308
1
      return *a == '\0';
309
106014
  } while (ToLower(*a++) == ToLower(*b++));
310
  return false;
311
}
312
313
bool StringEqualNoCaseN(const char* a, const char* b, size_t length) {
314
  for (size_t i = 0; i < length; i++) {
315
    if (ToLower(a[i]) != ToLower(b[i]))
316
      return false;
317
    if (a[i] == '\0')
318
      return true;
319
  }
320
  return true;
321
}
322
323
inline size_t MultiplyWithOverflowCheck(size_t a, size_t b) {
324
58439
  size_t ret = a * b;
325
  if (a != 0)
326
14
    CHECK_EQ(b, ret / a);
327
328
  return ret;
329
}
330
331
// These should be used in our code as opposed to the native
332
// versions as they abstract out some platform and or
333
// compiler version specific functionality.
334
// malloc(0) and realloc(ptr, 0) have implementation-defined behavior in
335
// that the standard allows them to either return a unique pointer or a
336
// nullptr for zero-sized allocation requests.  Normalize by always using
337
// a nullptr.
338
template <typename T>
339
52896
T* UncheckedRealloc(T* pointer, size_t n) {
340
52896
  size_t full_size = MultiplyWithOverflowCheck(sizeof(T), n);
341
342

52896
  if (full_size == 0) {
343
3
    free(pointer);
344
3
    return nullptr;
345
  }
346
347
52893
  void* allocated = realloc(pointer, full_size);
348
349

52893
  if (UNLIKELY(allocated == nullptr)) {
350
    // Tell V8 that memory is low and retry.
351
    LowMemoryNotification();
352
    allocated = realloc(pointer, full_size);
353
  }
354
355
  return static_cast<T*>(allocated);
356
}
357
358
// As per spec realloc behaves like malloc if passed nullptr.
359
template <typename T>
360
inline T* UncheckedMalloc(size_t n) {
361


34738
  if (n == 0) n = 1;
362
34740
  return UncheckedRealloc<T>(nullptr, n);
363
}
364
365
template <typename T>
366
inline T* UncheckedCalloc(size_t n) {
367
5543
  if (n == 0) n = 1;
368
5543
  MultiplyWithOverflowCheck(sizeof(T), n);
369
5543
  return static_cast<T*>(calloc(n, sizeof(T)));
370
}
371
372
template <typename T>
373
17627
inline T* Realloc(T* pointer, size_t n) {
374
17627
  T* ret = UncheckedRealloc(pointer, n);
375

17627
  if (n > 0) CHECK_NE(ret, nullptr);
376
17627
  return ret;
377
}
378
379
template <typename T>
380
21542
inline T* Malloc(size_t n) {
381
21542
  T* ret = UncheckedMalloc<T>(n);
382



21542
  if (n > 0) CHECK_NE(ret, nullptr);
383
21542
  return ret;
384
}
385
386
template <typename T>
387
inline T* Calloc(size_t n) {
388
  T* ret = UncheckedCalloc<T>(n);
389
  if (n > 0) CHECK_NE(ret, nullptr);
390
  return ret;
391
}
392
393
// Shortcuts for char*.
394
21217
inline char* Malloc(size_t n) { return Malloc<char>(n); }
395
inline char* Calloc(size_t n) { return Calloc<char>(n); }
396
13169
inline char* UncheckedMalloc(size_t n) { return UncheckedMalloc<char>(n); }
397
5543
inline char* UncheckedCalloc(size_t n) { return UncheckedCalloc<char>(n); }
398
399
}  // namespace node
400
401
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
402
403
#endif  // SRC_UTIL_INL_H_