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: 175 189 92.6 %
Date: 2019-03-02 22:23:06 Branches: 102 168 60.7 %

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 <cstring>
28
#include "util.h"
29
30
#if defined(_MSC_VER)
31
#include <intrin.h>
32
#define BSWAP_2(x) _byteswap_ushort(x)
33
#define BSWAP_4(x) _byteswap_ulong(x)
34
#define BSWAP_8(x) _byteswap_uint64(x)
35
#else
36
#define BSWAP_2(x) ((x) << 8) | ((x) >> 8)
37
#define BSWAP_4(x)                                                            \
38
  (((x) & 0xFF) << 24) |                                                      \
39
  (((x) & 0xFF00) << 8) |                                                     \
40
  (((x) >> 8) & 0xFF00) |                                                     \
41
  (((x) >> 24) & 0xFF)
42
#define BSWAP_8(x)                                                            \
43
  (((x) & 0xFF00000000000000ull) >> 56) |                                     \
44
  (((x) & 0x00FF000000000000ull) >> 40) |                                     \
45
  (((x) & 0x0000FF0000000000ull) >> 24) |                                     \
46
  (((x) & 0x000000FF00000000ull) >> 8) |                                      \
47
  (((x) & 0x00000000FF000000ull) << 8) |                                      \
48
  (((x) & 0x0000000000FF0000ull) << 24) |                                     \
49
  (((x) & 0x000000000000FF00ull) << 40) |                                     \
50
  (((x) & 0x00000000000000FFull) << 56)
51
#endif
52
53
namespace node {
54
55
template <typename T>
56
256749
ListNode<T>::ListNode() : prev_(this), next_(this) {}
57
58
template <typename T>
59
255527
ListNode<T>::~ListNode() {
60
255527
  Remove();
61
255527
}
62
63
template <typename T>
64
257572
void ListNode<T>::Remove() {
65
257572
  prev_->next_ = next_;
66
257572
  next_->prev_ = prev_;
67
257572
  prev_ = this;
68
257572
  next_ = this;
69
257572
}
70
71
template <typename T>
72
16363
bool ListNode<T>::IsEmpty() const {
73
16363
  return prev_ == this;
74
}
75
76
template <typename T, ListNode<T> (T::*M)>
77
33078
ListHead<T, M>::Iterator::Iterator(ListNode<T>* node) : node_(node) {}
78
79
template <typename T, ListNode<T> (T::*M)>
80
2283
T* ListHead<T, M>::Iterator::operator*() const {
81
2283
  return ContainerOf(M, node_);
82
}
83
84
template <typename T, ListNode<T> (T::*M)>
85
const typename ListHead<T, M>::Iterator&
86
2283
ListHead<T, M>::Iterator::operator++() {
87
2283
  node_ = node_->next_;
88
2283
  return *this;
89
}
90
91
template <typename T, ListNode<T> (T::*M)>
92
18822
bool ListHead<T, M>::Iterator::operator!=(const Iterator& that) const {
93
18822
  return node_ != that.node_;
94
}
95
96
template <typename T, ListNode<T> (T::*M)>
97
8098
ListHead<T, M>::~ListHead() {
98

16196
  while (IsEmpty() == false)
99
    head_.next_->Remove();
100
8097
}
101
102
template <typename T, ListNode<T> (T::*M)>
103
249942
void ListHead<T, M>::PushBack(T* element) {
104
249942
  ListNode<T>* that = &(element->*M);
105
249942
  head_.prev_->next_ = that;
106
249942
  that->prev_ = head_.prev_;
107
249942
  that->next_ = &head_;
108
249942
  head_.prev_ = that;
109
249942
}
110
111
template <typename T, ListNode<T> (T::*M)>
112
void ListHead<T, M>::PushFront(T* element) {
113
  ListNode<T>* that = &(element->*M);
114
  head_.next_->prev_ = that;
115
  that->prev_ = &head_;
116
  that->next_ = head_.next_;
117
  head_.next_ = that;
118
}
119
120
template <typename T, ListNode<T> (T::*M)>
121
16364
bool ListHead<T, M>::IsEmpty() const {
122
16364
  return head_.IsEmpty();
123
}
124
125
template <typename T, ListNode<T> (T::*M)>
126
T* ListHead<T, M>::PopFront() {
127
  if (IsEmpty())
128
    return nullptr;
129
  ListNode<T>* node = head_.next_;
130
  node->Remove();
131
  return ContainerOf(M, node);
132
}
133
134
template <typename T, ListNode<T> (T::*M)>
135
16539
typename ListHead<T, M>::Iterator ListHead<T, M>::begin() const {
136
16539
  return Iterator(head_.next_);
137
}
138
139
template <typename T, ListNode<T> (T::*M)>
140
16539
typename ListHead<T, M>::Iterator ListHead<T, M>::end() const {
141
16539
  return Iterator(const_cast<ListNode<T>*>(&head_));
142
}
143
144
template <typename Inner, typename Outer>
145
2565615
constexpr uintptr_t OffsetOf(Inner Outer::*field) {
146
2565615
  return reinterpret_cast<uintptr_t>(&(static_cast<Outer*>(nullptr)->*field));
147
}
148
149
template <typename Inner, typename Outer>
150
2526986
ContainerOfHelper<Inner, Outer>::ContainerOfHelper(Inner Outer::*field,
151
                                                   Inner* pointer)
152
    : pointer_(
153
        reinterpret_cast<Outer*>(
154
2526986
            reinterpret_cast<uintptr_t>(pointer) - OffsetOf(field))) {}
155
156
template <typename Inner, typename Outer>
157
template <typename TypeName>
158
2526982
ContainerOfHelper<Inner, Outer>::operator TypeName*() const {
159
2526982
  return static_cast<TypeName*>(pointer_);
160
}
161
162
template <typename Inner, typename Outer>
163
2526974
constexpr ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
164
                                                      Inner* pointer) {
165
2526974
  return ContainerOfHelper<Inner, Outer>(field, pointer);
166
}
167
168
3313553
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
169
                                           const char* data,
