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: 162 176 92.0 %
Date: 2019-02-13 22:28:58 Branches: 107 190 56.3 %

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
259168
ListNode<T>::ListNode() : prev_(this), next_(this) {}
57
58
template <typename T>
59
257947
ListNode<T>::~ListNode() {
60
257947
  Remove();
61
257947
}
62
63
template <typename T>
64
259984
void ListNode<T>::Remove() {
65
259984
  prev_->next_ = next_;
66
259984
  next_->prev_ = prev_;
67
259984
  prev_ = this;
68
259984
  next_ = this;
69
259984
}
70
71
template <typename T>
72
16351
bool ListNode<T>::IsEmpty() const {
73
16351
  return prev_ == this;
74
}
75
76
template <typename T, ListNode<T> (T::*M)>
77
33026
ListHead<T, M>::Iterator::Iterator(ListNode<T>* node) : node_(node) {}
78
79
template <typename T, ListNode<T> (T::*M)>
80
2278
T* ListHead<T, M>::Iterator::operator*() const {
81
2278
  return ContainerOf(M, node_);
82
}
83
84
template <typename T, ListNode<T> (T::*M)>
85
const typename ListHead<T, M>::Iterator&
86
2278
ListHead<T, M>::Iterator::operator++() {
87
2278
  node_ = node_->next_;
88
2278
  return *this;
89
}
90
91
template <typename T, ListNode<T> (T::*M)>
92
18791
bool ListHead<T, M>::Iterator::operator!=(const Iterator& that) const {
93
18791
  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
8098
}
101
102
template <typename T, ListNode<T> (T::*M)>
103
252351
void ListHead<T, M>::PushBack(T* element) {
104
252351
  ListNode<T>* that = &(element->*M);
105
252351
  head_.prev_->next_ = that;
106
252351
  that->prev_ = head_.prev_;
107
252351
  that->next_ = &head_;
108
252351
  head_.prev_ = that;
109
252351
}
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
16351
bool ListHead<T, M>::IsEmpty() const {
122
16351
  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
16513
typename ListHead<T, M>::Iterator ListHead<T, M>::begin() const {
136
16513
  return Iterator(head_.next_);
137
}
138
139
template <typename T, ListNode<T> (T::*M)>
140
16513
typename ListHead<T, M>::Iterator ListHead<T, M>::end() const {
141
16513
  return Iterator(const_cast<ListNode<T>*>(&head_));
142
}
143
144
template <typename Inner, typename Outer>
145
2557516
constexpr uintptr_t OffsetOf(Inner Outer::*field) {
146
2557516
  return reinterpret_cast<uintptr_t>(&(static_cast<Outer*>(nullptr)->*field));
147
}
148
149
template <typename Inner, typename Outer>
150
2518691
ContainerOfHelper<Inner, Outer>::ContainerOfHelper(Inner Outer::*field,
151
                                                   Inner* pointer)
152
    : pointer_(
153
        reinterpret_cast<Outer*>(
154
2518691
            reinterpret_cast<uintptr_t>(pointer) - OffsetOf(field))) {}
155
156
template <typename Inner, typename Outer>
157
template <typename TypeName>
158
2518684
ContainerOfHelper<Inner, Outer>::operator TypeName*() const {
159
2518684
  return static_cast<TypeName*>(pointer_);
160
}
161
162
template <typename Inner, typename Outer>
163
2518688
constexpr ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
164
                                                      Inner* pointer) {
165
2518688
  return ContainerOfHelper<Inner, Outer>(field, pointer);
166
}
167
168
3467530
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
6935076
                                    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
