GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/src/util-inl.h Lines: 64 74 86.5 %
Date: 2016-07-12 Branches: 72 138 52.2 %

Line Exec Source
1
#ifndef SRC_UTIL_INL_H_
2
#define SRC_UTIL_INL_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "util.h"
7
8
namespace node {
9
10
template <typename T>
11
60883
ListNode<T>::ListNode() : prev_(this), next_(this) {}
12
13
template <typename T>
14
ListNode<T>::~ListNode() {
15
57214
  Remove();
16
57214
}
17
18
template <typename T>
19
void ListNode<T>::Remove() {
20
65627
  prev_->next_ = next_;
21
65627
  next_->prev_ = prev_;
22
64791
  prev_ = this;
23
64791
  next_ = this;
24
}
25
26
template <typename T>
27
bool ListNode<T>::IsEmpty() const {
28
25598
  return prev_ == this;
29
}
30
31
template <typename T, ListNodeMember(T) M>
32
ListHead<T, M>::Iterator::Iterator(ListNode<T>* node) : node_(node) {}
33
34
template <typename T, ListNodeMember(T) M>
35
T* ListHead<T, M>::Iterator::operator*() const {
36
58
  return ContainerOf(M, node_);
37
}
38
39
template <typename T, ListNodeMember(T) M>
40
const typename ListHead<T, M>::Iterator&
41
ListHead<T, M>::Iterator::operator++() {
42
29
  node_ = node_->next_;
43
  return *this;
44
}
45
46
template <typename T, ListNodeMember(T) M>
47
bool ListHead<T, M>::Iterator::operator!=(const Iterator& that) const {
48
  return node_ != that.node_;
49
}
50
51
template <typename T, ListNodeMember(T) M>
52
ListHead<T, M>::~ListHead() {
53
7484
  while (IsEmpty() == false)
54
786
    head_.next_->Remove();
55
13396
}
56
57
template <typename T, ListNodeMember(T) M>
58
void ListHead<T, M>::MoveBack(ListHead* that) {
59
1674
  if (IsEmpty())
60
    return;
61
1674
  ListNode<T>* to = &that->head_;
62
1674
  head_.next_->prev_ = to->prev_;
63
1674
  to->prev_->next_ = head_.next_;
64
1674
  head_.prev_->next_ = to;
65
1674
  to->prev_ = head_.prev_;
66
1674
  head_.prev_ = &head_;
67
1674
  head_.next_ = &head_;
68
}
69
70
template <typename T, ListNodeMember(T) M>
71
void ListHead<T, M>::PushBack(T* element) {
72
52645
  ListNode<T>* that = &(element->*M);
73
52645
  head_.prev_->next_ = that;
74
52645
  that->prev_ = head_.prev_;
75
52645
  that->next_ = &head_;
76
52645
  head_.prev_ = that;
77
}
78
79
template <typename T, ListNodeMember(T) M>
80
void ListHead<T, M>::PushFront(T* element) {
81
  ListNode<T>* that = &(element->*M);
82
  head_.next_->prev_ = that;
83
  that->prev_ = &head_;
84
  that->next_ = head_.next_;
85
  head_.next_ = that;
86
}
87
88
template <typename T, ListNodeMember(T) M>
89
bool ListHead<T, M>::IsEmpty() const {
90
52032
  return head_.IsEmpty();
91
}
92
93
template <typename T, ListNodeMember(T) M>
94
T* ListHead<T, M>::PopFront() {
95
11188
  if (IsEmpty())
96
    return nullptr;
97
7627
  ListNode<T>* node = head_.next_;
98
7627
  node->Remove();
99
7627
  return ContainerOf(M, node);
100
}
101
102
template <typename T, ListNodeMember(T) M>
103
typename ListHead<T, M>::Iterator ListHead<T, M>::begin() const {
104
2
  return Iterator(head_.next_);
105
}
106
107
template <typename T, ListNodeMember(T) M>
108
typename ListHead<T, M>::Iterator ListHead<T, M>::end() const {
109
2
  return Iterator(const_cast<ListNode<T>*>(&head_));
110
}
111
112
template <typename Inner, typename Outer>
113
ContainerOfHelper<Inner, Outer>::ContainerOfHelper(Inner Outer::*field,
114
                                                   Inner* pointer)
115
    : pointer_(reinterpret_cast<Outer*>(
116
          reinterpret_cast<uintptr_t>(pointer) -
117
139193
          reinterpret_cast<uintptr_t>(&(static_cast<Outer*>(0)->*field)))) {
118
}
119
120
template <typename Inner, typename Outer>
121
template <typename TypeName>
122
ContainerOfHelper<Inner, Outer>::operator TypeName*() const {
123
  return static_cast<TypeName*>(pointer_);
124
}
125
126
template <typename Inner, typename Outer>
127
inline ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
128
                                                   Inner* pointer) {
129
139198
  return ContainerOfHelper<Inner, Outer>(field, pointer);
130
}
131
132
template <class TypeName>
133
520176
inline v8::Local<TypeName> PersistentToLocal(
134
    v8::Isolate* isolate,
135
    const v8::Persistent<TypeName>& persistent) {
136
1040352
  if (persistent.IsWeak()) {
137
    return WeakPersistentToLocal(isolate, persistent);
138
  } else {
139
    return StrongPersistentToLocal(persistent);
140
  }
141
}
142
143
template <class TypeName>
144
inline v8::Local<TypeName> StrongPersistentToLocal(
145
    const v8::Persistent<TypeName>& persistent) {
146
  return *reinterpret_cast<v8::Local<TypeName>*>(
147
1615981
      const_cast<v8::Persistent<TypeName>*>(&persistent));
148
}
149
150
template <class TypeName>
151
inline v8::Local<TypeName> WeakPersistentToLocal(
152
    v8::Isolate* isolate,
153
    const v8::Persistent<TypeName>& persistent) {
154
32308
  return v8::Local<TypeName>::New(isolate, persistent);
155
}
156
157
780140
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
158
                                           const char* data,
