GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/util-inl.h Lines: 189 204 92.6 %
Date: 2019-09-07 22:28:56 Branches: 113 184 61.4 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#ifndef SRC_UTIL_INL_H_
23
#define SRC_UTIL_INL_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include <cmath>
28
#include <cstring>
29
#include "util.h"
30
31
// These are defined by <sys/byteorder.h> or <netinet/in.h> on some systems.
32
// To avoid warnings, undefine them before redefining them.
33
#ifdef BSWAP_2
34
# undef BSWAP_2
35
#endif
36
#ifdef BSWAP_4
37
# undef BSWAP_4
38
#endif
39
#ifdef BSWAP_8
40
# undef BSWAP_8
41
#endif
42
43
#if defined(_MSC_VER)
44
#include <intrin.h>
45
#define BSWAP_2(x) _byteswap_ushort(x)
46
#define BSWAP_4(x) _byteswap_ulong(x)
47
#define BSWAP_8(x) _byteswap_uint64(x)
48
#else
49
#define BSWAP_2(x) ((x) << 8) | ((x) >> 8)
50
#define BSWAP_4(x)                                                            \
51
  (((x) & 0xFF) << 24) |                                                      \
52
  (((x) & 0xFF00) << 8) |                                                     \
53
  (((x) >> 8) & 0xFF00) |                                                     \
54
  (((x) >> 24) & 0xFF)
55
#define BSWAP_8(x)                                                            \
56
  (((x) & 0xFF00000000000000ull) >> 56) |                                     \
57
  (((x) & 0x00FF000000000000ull) >> 40) |                                     \
58
  (((x) & 0x0000FF0000000000ull) >> 24) |                                     \
59
  (((x) & 0x000000FF00000000ull) >> 8) |                                      \
60
  (((x) & 0x00000000FF000000ull) << 8) |                                      \
61
  (((x) & 0x0000000000FF0000ull) << 24) |                                     \
62
  (((x) & 0x000000000000FF00ull) << 40) |                                     \
63
  (((x) & 0x00000000000000FFull) << 56)