170
                                           int length) {
171
  return v8::String::NewFromOneByte(isolate,
172
                                    reinterpret_cast<const uint8_t*>(data),
173
                                    v8::NewStringType::kNormal,
174
6627114
                                    length).ToLocalChecked();
175
}
176
177
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
178
                                           const signed char* data,
179
                                           int length) {
180
  return v8::String::NewFromOneByte(isolate,
181
                                    reinterpret_cast<const uint8_t*>(data),
182
                                    v8::NewStringType::kNormal,
183
                                    length).ToLocalChecked();
184
}
185
186
112
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
187
                                           const unsigned char* data,
188
                                           int length) {
189
  return v8::String::NewFromOneByte(isolate,
190
                                    reinterpret_cast<const uint8_t*>(data),
191
                                    v8::NewStringType::kNormal,
192
224
                                    length).ToLocalChecked();
193
}
194
195
2
void SwapBytes16(char* data, size_t nbytes) {
196
2
  CHECK_EQ(nbytes % 2, 0);
197
198
#if defined(_MSC_VER)
199
  int align = reinterpret_cast<uintptr_t>(data) % sizeof(uint16_t);
200
  if (align == 0) {
201
    // MSVC has no strict aliasing, and is able to highly optimize this case.
202
    uint16_t* data16 = reinterpret_cast<uint16_t*>(data);
203
    size_t len16 = nbytes / sizeof(*data16);
204
    for (size_t i = 0; i < len16; i++) {
205
      data16[i] = BSWAP_2(data16[i]);
206
    }
207
    return;
208
  }
209
#endif
210
211
  uint16_t temp;
212
1537
  for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
213
1535
    memcpy(&temp, &data[i], sizeof(temp));
214
1535
    temp = BSWAP_2(temp);
215
1535
    memcpy(&data[i], &temp, sizeof(temp));
216
  }