11783386
char ToLower(char c) {
268

11783386
  return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c;
269
}
270
271
43
std::string ToLower(const std::string& in) {
272
43
  std::string out(in.size(), 0);
273
523
  for (size_t i = 0; i < in.size(); ++i)
274
480
    out[i] = ToLower(in[i]);
275
43
  return out;
276
}
277
278
6319848
bool StringEqualNoCase(const char* a, const char* b) {
279
5890591
  do {
280
6319848
    if (*a == '\0')
281
429241
      return *b == '\0';
282
5890607
    if (*b == '\0')
283
16
      return *a == '\0';
284
5890591
  } while (ToLower(*a++) == ToLower(*b++));
285
4567087
  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
3278371
inline T MultiplyWithOverflowCheck(T a, T b) {
300
3278371
  auto ret = a * b;
301
3278371
  if (a != 0)
302
3278370
    CHECK_EQ(b, ret / a);
303
304
3278371
  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
3175837
T* UncheckedRealloc(T* pointer, size_t n) {
316
3175837
  size_t full_size = MultiplyWithOverflowCheck(sizeof(T), n);
317
318


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


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


2175188
  if (n == 0) n = 1;
338
2175188
  return UncheckedRealloc<T>(nullptr, n);
339
}
340
341
template <typename T>
342
95012
inline T* UncheckedCalloc(size_t n) {
343


95012
  if (n == 0) n = 1;
344
95012
  MultiplyWithOverflowCheck(sizeof(T), n);
345
95012
  return static_cast<T*>(calloc(n, sizeof(T)));
346
}
347
348
template <typename T>
349
348074
inline T* Realloc(T* pointer, size_t n) {
350
348074
  T* ret = UncheckedRealloc(pointer, n);
351




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






429498
  CHECK_IMPLIES(n > 0, ret != nullptr);
359
429498
  return ret;
360
}
361
362
template <typename T>
363
49154
inline T* Calloc(size_t n) {
364
49154
  T* ret = UncheckedCalloc<T>(n);
365







49154
  CHECK_IMPLIES(n > 0, ret != nullptr);
366
49154
  return ret;
367
}
368
369
// Shortcuts for char*.
370
400359
inline char* Malloc(size_t n) { return Malloc<char>(n); }
371
inline char* Calloc(size_t n) { return Calloc<char>(n); }
372
1745440
inline char* UncheckedMalloc(size_t n) { return UncheckedMalloc<char>(n); }
373
45858
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
1485226
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
380
                                    const std::string& str,
381
                                    v8::Isolate* isolate) {
382
2244830
  if (isolate == nullptr) isolate = context->GetIsolate();
383
1485226
  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
1485226
             isolate, str.data(), v8::NewStringType::kNormal, str.size())
392
2970452
      .FromMaybe(v8::Local<v8::String>());
393
}
394
395
template <typename T>
396
141206
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
397
                                    const std::vector<T>& vec,
398
                                    v8::Isolate* isolate) {
399
207340
  if (isolate == nullptr) isolate = context->GetIsolate();
400
141206
  v8::EscapableHandleScope handle_scope(isolate);
401
402
282412
  MaybeStackBuffer<v8::Local<v8::Value>, 128> arr(vec.size());
403
141206
  arr.SetLength(vec.size());
404
791758
  for (size_t i = 0; i < vec.size(); ++i) {
405
1301104
    if (!ToV8Value(context, vec[i], isolate).ToLocal(&arr[i]))
406
      return v8::MaybeLocal<v8::Value>();
407
  }
408
409
282412
  return handle_scope.Escape(v8::Array::New(isolate, arr.out(), arr.length()));
410
}
411
412
template <typename T, typename U>
413
4416
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
414
                                    const std::unordered_map<T, U>& map,
415
                                    v8::Isolate* isolate) {
416
8832
  if (isolate == nullptr) isolate = context->GetIsolate();
417
4416
  v8::EscapableHandleScope handle_scope(isolate);
418
419
4416
  v8::Local<v8::Map> ret = v8::Map::New(isolate);
420
79488
  for (const auto& item : map) {
421
    v8::Local<v8::Value> first, second;
422


525504
    if (!ToV8Value(context, item.first, isolate).ToLocal(&first) ||
423
225216
        !ToV8Value(context, item.second, isolate).ToLocal(&second) ||
424
225216
        ret->Set(context, first, second).IsEmpty()) {
425
      return v8::MaybeLocal<v8::Value>();
426
    }
427
  }
428
429
4416
  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
138143
SlicedArguments::SlicedArguments(
459
138143
    const v8::FunctionCallbackInfo<v8::Value>& args, size_t start) {
460
138143
  const size_t length = static_cast<size_t>(args.Length());
461
276286
  if (start >= length) return;
462
137904
  const size_t size = length - start;
463
464
137904
  AllocateSufficientStorage(size);
465
483596
  for (size_t i = 0; i < size; ++i)
466
691384
    (*this)[i] = args[i + start];
467
}
468
469
}  // namespace node
470
471
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
472
473
#endif  // SRC_UTIL_INL_H_