64
#endif
65
66
namespace node {
67
68
template <typename T>
69
202348
ListNode<T>::ListNode() : prev_(this), next_(this) {}
70
71
template <typename T>
72
200991
ListNode<T>::~ListNode() {
73
200991
  Remove();
74
200991
}
75
76
template <typename T>
77
203229
void ListNode<T>::Remove() {
78
203229
  prev_->next_ = next_;
79
203229
  next_->prev_ = prev_;
80
203229
  prev_ = this;
81
203229
  next_ = this;
82
203229
}
83
84
template <typename T>
85
28853
bool ListNode<T>::IsEmpty() const {
86
28853
  return prev_ == this;
87
}
88
89
template <typename T, ListNode<T> (T::*M)>
90
37198
ListHead<T, M>::Iterator::Iterator(ListNode<T>* node) : node_(node) {}
91
92
template <typename T, ListNode<T> (T::*M)>
93
2790
T* ListHead<T, M>::Iterator::operator*() const {
94
2790
  return ContainerOf(M, node_);
95
}
96
97
template <typename T, ListNode<T> (T::*M)>
98
const typename ListHead<T, M>::Iterator&
99
2790
ListHead<T, M>::Iterator::operator++() {
100
2790
  node_ = node_->next_;
101
2790
  return *this;
102
}
103
104
template <typename T, ListNode<T> (T::*M)>
105
21389
bool ListHead<T, M>::Iterator::operator!=(const Iterator& that) const {
106
21389
  return node_ != that.node_;
107
}
108
109
template <typename T, ListNode<T> (T::*M)>
110
9380
ListHead<T, M>::~ListHead() {
111

18760
  while (IsEmpty() == false)
112
    head_.next_->Remove();
113
9380
}
114
115
template <typename T, ListNode<T> (T::*M)>
116
194370
void ListHead<T, M>::PushBack(T* element) {
117
194370
  ListNode<T>* that = &(element->*M);
118
194370
  head_.prev_->next_ = that;
119
194370
  that->prev_ = head_.prev_;
120
194370
  that->next_ = &head_;
121
194370
  head_.prev_ = that;
122
194370
}
123
124
template <typename T, ListNode<T> (T::*M)>
125
void ListHead<T, M>::PushFront(T* element) {
126
  ListNode<T>* that = &(element->*M);
127
  head_.next_->prev_ = that;
128
  that->prev_ = &head_;
129
  that->next_ = head_.next_;
130
  head_.next_ = that;
131
}
132
133
template <typename T, ListNode<T> (T::*M)>
134
28853
bool ListHead<T, M>::IsEmpty() const {
135
28853
  return head_.IsEmpty();
136
}
137
138
template <typename T, ListNode<T> (T::*M)>
139
T* ListHead<T, M>::PopFront() {
140
  if (IsEmpty())
141
    return nullptr;
142
  ListNode<T>* node = head_.next_;
143
  node->Remove();
144
  return ContainerOf(M, node);
145
}
146
147
template <typename T, ListNode<T> (T::*M)>
148
18599
typename ListHead<T, M>::Iterator ListHead<T, M>::begin() const {
149
18599
  return Iterator(head_.next_);
150
}
151
152
template <typename T, ListNode<T> (T::*M)>
153
18599
typename ListHead<T, M>::Iterator ListHead<T, M>::end() const {
154
18599
  return Iterator(const_cast<ListNode<T>*>(&head_));
155
}
156
157
template <typename Inner, typename Outer>
158
2497505
constexpr uintptr_t OffsetOf(Inner Outer::*field) {
159
2497505
  return reinterpret_cast<uintptr_t>(&(static_cast<Outer*>(nullptr)->*field));
160
}
161
162
template <typename Inner, typename Outer>
163
2452993
ContainerOfHelper<Inner, Outer>::ContainerOfHelper(Inner Outer::*field,
164
                                                   Inner* pointer)
165
    : pointer_(
166
        reinterpret_cast<Outer*>(
167
2452993
            reinterpret_cast<uintptr_t>(pointer) - OffsetOf(field))) {}
168
169
template <typename Inner, typename Outer>
170
template <typename TypeName>
171
2452980
ContainerOfHelper<Inner, Outer>::operator TypeName*() const {
172
2452980
  return static_cast<TypeName*>(pointer_);
173
}
174
175
template <typename Inner, typename Outer>
176
2452993
constexpr ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
177
                                                      Inner* pointer) {
178
2452993
  return ContainerOfHelper<Inner, Outer>(field, pointer);
179
}
180
181
5278722
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
182
                                           const char* data,
183
                                           int length) {
184
  return v8::String::NewFromOneByte(isolate,
185
                                    reinterpret_cast<const uint8_t*>(data),
186
                                    v8::NewStringType::kNormal,
187
10557448
                                    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
76
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
200
                                           const unsigned char* data,
201
                                           int length) {
202
  return v8::String::NewFromOneByte(
203
             isolate, data, v8::NewStringType::kNormal, length)
204
152
      .ToLocalChecked();
205
}
206
207
6
void SwapBytes16(char* data, size_t nbytes) {
208
6
  CHECK_EQ(nbytes % 2, 0);
209
210
#if defined(_MSC_VER)
211
  int align = reinterpret_cast<uintptr_t>(data) % sizeof(uint16_t);
212
  if (align == 0) {
213
    // MSVC has no strict aliasing, and is able to highly optimize this case.
214
    uint16_t* data16 = reinterpret_cast<uint16_t*>(data);
215
    size_t len16 = nbytes / sizeof(*data16);
216
    for (size_t i = 0; i < len16; i++) {
217
      data16[i] = BSWAP_2(data16[i]);
218
    }
219
    return;
220
  }
221
#endif
222
223
  uint16_t temp;
224
2053
  for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
225
2047
    memcpy(&temp, &data[i], sizeof(temp));
226
2047
    temp = BSWAP_2(temp);
227
2047
    memcpy(&data[i], &temp, sizeof(temp));
228
  }
229
6
}
230
231
2
void SwapBytes32(char* data, size_t nbytes) {
232
2
  CHECK_EQ(nbytes % 4, 0);
233
234
#if defined(_MSC_VER)
235
  int align = reinterpret_cast<uintptr_t>(data) % sizeof(uint32_t);
236
  // MSVC has no strict aliasing, and is able to highly optimize this case.
237
  if (align == 0) {
238
    uint32_t* data32 = reinterpret_cast<uint32_t*>(data);
239
    size_t len32 = nbytes / sizeof(*data32);
240
    for (size_t i = 0; i < len32; i++) {
241
      data32[i] = BSWAP_4(data32[i]);
242
    }
243
    return;
244
  }
245
#endif
246
247
  uint32_t temp;
248
769
  for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
249
767
    memcpy(&temp, &data[i], sizeof(temp));
250
767
    temp = BSWAP_4(temp);
251
767
    memcpy(&data[i], &temp, sizeof(temp));
252
  }