217
2
}
218
219
2
void SwapBytes32(char* data, size_t nbytes) {
220
2
  CHECK_EQ(nbytes % 4, 0);
221
222
#if defined(_MSC_VER)
223
  int align = reinterpret_cast<uintptr_t>(data) % sizeof(uint32_t);
224
  // MSVC has no strict aliasing, and is able to highly optimize this case.
225
  if (align == 0) {
226
    uint32_t* data32 = reinterpret_cast<uint32_t*>(data);
227
    size_t len32 = nbytes / sizeof(*data32);
228
    for (size_t i = 0; i < len32; i++) {
229
      data32[i] = BSWAP_4(data32[i]);
230
    }
231
    return;
232
  }
233
#endif
234
235
  uint32_t temp;
236
769
  for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
237
767
    memcpy(&temp, &data[i], sizeof(temp));
238
767
    temp = BSWAP_4(temp);
239
767
    memcpy(&data[i], &temp, sizeof(temp));
240
  }
241
2
}
242
243
2
void SwapBytes64(char* data, size_t nbytes) {
244
2
  CHECK_EQ(nbytes % 8, 0);
245
246
#if defined(_MSC_VER)
247
  int align = reinterpret_cast<uintptr_t>(data) % sizeof(uint64_t);
248
  if (align == 0) {
249
    // MSVC has no strict aliasing, and is able to highly optimize this case.
250
    uint64_t* data64 = reinterpret_cast<uint64_t*>(data);
251
    size_t len64 = nbytes / sizeof(*data64);
252
    for (size_t i = 0; i < len64; i++) {
253
      data64[i] = BSWAP_8(data64[i]);
254
    }
255
    return;
256
  }
257
#endif
258
259
  uint64_t temp;
260
513
  for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
261
511
    memcpy(&temp, &data[i], sizeof(temp));
262
511
    temp = BSWAP_8(temp);
263
511
    memcpy(&data[i], &temp, sizeof(temp));
264
  }
