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: 118 173 68.2 %
Date: 2019-02-01 22:03:38 Branches: 63 190 33.2 %

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

616
  while (IsEmpty() == false)
99
    head_.next_->Remove();
100
308
}
101
102
template <typename T, ListNode<T> (T::*M)>
103
8458
void ListHead<T, M>::PushBack(T* element) {
104
8458
  ListNode<T>* that = &(element->*M);
105
8458
  head_.prev_->next_ = that;
106
8458
  that->prev_ = head_.prev_;
107
8458
  that->next_ = &head_;
108
8458
  head_.prev_ = that;
109
8458
}
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
616
bool ListHead<T, M>::IsEmpty() const {
122
616
  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
616
typename ListHead<T, M>::Iterator ListHead<T, M>::begin() const {
136
616
  return Iterator(head_.next_);
137
}
138
139
template <typename T, ListNode<T> (T::*M)>
140
616
typename ListHead<T, M>::Iterator ListHead<T, M>::end() const {
141
616
  return Iterator(const_cast<ListNode<T>*>(&head_));
142
}
143
144
template <typename Inner, typename Outer>
145
139136
constexpr uintptr_t OffsetOf(Inner Outer::*field) {
146
139136
  return reinterpret_cast<uintptr_t>(&(static_cast<Outer*>(0)->*field));
147
}
148
149
template <typename Inner, typename Outer>
150
137660
ContainerOfHelper<Inner, Outer>::ContainerOfHelper(Inner Outer::*field,
151
                                                   Inner* pointer)
152
    : pointer_(
153
        reinterpret_cast<Outer*>(
154
137660
            reinterpret_cast<uintptr_t>(pointer) - OffsetOf(field))) {}
155
156
template <typename Inner, typename Outer>
157
template <typename TypeName>
158
137660
ContainerOfHelper<Inner, Outer>::operator TypeName*() const {
159
137660
  return static_cast<TypeName*>(pointer_);
160
}
161
162
template <typename Inner, typename Outer>
163
137660
constexpr ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
164
                                                      Inner* pointer) {
165
137660
  return ContainerOfHelper<Inner, Outer>(field, pointer);
166
}
167
168
151762
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
303524
                                    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
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
                                    length).ToLocalChecked();
193
}
194
195
void SwapBytes16(char* data, size_t nbytes) {
196
  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
  for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
213
    memcpy(&temp, &data[i], sizeof(temp));
214
    temp = BSWAP_2(temp);
215
    memcpy(&data[i], &temp, sizeof(temp));
216
  }
217
}
218
219
void SwapBytes32(char* data, size_t nbytes) {
220
  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
  for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
237
    memcpy(&temp, &data[i], sizeof(temp));
238
    temp = BSWAP_4(temp);
239
    memcpy(&data[i], &temp, sizeof(temp));
240
  }
241
}
242
243
void SwapBytes64(char* data, size_t nbytes) {
244
  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
  for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
261
    memcpy(&temp, &data[i], sizeof(temp));
262
    temp = BSWAP_8(temp);
263
    memcpy(&data[i], &temp, sizeof(temp));
264
  }
265
}
266
267
11124368
char ToLower(char c) {
268

11124368
  return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c;
269
}
270
271
std::string ToLower(const std::string& in) {
272
  std::string out(in.size(), 0);
273
  for (size_t i = 0; i < in.size(); ++i)
274
    out[i] = ToLower(in[i]);
275
  return out;
276
}
277
278
5959710
bool StringEqualNoCase(const char* a, const char* b) {
279
5562184
  do {
280
5959710
    if (*a == '\0')
281
397526
      return *b == '\0';
282
5562184
    if (*b == '\0')
283
      return *a == '\0';
284
5562184
  } while (ToLower(*a++) == ToLower(*b++));
285
4367698
  return false;
286
}
287
288
bool StringEqualNoCaseN(const char* a, const char* b, size_t length) {
289
  for (size_t i = 0; i < length; i++) {
290
    if (ToLower(a[i]) != ToLower(b[i]))
291
      return false;
292
    if (a[i] == '\0')
293
      return true;
294
  }
295
  return true;
296
}
297
298
template <typename T>
299
1208891
inline T MultiplyWithOverflowCheck(T a, T b) {
300
1208891
  auto ret = a * b;
301
1208891
  if (a != 0)
302
1208891
    CHECK_EQ(b, ret / a);
303
304
1208891
  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
1204823
T* UncheckedRealloc(T* pointer, size_t n) {
316
1204823
  size_t full_size = MultiplyWithOverflowCheck(sizeof(T), n);
317
318


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


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


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


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




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






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







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


19516
    if (!ToV8Value(context, item.first, isolate).ToLocal(&first) ||
423
8364
        !ToV8Value(context, item.second, isolate).ToLocal(&second) ||
424
8364
        ret->Set(context, first, second).IsEmpty()) {
425
      return v8::MaybeLocal<v8::Value>();
426
    }
427
  }
428
429
164
  return handle_scope.Escape(ret);
430
}
431
432
template <typename T, typename >
433
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
434
                                    const T& number,
435
                                    v8::Isolate* isolate) {
436
  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
  if (static_cast<uint32_t>(Limits::max()) <=
442
          std::numeric_limits<uint32_t>::max() &&
443
      static_cast<uint32_t>(Limits::min()) >=
444
          std::numeric_limits<uint32_t>::min() && Limits::is_exact) {
445
    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
137
SlicedArguments::SlicedArguments(
459
137
    const v8::FunctionCallbackInfo<v8::Value>& args, size_t start) {
460
137
  const size_t length = static_cast<size_t>(args.Length());
461
274
  if (start >= length) return;
462
137
  const size_t size = length - start;
463
464
137
  AllocateSufficientStorage(size);
465
275
  for (size_t i = 0; i < size; ++i)
466
276
    (*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_