YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/byte_buffer.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#ifndef YB_UTIL_BYTE_BUFFER_H
15
#define YB_UTIL_BYTE_BUFFER_H
16
17
#include <cstddef>
18
#include <string>
19
20
#include "yb/util/slice.h"
21
22
namespace yb {
23
24
// Utility class to store arbitrary amount of byte with inplace buffer of specified size.
25
template <size_t SmallLen>
26
class ByteBuffer {
27
 public:
28
  static_assert(SmallLen >= sizeof(void*), "Too small buffer");
29
30
252M
  ByteBuffer() : size_(0) {}
_ZN2yb10ByteBufferILm64EEC2Ev
Line
Count
Source
30
252M
  ByteBuffer() : size_(0) {}
_ZN2yb10ByteBufferILm8EEC2Ev
Line
Count
Source
30
1.00k
  ByteBuffer() : size_(0) {}
Unexecuted instantiation: _ZN2yb10ByteBufferILm16EEC2Ev
Unexecuted instantiation: _ZN2yb10ByteBufferILm32EEC2Ev
31
32
24.8k
  explicit ByteBuffer(const std::string& str) {
33
24.8k
    Assign(str.c_str(), str.size());
34
24.8k
  }
_ZN2yb10ByteBufferILm64EEC2ERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE
Line
Count
Source
32
24.8k
  explicit ByteBuffer(const std::string& str) {
33
24.8k
    Assign(str.c_str(), str.size());
34
24.8k
  }
_ZN2yb10ByteBufferILm8EEC2ERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE
Line
Count
Source
32
13
  explicit ByteBuffer(const std::string& str) {
33
13
    Assign(str.c_str(), str.size());
34
13
  }
35
36
  void operator=(const std::string& str) {
37
    Assign(str.c_str(), str.size());
38
  }
39
40
271M
  explicit ByteBuffer(const Slice& slice) {
41
271M
    Assign(slice.cdata(), slice.cend());
42
271M
  }
43
44
0
  void operator=(const Slice& slice) {
45
0
    Assign(slice.cdata(), slice.cend());
46
0
  }
47
48
357M
  ByteBuffer(const ByteBuffer<SmallLen>& rhs) {
49
357M
    Assign(rhs.ptr(), rhs.size_);
50
357M
  }
_ZN2yb10ByteBufferILm64EEC2ERKS1_
Line
Count
Source
48
357M
  ByteBuffer(const ByteBuffer<SmallLen>& rhs) {
49
357M
    Assign(rhs.ptr(), rhs.size_);
50
357M
  }
_ZN2yb10ByteBufferILm8EEC2ERKS1_
Line
Count
Source
48
1
  ByteBuffer(const ByteBuffer<SmallLen>& rhs) {
49
1
    Assign(rhs.ptr(), rhs.size_);
50
1
  }
51
52
25.3M
  void operator=(const ByteBuffer<SmallLen>& rhs) {
53
25.3M
    Assign(rhs.ptr(), rhs.size_);
54
25.3M
  }
_ZN2yb10ByteBufferILm64EEaSERKS1_
Line
Count
Source
52
25.3M
  void operator=(const ByteBuffer<SmallLen>& rhs) {
53
25.3M
    Assign(rhs.ptr(), rhs.size_);
54
25.3M
  }
_ZN2yb10ByteBufferILm8EEaSERKS1_
Line
Count
Source
52
1
  void operator=(const ByteBuffer<SmallLen>& rhs) {
53
1
    Assign(rhs.ptr(), rhs.size_);
54
1
  }
55
56
16.1M
  ByteBuffer(ByteBuffer<SmallLen>&& rhs) {
57
16.1M
    if (!rhs.big()) {
58
16.1M
      memcpy(small_buffer_, rhs.small_buffer_, rhs.size_);
59
18.4E
    } else {
60
18.4E
      capacity_ = rhs.capacity_;
61
18.4E
      big_buffer_ = rhs.big_buffer_;
62
18.4E
      rhs.capacity_ = SmallLen;
63
18.4E
    }
64
65
16.1M
    size_ = rhs.size_;
66
16.1M
    rhs.size_ = 0;
67
16.1M
  }
_ZN2yb10ByteBufferILm64EEC2EOS1_
Line
Count
Source
56
16.1M
  ByteBuffer(ByteBuffer<SmallLen>&& rhs) {
57
16.1M
    if (!rhs.big()) {
58
16.1M
      memcpy(small_buffer_, rhs.small_buffer_, rhs.size_);
59
18.4E
    } else {
60
18.4E
      capacity_ = rhs.capacity_;
61
18.4E
      big_buffer_ = rhs.big_buffer_;
62
18.4E
      rhs.capacity_ = SmallLen;
63
18.4E
    }
64
65
16.1M
    size_ = rhs.size_;
66
16.1M
    rhs.size_ = 0;
67
16.1M
  }
_ZN2yb10ByteBufferILm8EEC2EOS1_
Line
Count
Source
56
7
  ByteBuffer(ByteBuffer<SmallLen>&& rhs) {
57
7
    if (!rhs.big()) {
58
6
      memcpy(small_buffer_, rhs.small_buffer_, rhs.size_);
59
1
    } else {
60
1
      capacity_ = rhs.capacity_;
61
1
      big_buffer_ = rhs.big_buffer_;
62
1
      rhs.capacity_ = SmallLen;
63
1
    }
64
65
7
    size_ = rhs.size_;
66
7
    rhs.size_ = 0;
67
7
  }
68
69
9.03M
  void operator=(ByteBuffer<SmallLen>&& rhs) {
70
9.03M
    if (!rhs.big()) {
71
9.01M
      memcpy(ptr(), rhs.small_buffer_, rhs.size_);
72
23.1k
    } else {
73
23.1k
      if (big()) {
74
1
        free(big_buffer_);
75
1
      }
76
23.1k
      capacity_ = rhs.capacity_;
77
23.1k
      big_buffer_ = rhs.big_buffer_;
78
23.1k
      rhs.capacity_ = SmallLen;
79
23.1k
    }
80
81
9.03M
    size_ = rhs.size_;
82
9.03M
    rhs.size_ = 0;
83
9.03M
  }
_ZN2yb10ByteBufferILm64EEaSEOS1_
Line
Count
Source
69
9.03M
  void operator=(ByteBuffer<SmallLen>&& rhs) {
70
9.03M
    if (!rhs.big()) {
71
9.01M
      memcpy(ptr(), rhs.small_buffer_, rhs.size_);
72
23.1k
    } else {
73
23.1k
      if (big()) {
74
0
        free(big_buffer_);
75
0
      }
76
23.1k
      capacity_ = rhs.capacity_;
77
23.1k
      big_buffer_ = rhs.big_buffer_;
78
23.1k
      rhs.capacity_ = SmallLen;
79
23.1k
    }
80
81
9.03M
    size_ = rhs.size_;
82
9.03M
    rhs.size_ = 0;
83
9.03M
  }
_ZN2yb10ByteBufferILm8EEaSEOS1_
Line
Count
Source
69
1
  void operator=(ByteBuffer<SmallLen>&& rhs) {
70
1
    if (!rhs.big()) {
71
0
      memcpy(ptr(), rhs.small_buffer_, rhs.size_);
72
1
    } else {
73
1
      if (big()) {
74
1
        free(big_buffer_);
75
1
      }
76
1
      capacity_ = rhs.capacity_;
77
1
      big_buffer_ = rhs.big_buffer_;
78
1
      rhs.capacity_ = SmallLen;
79
1
    }
80
81
1
    size_ = rhs.size_;
82
1
    rhs.size_ = 0;
83
1
  }
84
85
897M
  ~ByteBuffer() {
86
897M
    if (big()) {
87
57.1M
      free(big_buffer_);
88
57.1M
    }
89
897M
  }
_ZN2yb10ByteBufferILm64EED2Ev
Line
Count
Source
85
897M
  ~ByteBuffer() {
86
897M
    if (big()) {
87
57.1M
      free(big_buffer_);
88
57.1M
    }
89
897M
  }
_ZN2yb10ByteBufferILm8EED2Ev
Line
Count
Source
85
1.02k
  ~ByteBuffer() {
86
1.02k
    if (big()) {
87
1.00k
      free(big_buffer_);
88
1.00k
    }
89
1.02k
  }
Unexecuted instantiation: _ZN2yb10ByteBufferILm16EED2Ev
Unexecuted instantiation: _ZN2yb10ByteBufferILm32EED2Ev
90
91
3.83G
  bool empty() const {
92
3.83G
    return size_ == 0;
93
3.83G
  }
_ZNK2yb10ByteBufferILm8EE5emptyEv
Line
Count
Source
91
2
  bool empty() const {
92
2
    return size_ == 0;
93
2
  }
_ZNK2yb10ByteBufferILm64EE5emptyEv
Line
Count
Source
91
3.83G
  bool empty() const {
92
3.83G
    return size_ == 0;
93
3.83G
  }
94
95
1.27G
  size_t size() const {
96
1.27G
    return size_;
97
1.27G
  }
_ZNK2yb10ByteBufferILm64EE4sizeEv
Line
Count
Source
95
1.27G
  size_t size() const {
96
1.27G
    return size_;
97
1.27G
  }
_ZNK2yb10ByteBufferILm8EE4sizeEv
Line
Count
Source
95
25.5k
  size_t size() const {
96
25.5k
    return size_;
97
25.5k
  }
98
99
151M
  void Clear() {
100
151M
    size_ = 0;
101
151M
  }
Unexecuted instantiation: _ZN2yb10ByteBufferILm8EE5ClearEv
Unexecuted instantiation: _ZN2yb10ByteBufferILm16EE5ClearEv
Unexecuted instantiation: _ZN2yb10ByteBufferILm32EE5ClearEv
_ZN2yb10ByteBufferILm64EE5ClearEv
Line
Count
Source
99
151M
  void Clear() {
100
151M
    size_ = 0;
101
151M
  }
102
103
47.3M
  char& Back() {
104
47.3M
    return ptr()[size_ - 1];
105
47.3M
  }
106
107
  char Back() const {
108
    return ptr()[size_ - 1];
109
  }
110
111
259
  char& operator[](size_t len) {
112
259
    return ptr()[len];
113
259
  }
114
115
  char operator[](size_t len) const {
116
    return ptr()[len];
117
  }
118
119
98.5M
  void PopBack() {
120
98.5M
    --size_;
121
98.5M
  }
122
123
563M
  void Truncate(size_t new_size) {
124
563M
    size_ = new_size;
125
563M
  }
126
127
1
  void Assign(const Slice& slice) {
128
1
    Assign(slice.cdata(), slice.cend());
129
1
  }
130
131
271M
  void Assign(const char* a, const char* b) {
132
271M
    Assign(a, b - a);
133
271M
  }
_ZN2yb10ByteBufferILm8EE6AssignEPKcS3_
Line
Count
Source
131
1
  void Assign(const char* a, const char* b) {
132
1
    Assign(a, b - a);
133
1
  }
_ZN2yb10ByteBufferILm64EE6AssignEPKcS3_
Line
Count
Source
131
271M
  void Assign(const char* a, const char* b) {
132
271M
    Assign(a, b - a);
133
271M
  }
134
135
758M
  void Assign(const char* a, size_t size) {
136
758M
    DoAppend(0, a, size);
137
758M
  }
_ZN2yb10ByteBufferILm64EE6AssignEPKcm
Line
Count
Source
135
758M
  void Assign(const char* a, size_t size) {
136
758M
    DoAppend(0, a, size);
137
758M
  }
_ZN2yb10ByteBufferILm8EE6AssignEPKcm
Line
Count
Source
135
16
  void Assign(const char* a, size_t size) {
136
16
    DoAppend(0, a, size);
137
16
  }
138
139
606M
  void Append(const Slice& slice) {
140
606M
    Append(slice.cdata(), slice.cend());
141
606M
  }
_ZN2yb10ByteBufferILm8EE6AppendERKNS_5SliceE
Line
Count
Source
139
5.74k
  void Append(const Slice& slice) {
140
5.74k
    Append(slice.cdata(), slice.cend());
141
5.74k
  }
_ZN2yb10ByteBufferILm64EE6AppendERKNS_5SliceE
Line
Count
Source
139
606M
  void Append(const Slice& slice) {
140
606M
    Append(slice.cdata(), slice.cend());
141
606M
  }
142
143
  template <size_t OtherSmallLen>
144
0
  void Append(const ByteBuffer<OtherSmallLen>& rhs) {
145
0
    Append(rhs.ptr(), rhs.size_);
146
0
  }
147
148
609M
  void Append(const char* a, const char* b) {
149
609M
    Append(a, b - a);
150
609M
  }
_ZN2yb10ByteBufferILm8EE6AppendEPKcS3_
Line
Count
Source
148
5.74k
  void Append(const char* a, const char* b) {
149
5.74k
    Append(a, b - a);
150
5.74k
  }
Unexecuted instantiation: _ZN2yb10ByteBufferILm16EE6AppendEPKcS3_
Unexecuted instantiation: _ZN2yb10ByteBufferILm32EE6AppendEPKcS3_
_ZN2yb10ByteBufferILm64EE6AppendEPKcS3_
Line
Count
Source
148
609M
  void Append(const char* a, const char* b) {
149
609M
    Append(a, b - a);
150
609M
  }
151
152
1.07G
  void Append(const char* a, size_t size) {
153
1.07G
    DoAppend(size_, a, size);
154
1.07G
  }
_ZN2yb10ByteBufferILm8EE6AppendEPKcm
Line
Count
Source
152
5.74k
  void Append(const char* a, size_t size) {
153
5.74k
    DoAppend(size_, a, size);
154
5.74k
  }
Unexecuted instantiation: _ZN2yb10ByteBufferILm16EE6AppendEPKcm
Unexecuted instantiation: _ZN2yb10ByteBufferILm32EE6AppendEPKcm
_ZN2yb10ByteBufferILm64EE6AppendEPKcm
Line
Count
Source
152
1.07G
  void Append(const char* a, size_t size) {
153
1.07G
    DoAppend(size_, a, size);
154
1.07G
  }
155
156
422M
  void Reserve(size_t capacity) {
157
422M
    EnsureCapacity(capacity, size_);
158
422M
  }
159
160
1.03G
  void PushBack(char ch) {
161
1.03G
    *(EnsureCapacity(size_ + 1, size_) + size_) = ch;
162
1.03G
    ++size_;
163
1.03G
  }
_ZN2yb10ByteBufferILm8EE8PushBackEc
Line
Count
Source
160
756
  void PushBack(char ch) {
161
756
    *(EnsureCapacity(size_ + 1, size_) + size_) = ch;
162
756
    ++size_;
163
756
  }
_ZN2yb10ByteBufferILm64EE8PushBackEc
Line
Count
Source
160
1.03G
  void PushBack(char ch) {
161
1.03G
    *(EnsureCapacity(size_ + 1, size_) + size_) = ch;
162
1.03G
    ++size_;
163
1.03G
  }
164
165
25.7M
  std::string ToStringBuffer() const {
166
25.7M
    return AsSlice().ToBuffer();
167
25.7M
  }
_ZNK2yb10ByteBufferILm64EE14ToStringBufferEv
Line
Count
Source
165
25.7M
  std::string ToStringBuffer() const {
166
25.7M
    return AsSlice().ToBuffer();
167
25.7M
  }
Unexecuted instantiation: _ZNK2yb10ByteBufferILm8EE14ToStringBufferEv
Unexecuted instantiation: _ZNK2yb10ByteBufferILm16EE14ToStringBufferEv
Unexecuted instantiation: _ZNK2yb10ByteBufferILm32EE14ToStringBufferEv
168
169
0
  std::string ToString() const {
170
0
    return AsSlice().ToDebugHexString();
171
0
  }
172
173
2.80G
  Slice AsSlice() const {
174
2.80G
    return Slice(ptr(), size_);
175
2.80G
  }
_ZNK2yb10ByteBufferILm64EE7AsSliceEv
Line
Count
Source
173
2.80G
  Slice AsSlice() const {
174
2.80G
    return Slice(ptr(), size_);
175
2.80G
  }
_ZNK2yb10ByteBufferILm8EE7AsSliceEv
Line
Count
Source
173
6.55k
  Slice AsSlice() const {
174
6.55k
    return Slice(ptr(), size_);
175
6.55k
  }
Unexecuted instantiation: _ZNK2yb10ByteBufferILm16EE7AsSliceEv
Unexecuted instantiation: _ZNK2yb10ByteBufferILm32EE7AsSliceEv
176
177
  // STL container compatibility
178
151M
  void clear() {
179
151M
    Clear();
180
151M
  }
Unexecuted instantiation: _ZN2yb10ByteBufferILm8EE5clearEv
Unexecuted instantiation: _ZN2yb10ByteBufferILm16EE5clearEv
Unexecuted instantiation: _ZN2yb10ByteBufferILm32EE5clearEv
_ZN2yb10ByteBufferILm64EE5clearEv
Line
Count
Source
178
151M
  void clear() {
179
151M
    Clear();
180
151M
  }
181
182
  template <class... Args>
183
1.07G
  void append(Args&&... args) {
184
1.07G
    Append(std::forward<Args>(args)...);
185
1.07G
  }
Unexecuted instantiation: _ZN2yb10ByteBufferILm64EE6appendIJRKS1_EEEvDpOT_
_ZN2yb10ByteBufferILm64EE6appendIJRA8_cmEEEvDpOT_
Line
Count
Source
183
2.72M
  void append(Args&&... args) {
184
2.72M
    Append(std::forward<Args>(args)...);
185
2.72M
  }
_ZN2yb10ByteBufferILm8EE6appendIJNS_5SliceEEEEvDpOT_
Line
Count
Source
183
5.74k
  void append(Args&&... args) {
184
5.74k
    Append(std::forward<Args>(args)...);
185
5.74k
  }
_ZN2yb10ByteBufferILm64EE6appendIJPKcmEEEvDpOT_
Line
Count
Source
183
45.9M
  void append(Args&&... args) {
184
45.9M
    Append(std::forward<Args>(args)...);
185
45.9M
  }
_ZN2yb10ByteBufferILm64EE6appendIJRPKcRmEEEvDpOT_
Line
Count
Source
183
64.5M
  void append(Args&&... args) {
184
64.5M
    Append(std::forward<Args>(args)...);
185
64.5M
  }
_ZN2yb10ByteBufferILm64EE6appendIJRKNS_5SliceEEEEvDpOT_
Line
Count
Source
183
591M
  void append(Args&&... args) {
184
591M
    Append(std::forward<Args>(args)...);
185
591M
  }
_ZN2yb10ByteBufferILm64EE6appendIJRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEEEEvDpOT_
Line
Count
Source
183
14.8M
  void append(Args&&... args) {
184
14.8M
    Append(std::forward<Args>(args)...);
185
14.8M
  }
_ZN2yb10ByteBufferILm64EE6appendIJRA4_cmEEEvDpOT_
Line
Count
Source
183
45.2M
  void append(Args&&... args) {
184
45.2M
    Append(std::forward<Args>(args)...);
185
45.2M
  }
_ZN2yb10ByteBufferILm64EE6appendIJRA30_cPcEEEvDpOT_
Line
Count
Source
183
2.76M
  void append(Args&&... args) {
184
2.76M
    Append(std::forward<Args>(args)...);
185
2.76M
  }
_ZN2yb10ByteBufferILm64EE6appendIJRA16_cRmEEEvDpOT_
Line
Count
Source
183
287M
  void append(Args&&... args) {
184
287M
    Append(std::forward<Args>(args)...);
185
287M
  }
_ZN2yb10ByteBufferILm64EE6appendIJRA2_cmEEEvDpOT_
Line
Count
Source
183
21.6M
  void append(Args&&... args) {
184
21.6M
    Append(std::forward<Args>(args)...);
185
21.6M
  }
186
187
  template <class... Args>
188
105M
  void assign(Args&&... args) {
189
105M
    Assign(std::forward<Args>(args)...);
190
105M
  }
Unexecuted instantiation: _ZN2yb10ByteBufferILm64EE6assignIJRPKcRmEEEvDpOT_
_ZN2yb10ByteBufferILm64EE6assignIJPKcmEEEvDpOT_
Line
Count
Source
188
105M
  void assign(Args&&... args) {
189
105M
    Assign(std::forward<Args>(args)...);
190
105M
  }
191
192
422M
  void reserve(size_t capacity) {
193
422M
    Reserve(capacity);
194
422M
  }
195
196
1.03G
  void push_back(char ch) {
197
1.03G
    PushBack(ch);
198
1.03G
  }
199
200
47.3M
  char& back() {
201
47.3M
    return Back();
202
47.3M
  }
203
204
  char back() const {
205
    return Back();
206
  }
207
208
98.5M
  void pop_back() {
209
98.5M
    PopBack();
210
98.5M
  }
211
212
 private:
213
1.82G
  void DoAppend(size_t keep_size, const char* a, size_t len) {
214
1.82G
    size_t new_size = keep_size + len;
215
1.82G
    memcpy(EnsureCapacity(new_size, keep_size) + keep_size, a, len);
216
1.82G
    size_ = new_size;
217
1.82G
  }
_ZN2yb10ByteBufferILm64EE8DoAppendEmPKcm
Line
Count
Source
213
1.82G
  void DoAppend(size_t keep_size, const char* a, size_t len) {
214
1.82G
    size_t new_size = keep_size + len;
215
1.82G
    memcpy(EnsureCapacity(new_size, keep_size) + keep_size, a, len);
216
1.82G
    size_ = new_size;
217
1.82G
  }
_ZN2yb10ByteBufferILm8EE8DoAppendEmPKcm
Line
Count
Source
213
5.76k
  void DoAppend(size_t keep_size, const char* a, size_t len) {
214
5.76k
    size_t new_size = keep_size + len;
215
5.76k
    memcpy(EnsureCapacity(new_size, keep_size) + keep_size, a, len);
216
5.76k
    size_ = new_size;
217
5.76k
  }
Unexecuted instantiation: _ZN2yb10ByteBufferILm16EE8DoAppendEmPKcm
Unexecuted instantiation: _ZN2yb10ByteBufferILm32EE8DoAppendEmPKcm
218
219
  // Ensures that buffer could contain at least capacity bytes.
220
  // In case of relocation, keep_size bytes will be copied.
221
3.27G
  char* EnsureCapacity(size_t capacity, size_t keep_size) {
222
3.27G
    if (capacity <= capacity_) {
223
3.21G
      return ptr();
224
3.21G
    }
225
226
55.9M
    bool was_big = big();
227
91.5M
    while ((capacity_ <<= 1ULL) < capacity) {}
228
55.9M
    char* new_buffer = static_cast<char*>(malloc(capacity_));
229
55.9M
    char*& big_buffer = big_buffer_;
230
55.9M
    if (was_big) {
231
68.4k
      memcpy(new_buffer, big_buffer, keep_size);
232
68.4k
      free(big_buffer);
233
55.8M
    } else {
234
55.8M
      memcpy(new_buffer, small_buffer_, keep_size);
235
55.8M
    }
236
55.9M
    return big_buffer = new_buffer;
237
55.9M
  }
_ZN2yb10ByteBufferILm64EE14EnsureCapacityEmm
Line
Count
Source
221
3.27G
  char* EnsureCapacity(size_t capacity, size_t keep_size) {
222
3.27G
    if (capacity <= capacity_) {
223
3.21G
      return ptr();
224
3.21G
    }
225
226
55.9M
    bool was_big = big();
227
91.5M
    while ((capacity_ <<= 1ULL) < capacity) {}
228
55.9M
    char* new_buffer = static_cast<char*>(malloc(capacity_));
229
55.9M
    char*& big_buffer = big_buffer_;
230
55.9M
    if (was_big) {
231
67.9k
      memcpy(new_buffer, big_buffer, keep_size);
232
67.9k
      free(big_buffer);
233
55.8M
    } else {
234
55.8M
      memcpy(new_buffer, small_buffer_, keep_size);
235
55.8M
    }
236
55.9M
    return big_buffer = new_buffer;
237
55.9M
  }
_ZN2yb10ByteBufferILm8EE14EnsureCapacityEmm
Line
Count
Source
221
6.52k
  char* EnsureCapacity(size_t capacity, size_t keep_size) {
222
6.52k
    if (capacity <= capacity_) {
223
4.99k
      return ptr();
224
4.99k
    }
225
226
1.52k
    bool was_big = big();
227
4.00k
    while ((capacity_ <<= 1ULL) < capacity) {}
228
1.52k
    char* new_buffer = static_cast<char*>(malloc(capacity_));
229
1.52k
    char*& big_buffer = big_buffer_;
230
1.52k
    if (was_big) {
231
518
      memcpy(new_buffer, big_buffer, keep_size);
232
518
      free(big_buffer);
233
1.00k
    } else {
234
1.00k
      memcpy(new_buffer, small_buffer_, keep_size);
235
1.00k
    }
236
1.52k
    return big_buffer = new_buffer;
237
1.52k
  }
Unexecuted instantiation: _ZN2yb10ByteBufferILm16EE14EnsureCapacityEmm
Unexecuted instantiation: _ZN2yb10ByteBufferILm32EE14EnsureCapacityEmm
238
239
7.39G
  bool big() const {
240
7.39G
    return capacity_ > SmallLen;
241
7.39G
  }
_ZNK2yb10ByteBufferILm64EE3bigEv
Line
Count
Source
239
7.39G
  bool big() const {
240
7.39G
    return capacity_ > SmallLen;
241
7.39G
  }
_ZNK2yb10ByteBufferILm8EE3bigEv
Line
Count
Source
239
14.1k
  bool big() const {
240
14.1k
    return capacity_ > SmallLen;
241
14.1k
  }
Unexecuted instantiation: _ZNK2yb10ByteBufferILm16EE3bigEv
Unexecuted instantiation: _ZNK2yb10ByteBufferILm32EE3bigEv
242
243
3.27G
  char* ptr() {
244
1.82G
    return !big() ? small_buffer_ : big_buffer_;
245
3.27G
  }
_ZN2yb10ByteBufferILm64EE3ptrEv
Line
Count
Source
243
3.27G
  char* ptr() {
244
1.82G
    return !big() ? small_buffer_ : big_buffer_;
245
3.27G
  }
_ZN2yb10ByteBufferILm8EE3ptrEv
Line
Count
Source
243
4.99k
  char* ptr() {
244
4.90k
    return !big() ? small_buffer_ : big_buffer_;
245
4.99k
  }
Unexecuted instantiation: _ZN2yb10ByteBufferILm16EE3ptrEv
Unexecuted instantiation: _ZN2yb10ByteBufferILm32EE3ptrEv
246
247
3.18G
  const char* ptr() const {
248
2.05G
    return !big() ? small_buffer_ : big_buffer_;
249
3.18G
  }
_ZNK2yb10ByteBufferILm64EE3ptrEv
Line
Count
Source
247
3.18G
  const char* ptr() const {
248
2.05G
    return !big() ? small_buffer_ : big_buffer_;
249
3.18G
  }
_ZNK2yb10ByteBufferILm8EE3ptrEv
Line
Count
Source
247
6.55k
  const char* ptr() const {
248
6.42k
    return !big() ? small_buffer_ : big_buffer_;
249
6.55k
  }
Unexecuted instantiation: _ZNK2yb10ByteBufferILm16EE3ptrEv
Unexecuted instantiation: _ZNK2yb10ByteBufferILm32EE3ptrEv
250
251
  size_t capacity_ = SmallLen;
252
  size_t size_;
253
  union {
254
    char small_buffer_[SmallLen];
255
    char* big_buffer_;
256
  };
257
};
258
259
template <size_t SmallLenLhs, size_t SmallLenRhs>
260
280M
bool operator<(const ByteBuffer<SmallLenLhs>& lhs, const ByteBuffer<SmallLenRhs>& rhs) {
261
280M
  return lhs.AsSlice().compare(rhs.AsSlice()) < 0;
262
280M
}
_ZN2ybltILm64ELm64EEEbRKNS_10ByteBufferIXT_EEERKNS1_IXT0_EEE
Line
Count
Source
260
280M
bool operator<(const ByteBuffer<SmallLenLhs>& lhs, const ByteBuffer<SmallLenRhs>& rhs) {
261
280M
  return lhs.AsSlice().compare(rhs.AsSlice()) < 0;
262
280M
}
_ZN2ybltILm8ELm8EEEbRKNS_10ByteBufferIXT_EEERKNS1_IXT0_EEE
Line
Count
Source
260
14
bool operator<(const ByteBuffer<SmallLenLhs>& lhs, const ByteBuffer<SmallLenRhs>& rhs) {
261
14
  return lhs.AsSlice().compare(rhs.AsSlice()) < 0;
262
14
}
263
264
template <size_t SmallLenLhs, size_t SmallLenRhs>
265
61.7M
bool operator==(const ByteBuffer<SmallLenLhs>& lhs, const ByteBuffer<SmallLenRhs>& rhs) {
266
61.7M
  return lhs.AsSlice() == rhs.AsSlice();
267
61.7M
}
_ZN2ybeqILm8ELm8EEEbRKNS_10ByteBufferIXT_EEERKNS1_IXT0_EEE
Line
Count
Source
265
9
bool operator==(const ByteBuffer<SmallLenLhs>& lhs, const ByteBuffer<SmallLenRhs>& rhs) {
266
9
  return lhs.AsSlice() == rhs.AsSlice();
267
9
}
_ZN2ybeqILm64ELm64EEEbRKNS_10ByteBufferIXT_EEERKNS1_IXT0_EEE
Line
Count
Source
265
61.7M
bool operator==(const ByteBuffer<SmallLenLhs>& lhs, const ByteBuffer<SmallLenRhs>& rhs) {
266
61.7M
  return lhs.AsSlice() == rhs.AsSlice();
267
61.7M
}
268
269
struct ByteBufferHash {
270
  typedef std::size_t result_type;
271
272
  template <size_t SmallLen>
273
61.4M
  result_type operator()(const ByteBuffer<SmallLen>& buffer) const {
274
61.4M
    return buffer.AsSlice().hash();
275
61.4M
  }
_ZNK2yb14ByteBufferHashclILm8EEEmRKNS_10ByteBufferIXT_EEE
Line
Count
Source
273
6
  result_type operator()(const ByteBuffer<SmallLen>& buffer) const {
274
6
    return buffer.AsSlice().hash();
275
6
  }
_ZNK2yb14ByteBufferHashclILm64EEEmRKNS_10ByteBufferIXT_EEE
Line
Count
Source
273
61.4M
  result_type operator()(const ByteBuffer<SmallLen>& buffer) const {
274
61.4M
    return buffer.AsSlice().hash();
275
61.4M
  }
276
};
277
278
} // namespace yb
279
280
#endif // YB_UTIL_BYTE_BUFFER_H