253
2
}
254
255
2
void SwapBytes64(char* data, size_t nbytes) {
256
2
  CHECK_EQ(nbytes % 8, 0);
257
258
#if defined(_MSC_VER)
259
  int align = reinterpret_cast<uintptr_t>(data) % sizeof(uint64_t);
260
  if (align == 0) {
261
    // MSVC has no strict aliasing, and is able to highly optimize this case.
262
    uint64_t* data64 = reinterpret_cast<uint64_t*>(data);
263
    size_t len64 = nbytes / sizeof(*data64);
264
    for (size_t i = 0; i < len64; i++) {
265
      data64[i] = BSWAP_8(data64[i]);
266
    }
267
    return;
268
  }
269
#endif
270
271
  uint64_t temp;
272
513
  for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
273
511
    memcpy(&temp, &data[i], sizeof(temp));
274
511
    temp = BSWAP_8(temp);
275
511
    memcpy(&data[i], &temp, sizeof(temp));
276
  }
277
2
}
278
279
12915037
char ToLower(char c) {
280

12915037
  return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c;
281
}
282
283
877
std::string ToLower(const std::string& in) {
284
877
  std::string out(in.size(), 0);
285
11316
  for (size_t i = 0; i < in.size(); ++i)
286
10439
    out[i] = ToLower(in[i]);
287
877
  return out;
288
}
289
290
12256
char ToUpper(char c) {
291

12256
  return c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c;
292
}
293
294
std::string ToUpper(const std::string& in) {
295
  std::string out(in.size(), 0);
296
  for (size_t i = 0; i < in.size(); ++i)
297
    out[i] = ToUpper(in[i]);
298
  return out;
299
}
300
301
6969578
bool StringEqualNoCase(const char* a, const char* b) {
302
6451162
  do {
303
6969578
    if (*a == '\0')
304
518402
      return *b == '\0';
305
6451176
    if (*b == '\0')
306
14
      return *a == '\0';
307
6451162
  } while (ToLower(*a++) == ToLower(*b++));
308
4663886
  return false;
309
}
310
311
332
bool StringEqualNoCaseN(const char* a, const char* b, size_t length) {
312
1243
  for (size_t i = 0; i < length; i++) {
313
1137
    if (ToLower(a[i]) != ToLower(b[i]))
314
226
      return false;
315
911
    if (a[i] == '\0')
316
      return true;
317
  }
318
106
  return true;
319
}
320
321
template <typename T>
322
3692590
inline T MultiplyWithOverflowCheck(T a, T b) {
323
3692590
  auto ret = a * b;
324
3692590
  if (a != 0)
325
3692589
    CHECK_EQ(b, ret / a);
326
327
3692590
  return ret;
328
}
329
330
// These should be used in our code as opposed to the native
331
// versions as they abstract out some platform and or
332
// compiler version specific functionality.
333
// malloc(0) and realloc(ptr, 0) have implementation-defined behavior in
334
// that the standard allows them to either return a unique pointer or a
335
// nullptr for zero-sized allocation requests.  Normalize by always using
336
// a nullptr.
337
template <typename T>
338
3159781
T* UncheckedRealloc(T* pointer, size_t n) {
339
3159781
  size_t full_size = MultiplyWithOverflowCheck(sizeof(T), n);
340
341


3159781
  if (full_size == 0) {
342
102440
    free(pointer);
343
102440
    return nullptr;
344
  }
345
346
3057341
  void* allocated = realloc(pointer, full_size);
347
348


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


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




412389
  CHECK_IMPLIES(n > 0, ret != nullptr);
375
412389
  return ret;
376
}
377
378
template <typename T>
379
114
inline T* Malloc(size_t n) {
380
114
  T* ret = UncheckedMalloc<T>(n);
381






114
  CHECK_IMPLIES(n > 0, ret != nullptr);
382
114
  return ret;
383
}
384
385
template <typename T>
386
inline T* Calloc(size_t n) {
387
  T* ret = UncheckedCalloc<T>(n);
388
  CHECK_IMPLIES(n > 0, ret != nullptr);
389
  return ret;
390
}
391
392
// Shortcuts for char*.
393
12
inline char* Malloc(size_t n) { return Malloc<char>(n); }
394
inline char* Calloc(size_t n) { return Calloc<char>(n); }
395
2211487
inline char* UncheckedMalloc(size_t n) { return UncheckedMalloc<char>(n); }
396
61591
inline char* UncheckedCalloc(size_t n) { return UncheckedCalloc<char>(n); }
397
398
// This is a helper in the .cc file so including util-inl.h doesn't include more
399
// headers than we really need to.
400
void ThrowErrStringTooLong(v8::Isolate* isolate);
401
402
2934459
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
403
                                    const std::string& str,