159
                                           int length) {
160
780140
  return v8::String::NewFromOneByte(isolate,
161
                                    reinterpret_cast<const uint8_t*>(data),
162
                                    v8::NewStringType::kNormal,
163
780140
                                    length).ToLocalChecked();
164
}
165
166
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
167
                                           const signed char* data,
168
                                           int length) {
169
  return v8::String::NewFromOneByte(isolate,
170
                                    reinterpret_cast<const uint8_t*>(data),
171
                                    v8::NewStringType::kNormal,
172
                                    length).ToLocalChecked();
173
}
174
175
286
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
176
                                           const unsigned char* data,
177
                                           int length) {
178
286
  return v8::String::NewFromOneByte(isolate,
179
                                    reinterpret_cast<const uint8_t*>(data),
180
                                    v8::NewStringType::kNormal,
181
286
                                    length).ToLocalChecked();
182
}
183
184
template <typename TypeName>
185
155786
void Wrap(v8::Local<v8::Object> object, TypeName* pointer) {
186
155786
  CHECK_EQ(false, object.IsEmpty());
187
155786
  CHECK_GT(object->InternalFieldCount(), 0);
188
155786
  object->SetAlignedPointerInInternalField(0, pointer);
189
155786
}
190
191
void ClearWrap(v8::Local<v8::Object> object) {
192
42957
  Wrap<void>(object, nullptr);
193
}
194
195
template <typename TypeName>
196
214555
TypeName* Unwrap(v8::Local<v8::Object> object) {
197
214555
  CHECK_EQ(false, object.IsEmpty());
198
214555
  CHECK_GT(object->InternalFieldCount(), 0);
199
429110
  void* pointer = object->GetAlignedPointerFromInternalField(0);
200
214555
  return static_cast<TypeName*>(pointer);
201
}
202
203
void SwapBytes(uint16_t* dst, const uint16_t* src, size_t buflen) {
204
  for (size_t i = 0; i < buflen; i += 1)
205
    dst[i] = (src[i] << 8) | (src[i] >> 8);
206
}
207
208
char ToLower(char c) {
209
69422
  return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c;
210
}
211
212
29564
bool StringEqualNoCase(const char* a, const char* b) {
213
34711
  do {
214
37208
    if (*a == '\0')
215
2496
      return *b == '\0';
216
34712
    if (*b == '\0')
217
1
      return *a == '\0';
218
104133
  } while (ToLower(*a++) == ToLower(*b++));
219
  return false;
220
}
221
222
bool StringEqualNoCaseN(const char* a, const char* b, size_t length) {
223
  for (size_t i = 0; i < length; i++) {
224
    if (ToLower(a[i]) != ToLower(b[i]))
225
      return false;
226
    if (a[i] == '\0')
227
      return true;
228
  }
229
  return true;
230
}
231
232
}  // namespace node
233
234
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
235
236
#endif  // SRC_UTIL_INL_H_