265
2
}
266
267
11922179
char ToLower(char c) {
268

11922179
  return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c;
269
}
270
271
133
std::string ToLower(const std::string& in) {
272
133
  std::string out(in.size(), 0);
273
1608
  for (size_t i = 0; i < in.size(); ++i)
274
1475
    out[i] = ToLower(in[i]);
275
133
  return out;
276
}
277
278
6393668
bool StringEqualNoCase(const char* a, const char* b) {
279
5959490
  do {
280
6393668
    if (*a == '\0')
281
434162
      return *b == '\0';
282
5959506
    if (*b == '\0')
283
16
      return *a == '\0';
284
5959490
  } while (ToLower(*a++) == ToLower(*b++));
285
4621226
  return false;
286
}
287
288
252
bool StringEqualNoCaseN(const char* a, const char* b, size_t length) {
289
941
  for (size_t i = 0; i < length; i++) {
290
862
    if (ToLower(a[i]) != ToLower(b[i]))
291
173
      return false;
292
689
    if (a[i] == '\0')
293
      return true;
294
  }
295
79
  return true;
296
}
297
298
template <typename T>
299
3368902
inline T MultiplyWithOverflowCheck(T a, T b) {
300
3368902
  auto ret = a * b;
301
3368902
  if (a != 0)
302
3368901
    CHECK_EQ(b, ret / a);
303
304
3368902
  return ret;
305
}
306
307
// These should be used in our code as opposed to the native
308
// versions as they abstract out some platform and or
309
// compiler version specific functionality.
310
// malloc(0) and realloc(ptr, 0) have implementation-defined behavior in
311
// that the standard allows them to either return a unique pointer or a
312
// nullptr for zero-sized allocation requests.  Normalize by always using
313
// a nullptr.
314
template <typename T>
315
3259946
T* UncheckedRealloc(T* pointer, size_t n) {
316
3259946
  size_t full_size = MultiplyWithOverflowCheck(sizeof(T), n);
317
318


3259946
  if (full_size == 0) {
319
117857
    free(pointer);
320
117857
    return nullptr;
321
  }
322
323
3142089
  void* allocated = realloc(pointer, full_size);
324
325


3142089
  if (UNLIKELY(allocated == nullptr)) {
326
    // Tell V8 that memory is low and retry.
327
    LowMemoryNotification();
328
    allocated = realloc(pointer, full_size);
329
  }
330
331
3142089
  return static_cast<T*>(allocated);
332
}
333
334
// As per spec realloc behaves like malloc if passed nullptr.
335
template <typename T>
336
2199439
inline T* UncheckedMalloc(size_t n) {
337


2199439
  if (n == 0) n = 1;
338
2199439
  return UncheckedRealloc<T>(nullptr, n);
339
}
340
341
template <typename T>
342
46349
inline T* UncheckedCalloc(size_t n) {
343
46349
  if (n == 0) n = 1;
344
46349
  MultiplyWithOverflowCheck(sizeof(T), n);
345
46349
  return static_cast<T*>(calloc(n, sizeof(T)));
346
}
347
348
template <typename T>
349
1795
inline T* Realloc(T* pointer, size_t n) {
350
1795
  T* ret = UncheckedRealloc(pointer, n);
351




1795
  CHECK_IMPLIES(n > 0, ret != nullptr);
352
1795
  return ret;
353
}
354
355
template <typename T>
356
105
inline T* Malloc(size_t n) {
357
105
  T* ret = UncheckedMalloc<T>(n);
358






105
  CHECK_IMPLIES(n > 0, ret != nullptr);
359
105
  return ret;
360
}
361
362
template <typename T>
363
inline T* Calloc(size_t n) {
364
  T* ret = UncheckedCalloc<T>(n);
365
  CHECK_IMPLIES(n > 0, ret != nullptr);
366
  return ret;
367
}
368
369
// Shortcuts for char*.
370
12
inline char* Malloc(size_t n) { return Malloc<char>(n); }
371
inline char* Calloc(size_t n) { return Calloc<char>(n); }
372
2199308
inline char* UncheckedMalloc(size_t n) { return UncheckedMalloc<char>(n); }
373
46349
inline char* UncheckedCalloc(size_t n) { return UncheckedCalloc<char>(n); }
374
375
// This is a helper in the .cc file so including util-inl.h doesn't include more
376
// headers than we really need to.
377
void ThrowErrStringTooLong(v8::Isolate* isolate);
378
379
1485782
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
380
                                    const std::string& str,
381
                                    v8::Isolate* isolate) {
382
2239806
  if (isolate == nullptr) isolate = context->GetIsolate();
383
1485782
  if (UNLIKELY(str.size() >= static_cast<size_t>(v8::String::kMaxLength))) {
384
    // V8 only has a TODO comment about adding an exception when the maximum
385
    // string size is exceeded.
386
    ThrowErrStringTooLong(isolate);
387
    return v8::MaybeLocal<v8::Value>();
388
  }
389
390
  return v8::String::NewFromUtf8(
391
1485782
             isolate, str.data(), v8::NewStringType::kNormal, str.size())
392
2971564
      .FromMaybe(v8::Local<v8::String>());
393
}
394
395
template <typename T>
396
141630
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
397
                                    const std::vector<T>& vec,
398
                                    v8::Isolate* isolate) {
399
208324
  if (isolate == nullptr) isolate = context->GetIsolate();
400
141630
  v8::EscapableHandleScope handle_scope(isolate);
401
402
283260
  MaybeStackBuffer<v8::Local<v8::Value>, 128> arr(vec.size());
403
141630
  arr.SetLength(vec.size());
404
798454
  for (size_t i = 0; i < vec.size(); ++i) {
405
1313648
    if (!ToV8Value(context, vec[i], isolate).ToLocal(&arr[i]))
406
      return v8::MaybeLocal<v8::Value>();
407
  }
408
409
283260
  return handle_scope.Escape(v8::Array::New(isolate, arr.out(), arr.length()));
410
}
411
412
template <typename T, typename U>
413
4408
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
414
                                    const std::unordered_map<T, U>& map,