404
                                    v8::Isolate* isolate) {
405
4110437
  if (isolate == nullptr) isolate = context->GetIsolate();
406
2934459
  if (UNLIKELY(str.size() >= static_cast<size_t>(v8::String::kMaxLength))) {
407
    // V8 only has a TODO comment about adding an exception when the maximum
408
    // string size is exceeded.
409
    ThrowErrStringTooLong(isolate);
410
    return v8::MaybeLocal<v8::Value>();
411
  }
412
413
  return v8::String::NewFromUtf8(
414
2934460
             isolate, str.data(), v8::NewStringType::kNormal, str.size())
415
5868924
      .FromMaybe(v8::Local<v8::String>());
416
}
417
418
template <typename T>
419
164889
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
420
                                    const std::vector<T>& vec,
421
                                    v8::Isolate* isolate) {
422
238194
  if (isolate == nullptr) isolate = context->GetIsolate();
423
164889
  v8::EscapableHandleScope handle_scope(isolate);
424
425
329778
  MaybeStackBuffer<v8::Local<v8::Value>, 128> arr(vec.size());
426
164889
  arr.SetLength(vec.size());
427
1831795
  for (size_t i = 0; i < vec.size(); ++i) {
428
3333810
    if (!ToV8Value(context, vec[i], isolate).ToLocal(&arr[i]))
429
      return v8::MaybeLocal<v8::Value>();
430
  }
431
432
329778
  return handle_scope.Escape(v8::Array::New(isolate, arr.out(), arr.length()));
433
}
434
435
template <typename T, typename U>
436
5088
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
437
                                    const std::unordered_map<T, U>& map,
438
                                    v8::Isolate* isolate) {
439
10176
  if (isolate == nullptr) isolate = context->GetIsolate();
440
5088
  v8::EscapableHandleScope handle_scope(isolate);
441
442
5088
  v8::Local<v8::Map> ret = v8::Map::New(isolate);
443
96672
  for (const auto& item : map) {
444
    v8::Local<v8::Value> first, second;
445


641088
    if (!ToV8Value(context, item.first, isolate).ToLocal(&first) ||
446
274752
        !ToV8Value(context, item.second, isolate).ToLocal(&second) ||
447
274752
        ret->Set(context, first, second).IsEmpty()) {
448
      return v8::MaybeLocal<v8::Value>();
449
    }
450
  }
451
452
5088
  return handle_scope.Escape(ret);
453
}
454
455
template <typename T, typename >
456
2
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
457
                                    const T& number,
