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: 158 172 91.9 %
Date: 2019-02-26 22:23:30 Branches: 88 152 57.9 %

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
257378
ListNode<T>::ListNode() : prev_(this), next_(this) {}
57
58
template <typename T>
59
256158
ListNode<T>::~ListNode() {
60
256158
  Remove();
61
256158
}
62
63
template <typename T>
64
258197
void ListNode<T>::Remove() {
65
258197
  prev_->next_ = next_;
66
258197
  next_->prev_ = prev_;
67
258197
  prev_ = this;
68
258197
  next_ = this;
69
258197
}
70
71
template <typename T>
72
16305
bool ListNode<T>::IsEmpty() const {
73
16305
  return prev_ == this;
74
}
75
76
template <typename T, ListNode<T> (T::*M)>
77
32954
ListHead<T, M>::Iterator::Iterator(ListNode<T>* node) : node_(node) {}
78
79
template <typename T, ListNode<T> (T::*M)>
80
2258
T* ListHead<T, M>::Iterator::operator*() const {
81
2258
  return ContainerOf(M, node_);
82
}
83
84
template <typename T, ListNode<T> (T::*M)>
85
const typename ListHead<T, M>::Iterator&
86
2258
ListHead<T, M>::Iterator::operator++() {
87
2258
  node_ = node_->next_;
88
2258
  return *this;
89
}
90
91
template <typename T, ListNode<T> (T::*M)>
92
18735
bool ListHead<T, M>::Iterator::operator!=(const Iterator& that) const {
93
18735
  return node_ != that.node_;
94
}
95
96
template <typename T, ListNode<T> (T::*M)>
97
8070
ListHead<T, M>::~ListHead() {
98

16140
  while (IsEmpty() == false)
99
    head_.next_->Remove();
100
8070
}
101
102
template <typename T, ListNode<T> (T::*M)>
103
250593
void ListHead<T, M>::PushBack(T* element) {
104
250593
  ListNode<T>* that = &(element->*M);
105
250593
  head_.prev_->next_ = that;
106
250593
  that->prev_ = head_.prev_;
107
250593
  that->next_ = &head_;
108
250593
  head_.prev_ = that;
109
250593
}
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
16305
bool ListHead<T, M>::IsEmpty() const {
122
16305
  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
16477
typename ListHead<T, M>::Iterator ListHead<T, M>::begin() const {
136
16477
  return Iterator(head_.next_);
137
}
138
139
template <typename T, ListNode<T> (T::*M)>
140
16477
typename ListHead<T, M>::Iterator ListHead<T, M>::end() const {
141
16477
  return Iterator(const_cast<ListNode<T>*>(&head_));
142
}
143
144
template <typename Inner, typename Outer>
145
2550255
constexpr uintptr_t OffsetOf(Inner Outer::*field) {
146
2550255
  return reinterpret_cast<uintptr_t>(&(static_cast<Outer*>(nullptr)->*field));
147
}
148
149
template <typename Inner, typename Outer>
150
2511716
ContainerOfHelper<Inner, Outer>::ContainerOfHelper(Inner Outer::*field,
151
                                                   Inner* pointer)
152
    : pointer_(
153
        reinterpret_cast<Outer*>(
154
2511716
            reinterpret_cast<uintptr_t>(pointer) - OffsetOf(field))) {}
155
156
template <typename Inner, typename Outer>
157
template <typename TypeName>
158
2511718
ContainerOfHelper<Inner, Outer>::operator TypeName*() const {
159
2511718
  return static_cast<TypeName*>(pointer_);
160
}
161
162
template <typename Inner, typename Outer>
163
2511716
constexpr ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
164
                                                      Inner* pointer) {
165
2511716
  return ContainerOfHelper<Inner, Outer>(field, pointer);
166
}
167
168
3300842
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
6601696
                                    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
11923261
char ToLower(char c) {
268

11923261
  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
6394248
bool StringEqualNoCase(const char* a, const char* b) {
279
5960031
  do {
280
6394248
    if (*a == '\0')
281
434201
      return *b == '\0';
282
5960047
    if (*b == '\0')
283
16
      return *a == '\0';
284
5960031
  } while (ToLower(*a++) == ToLower(*b++));
285
4621647
  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
3372201
inline T MultiplyWithOverflowCheck(T a, T b) {
300
3372201
  auto ret = a * b;
301
3372201
  if (a != 0)
302
3372199
    CHECK_EQ(b, ret / a);
303
304
3372201
  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
3219341
T* UncheckedRealloc(T* pointer, size_t n) {
316
3219341
  size_t full_size = MultiplyWithOverflowCheck(sizeof(T), n);
317
318


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


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


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




1799
  CHECK_IMPLIES(n > 0, ret != nullptr);
352
1799
  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
2169281
inline char* UncheckedMalloc(size_t n) { return UncheckedMalloc<char>(n); }
373
90423
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
1480480
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
380
                                    const std::string& str,
381
                                    v8::Isolate* isolate) {
382
2232106
  if (isolate == nullptr) isolate = context->GetIsolate();
383
1480480
  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
1480479
             isolate, str.data(), v8::NewStringType::kNormal, str.size())
392
2960958
      .FromMaybe(v8::Local<v8::String>());
393
}
394
395
template <typename T>
396
141138
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
397
                                    const std::vector<T>& vec,
398
                                    v8::Isolate* isolate) {
399
207578
  if (isolate == nullptr) isolate = context->GetIsolate();
400
141138
  v8::EscapableHandleScope handle_scope(isolate);
401
402
282276
  MaybeStackBuffer<v8::Local<v8::Value>, 128> arr(vec.size());
403
141138
  arr.SetLength(vec.size());
404
795296
  for (size_t i = 0; i < vec.size(); ++i) {
405
1308316
    if (!ToV8Value(context, vec[i], isolate).ToLocal(&arr[i]))
406
      return v8::MaybeLocal<v8::Value>();
407
  }
408
409
282276
  return handle_scope.Escape(v8::Array::New(isolate, arr.out(), arr.length()));
410
}
411
412
template <typename T, typename U>
413
4394
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
414
                                    const std::unordered_map<T, U>& map,
415
                                    v8::Isolate* isolate) {
416
8788
  if (isolate == nullptr) isolate = context->GetIsolate();
417
4394
  v8::EscapableHandleScope handle_scope(isolate);
418
419
4394
  v8::Local<v8::Map> ret = v8::Map::New(isolate);
420
79092
  for (const auto& item : map) {
421
    v8::Local<v8::Value> first, second;
422


522886
    if (!ToV8Value(context, item.first, isolate).ToLocal(&first) ||
423
224094
        !ToV8Value(context, item.second, isolate).ToLocal(&second) ||
424
224094
        ret->Set(context, first, second).IsEmpty()) {
425
      return v8::MaybeLocal<v8::Value>();
426
    }
427
  }
428
429
4394
  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
128239
SlicedArguments::SlicedArguments(
459
128239
    const v8::FunctionCallbackInfo<v8::Value>& args, size_t start) {
460
128239
  const size_t length = static_cast<size_t>(args.Length());
461
256478
  if (start >= length) return;
462
127999
  const size_t size = length - start;
463
464
127999
  AllocateSufficientStorage(size);
465
463797
  for (size_t i = 0; i < size; ++i)
466
671596
    (*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_