GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/util-inl.h Lines: 161 175 92.0 %
Date: 2019-01-07 12:15:22 Branches: 124 212 58.5 %

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

13354
  while (IsEmpty() == false)
99
    head_.next_->Remove();
100
6677
}
101
102
template <typename T, ListNode<T> (T::*M)>
103
190066
void ListHead<T, M>::PushBack(T* element) {
104
190066
  ListNode<T>* that = &(element->*M);
105
190066
  head_.prev_->next_ = that;
106
190066
  that->prev_ = head_.prev_;
107
190066
  that->next_ = &head_;
108
190066
  head_.prev_ = that;
109
190066
}
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
13476
bool ListHead<T, M>::IsEmpty() const {
122
13476
  return head_.IsEmpty();
123
}
124
125
template <typename T, ListNode<T> (T::*M)>
126
2
T* ListHead<T, M>::PopFront() {
127
2
  if (IsEmpty())
128
    return nullptr;
129
2
  ListNode<T>* node = head_.next_;
130
2
  node->Remove();
131
2
  return ContainerOf(M, node);
132
}
133
134
template <typename T, ListNode<T> (T::*M)>
135
13596
typename ListHead<T, M>::Iterator ListHead<T, M>::begin() const {
136
13596
  return Iterator(head_.next_);
137
}
138
139
template <typename T, ListNode<T> (T::*M)>
140
13600
typename ListHead<T, M>::Iterator ListHead<T, M>::end() const {
141
13600
  return Iterator(const_cast<ListNode<T>*>(&head_));
142
}
143
144
template <typename Inner, typename Outer>
145
1574795
constexpr uintptr_t OffsetOf(Inner Outer::*field) {
146
1574795
  return reinterpret_cast<uintptr_t>(&(static_cast<Outer*>(0)->*field));
147
}
148
149
template <typename Inner, typename Outer>
150
1542424
ContainerOfHelper<Inner, Outer>::ContainerOfHelper(Inner Outer::*field,
151
                                                   Inner* pointer)
152
    : pointer_(
153
        reinterpret_cast<Outer*>(
154
1542424
            reinterpret_cast<uintptr_t>(pointer) - OffsetOf(field))) {}
155
156
template <typename Inner, typename Outer>
157
template <typename TypeName>
158
1542416
ContainerOfHelper<Inner, Outer>::operator TypeName*() const {
159
1542416
  return static_cast<TypeName*>(pointer_);
160
}
161
162
template <typename Inner, typename Outer>
163
1542425
constexpr ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
164
                                                      Inner* pointer) {
165
1542425
  return ContainerOfHelper<Inner, Outer>(field, pointer);
166
}
167
168
2794877
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
5589758
                                    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
110
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
220
                                    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
91663
char ToLower(char c) {
268

91663
  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
46727
bool StringEqualNoCase(const char* a, const char* b) {
279
43482
  do {
280
46727
    if (*a == '\0')
281
3243
      return *b == '\0';
282
43484
    if (*b == '\0')
283
2
      return *a == '\0';
284
43482
  } while (ToLower(*a++) == ToLower(*b++));
285
32574
  return false;
286
}
287
288
545
bool StringEqualNoCaseN(const char* a, const char* b, size_t length) {
289
2267
  for (size_t i = 0; i < length; i++) {
290
2108
    if (ToLower(a[i]) != ToLower(b[i]))
291
385
      return false;
292
1723
    if (a[i] == '\0')
293
1
      return true;
294
  }
295
159
  return true;
296
}
297
298
template <typename T>
299
1000513
inline T MultiplyWithOverflowCheck(T a, T b) {
300
1000513
  auto ret = a * b;
301
1000513
  if (a != 0)
302
1000511
    CHECK_EQ(b, ret / a);
303
304
1000513
  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
916619
T* UncheckedRealloc(T* pointer, size_t n) {
316
916619
  size_t full_size = MultiplyWithOverflowCheck(sizeof(T), n);
317
318


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


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


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




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




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






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












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


436849
    if (!ToV8Value(context, item.first, isolate).ToLocal(&first) ||
423
187221
        !ToV8Value(context, item.second, isolate).ToLocal(&second) ||
424
187221
        ret->Set(context, first, second).IsEmpty()) {
425
      return v8::MaybeLocal<v8::Value>();
426
    }
427
  }
428
429
3671
  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
}  // namespace node
459
460
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
461
462
#endif  // SRC_UTIL_INL_H_