458
                                    v8::Isolate* isolate) {
459
2
  if (isolate == nullptr) isolate = context->GetIsolate();
460
461
  using Limits = std::numeric_limits<T>;
462
  // Choose Uint32, Int32, or Double depending on range checks.
463
  // These checks should all collapse at compile time.
464

6
  if (static_cast<uint32_t>(Limits::max()) <=
465
2
          std::numeric_limits<uint32_t>::max() &&
466
2
      static_cast<uint32_t>(Limits::min()) >=
467
2
          std::numeric_limits<uint32_t>::min() && Limits::is_exact) {
468
4
    return v8::Integer::NewFromUnsigned(isolate, static_cast<uint32_t>(number));
469
  }
470
471
  if (static_cast<int32_t>(Limits::max()) <=
472
          std::numeric_limits<int32_t>::max() &&
473
      static_cast<int32_t>(Limits::min()) >=
474
          std::numeric_limits<int32_t>::min() && Limits::is_exact) {
475
    return v8::Integer::New(isolate, static_cast<int32_t>(number));
476
  }
477
478
  return v8::Number::New(isolate, static_cast<double>(number));
479
}
480
481
231964
SlicedArguments::SlicedArguments(
482
231964
    const v8::FunctionCallbackInfo<v8::Value>& args, size_t start) {
483
231964
  const size_t length = static_cast<size_t>(args.Length());
484
463928
  if (start >= length) return;
485
231714
  const size_t size = length - start;
486
487
231714
  AllocateSufficientStorage(size);
488
674623
  for (size_t i = 0; i < size; ++i)
489
885818
    (*this)[i] = args[i + start];
490
}
491
492
template <typename T, size_t S>
493
854116
ArrayBufferViewContents<T, S>::ArrayBufferViewContents(
494
854116
    v8::Local<v8::Value> value) {
495

854116
  CHECK(value->IsArrayBufferView());
496
854116
  Read(value.As<v8::ArrayBufferView>());
497
854116
}
498
499
template <typename T, size_t S>
500
528474
ArrayBufferViewContents<T, S>::ArrayBufferViewContents(
501
528474
    v8::Local<v8::Object> value) {
502
528474
  CHECK(value->IsArrayBufferView());
503
528474
  Read(value.As<v8::ArrayBufferView>());
504
528474
}
505
506
template <typename T, size_t S>
507
193052
ArrayBufferViewContents<T, S>::ArrayBufferViewContents(
508
193052
    v8::Local<v8::ArrayBufferView> abv) {
509
193052
  Read(abv);
510
193052
}
511
512
template <typename T, size_t S>
513
1576810
void ArrayBufferViewContents<T, S>::Read(v8::Local<v8::ArrayBufferView> abv) {
514
  static_assert(sizeof(T) == 1, "Only supports one-byte data at the moment");
515
1576810
  length_ = abv->ByteLength();
516



2187613
  if (length_ > sizeof(stack_storage_) || abv->HasBuffer()) {
517
4720275
    data_ = static_cast<T*>(abv->Buffer()->GetContents().Data()) +
518
1573425
        abv->ByteOffset();
519
  } else {
520
6770
    abv->CopyContents(stack_storage_, sizeof(stack_storage_));
521
3385
    data_ = stack_storage_;
522
  }
523
1576810
}
524
525
// ECMA262 20.1.2.5
526
540326
inline bool IsSafeJsInt(v8::Local<v8::Value> v) {
527
540326
  if (!v->IsNumber()) return false;
528
792542
  double v_d = v.As<v8::Number>()->Value();
529
396271
  if (std::isnan(v_d)) return false;
530
396271
  if (std::isinf(v_d)) return false;
531
396271
  if (std::trunc(v_d) != v_d) return false;  // not int
532
396270
  if (std::abs(v_d) <= static_cast<double>(kMaxSafeJsInteger)) return true;
533
  return false;
534
}
535
536
}  // namespace node
537
538
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
539
540
#endif  // SRC_UTIL_INL_H_