415
                                    v8::Isolate* isolate) {
416
8816
  if (isolate == nullptr) isolate = context->GetIsolate();
417
4408
  v8::EscapableHandleScope handle_scope(isolate);
418
419
4408
  v8::Local<v8::Map> ret = v8::Map::New(isolate);
420
79344
  for (const auto& item : map) {
421
    v8::Local<v8::Value> first, second;
422


524552
    if (!ToV8Value(context, item.first, isolate).ToLocal(&first) ||
423
224808
        !ToV8Value(context, item.second, isolate).ToLocal(&second) ||
424
224808
        ret->Set(context, first, second).IsEmpty()) {
425
      return v8::MaybeLocal<v8::Value>();
426
    }
427
  }
428
429
4408
  return handle_scope.Escape(ret);
430
}
431
432
template <typename T, typename >
433
2
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
434
                                    const T& number,
435
                                    v8::Isolate* isolate) {
436
2
  if (isolate == nullptr) isolate = context->GetIsolate();
437
438
  using Limits = std::numeric_limits<T>;
439
  // Choose Uint32, Int32, or Double depending on range checks.
440
  // These checks should all collapse at compile time.
441

6
  if (static_cast<uint32_t>(Limits::max()) <=
442
2
          std::numeric_limits<uint32_t>::max() &&
443
2
      static_cast<uint32_t>(Limits::min()) >=
444
2
          std::numeric_limits<uint32_t>::min() && Limits::is_exact) {
445
4
    return v8::Integer::NewFromUnsigned(isolate, static_cast<uint32_t>(number));
446
  }
447
448
  if (static_cast<int32_t>(Limits::max()) <=
449
          std::numeric_limits<int32_t>::max() &&
450
      static_cast<int32_t>(Limits::min()) >=
451
          std::numeric_limits<int32_t>::min() && Limits::is_exact) {
452
    return v8::Integer::New(isolate, static_cast<int32_t>(number));
453
  }
454
455
  return v8::Number::New(isolate, static_cast<double>(number));
456
}
457
458
128287
SlicedArguments::SlicedArguments(
459
128287
    const v8::FunctionCallbackInfo<v8::Value>& args, size_t start) {
460
128287
  const size_t length = static_cast<size_t>(args.Length());
461
256574
  if (start >= length) return;
462
128047
  const size_t size = length - start;
463
464
128047
  AllocateSufficientStorage(size);
465
463951
  for (size_t i = 0; i < size; ++i)
466
671808
    (*this)[i] = args[i + start];
467
}
468
469
template <typename T, size_t S>
470
5085
ArrayBufferViewContents<T, S>::ArrayBufferViewContents(
471
5085
    v8::Local<v8::Value> value) {
472

5085
  CHECK(value->IsArrayBufferView());
473
5085
  Read(value.As<v8::ArrayBufferView>());
474
5085
}
475
476
template <typename T, size_t S>
477
136971
ArrayBufferViewContents<T, S>::ArrayBufferViewContents(
478
136971
    v8::Local<v8::ArrayBufferView> abv) {
479
136971
  Read(abv);
480
136971
}
481
482
template <typename T, size_t S>
483
143219
void ArrayBufferViewContents<T, S>::Read(v8::Local<v8::ArrayBufferView> abv) {
484
  static_assert(sizeof(T) == 1, "Only supports one-byte data at the moment");
485
143219
  length_ = abv->ByteLength();
486



272611
  if (length_ > sizeof(stack_storage_) || abv->HasBuffer()) {
487
428097
    data_ = static_cast<T*>(abv->Buffer()->GetContents().Data()) +
488
142699
        abv->ByteOffset();
489
  } else {
490
1040
    abv->CopyContents(stack_storage_, sizeof(stack_storage_));
491
520
    data_ = stack_storage_;
492
  }
493
143219
}
494
495
}  // namespace node
496
497
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
498
499
#endif  // SRC_UTIL_INL_H_