YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/ref_cnt_buffer.h
Line
Count
Source (jump to first uncovered line)
1
//
2
// Copyright (c) YugaByte, Inc.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
// in compliance with the License.  You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software distributed under the License
10
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
// or implied.  See the License for the specific language governing permissions and limitations
12
// under the License.
13
//
14
//
15
16
#ifndef YB_UTIL_REF_CNT_BUFFER_H
17
#define YB_UTIL_REF_CNT_BUFFER_H
18
19
#include <stdlib.h>
20
#include <string.h>
21
22
#include <atomic>
23
#include <string>
24
25
#include "yb/util/slice.h"
26
27
namespace yb {
28
29
class faststring;
30
31
// Byte buffer with reference counting. It embeds reference count, size and data in a single block.
32
class RefCntBuffer {
33
 public:
34
  RefCntBuffer();
35
  explicit RefCntBuffer(size_t size);
36
  RefCntBuffer(const char *data, size_t size);
37
38
  RefCntBuffer(const char *data, const char *end)
39
10.0k
      : RefCntBuffer(data, end - data) {}
Unexecuted instantiation: _ZN2yb12RefCntBufferC2EPKcS2_
_ZN2yb12RefCntBufferC1EPKcS2_
Line
Count
Source
39
10.0k
      : RefCntBuffer(data, end - data) {}
40
41
  RefCntBuffer(const uint8_t *data, size_t size)
42
19.1M
      : RefCntBuffer(static_cast<const char*>(static_cast<const void*>(data)), size) {}
Unexecuted instantiation: _ZN2yb12RefCntBufferC2EPKhm
_ZN2yb12RefCntBufferC1EPKhm
Line
Count
Source
42
19.1M
      : RefCntBuffer(static_cast<const char*>(static_cast<const void*>(data)), size) {}
43
44
  explicit RefCntBuffer(const std::string& str) :
45
30
      RefCntBuffer(str.c_str(), str.length()) {}
Unexecuted instantiation: _ZN2yb12RefCntBufferC2ERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE
_ZN2yb12RefCntBufferC1ERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE
Line
Count
Source
45
30
      RefCntBuffer(str.c_str(), str.length()) {}
46
47
  explicit RefCntBuffer(const faststring& str);
48
49
  explicit RefCntBuffer(const Slice& slice) :
50
14.6M
      RefCntBuffer(slice.data(), slice.size()) {}
Unexecuted instantiation: _ZN2yb12RefCntBufferC2ERKNS_5SliceE
_ZN2yb12RefCntBufferC1ERKNS_5SliceE
Line
Count
Source
50
14.6M
      RefCntBuffer(slice.data(), slice.size()) {}
51
52
  RefCntBuffer(const RefCntBuffer& rhs) noexcept;
53
  RefCntBuffer(RefCntBuffer&& rhs) noexcept;
54
55
  void operator=(const RefCntBuffer& rhs) noexcept;
56
  void operator=(RefCntBuffer&& rhs) noexcept;
57
58
  ~RefCntBuffer();
59
60
642M
  size_t size() const {
61
642M
    return size_reference();
62
642M
  }
63
64
61.2M
  size_t DynamicMemoryUsage() const { return data_ ? GetInternalBufSize(size()) : 0; }
65
66
35.2M
  bool empty() const {
67
35.2M
    return size() == 0;
68
35.2M
  }
69
70
872M
  char* data() const {
71
872M
    return data_ + sizeof(CounterType) + sizeof(size_t);
72
872M
  }
73
74
81.9M
  char* begin() const {
75
81.9M
    return data();
76
81.9M
  }
77
78
52.5k
  char* end() const {
79
52.5k
    return begin() + size();
80
52.5k
  }
81
82
168M
  uint8_t* udata() const {
83
168M
    return static_cast<unsigned char*>(static_cast<void*>(data()));
84
168M
  }
85
86
5.12M
  uint8_t* ubegin() const {
87
5.12M
    return udata();
88
5.12M
  }
89
90
5.23M
  uint8_t* uend() const {
91
5.23M
    return udata() + size();
92
5.23M
  }
93
94
535M
  void Reset() { DoReset(nullptr); }
95
96
13.6M
  explicit operator bool() const {
97
13.6M
    return data_ != nullptr;
98
13.6M
  }
99
100
36.7M
  bool operator!() const {
101
36.7M
    return data_ == nullptr;
102
36.7M
  }
103
104
2.56k
  std::string ToBuffer() const {
105
2.56k
    return std::string(begin(), end());
106
2.56k
  }
107
108
1.54M
  Slice AsSlice() const {
109
1.54M
    return Slice(data(), size());
110
1.54M
  }
111
112
2.53k
  Slice as_slice() const __attribute__ ((deprecated)) {
113
2.53k
    return Slice(data(), size());
114
2.53k
  }
115
116
1.82M
  void Shrink(size_t new_size) {
117
1.82M
    size_reference() = new_size;
118
1.82M
  }
119
120
 private:
121
  void DoReset(char* data);
122
123
  static size_t GetInternalBufSize(size_t data_size);
124
125
  // Using ptrdiff_t since it matches register size and is signed.
126
  typedef std::atomic<std::ptrdiff_t> CounterType;
127
128
750M
  size_t& size_reference() const {
129
750M
    return *static_cast<size_t*>(static_cast<void*>(data_ + sizeof(CounterType)));
130
750M
  }
131
132
648M
  CounterType& counter_reference() const {
133
648M
    return *static_cast<CounterType*>(static_cast<void*>(data_));
134
648M
  }
135
136
  char *data_;
137
};
138
139
struct RefCntBufferHash {
140
0
  size_t operator()(const RefCntBuffer& inp) const {
141
0
    return inp.as_slice().hash();
142
0
  }
143
};
144
145
class RefCntPrefix {
146
 public:
147
9.87M
  RefCntPrefix() : size_(0) {}
148
149
  explicit RefCntPrefix(const std::string& str)
150
0
      : bytes_(RefCntBuffer(str)), size_(bytes_.size()) {}
151
152
  explicit RefCntPrefix(const Slice& slice)
153
9.46M
      : bytes_(RefCntBuffer(slice)), size_(bytes_.size()) {}
154
155
  RefCntPrefix(RefCntBuffer bytes) // NOLINT
156
7.96M
      : bytes_(std::move(bytes)), size_(bytes_.size()) {}
157
158
  RefCntPrefix(RefCntBuffer bytes, size_t size)
159
0
      : bytes_(std::move(bytes)), size_(size) {}
160
161
  RefCntPrefix(const RefCntPrefix& doc_key, size_t size)
162
0
      : bytes_(doc_key.bytes_), size_(size) {}
163
164
13.5M
  explicit operator bool() const {
165
13.5M
    return static_cast<bool>(bytes_);
166
13.5M
  }
167
168
  void Resize(size_t value);
169
170
66.3M
  Slice as_slice() const {
171
66.3M
    return Slice(bytes_.data(), size_);
172
66.3M
  }
173
174
101M
  const char* data() const {
175
101M
    return bytes_.data();
176
101M
  }
177
178
27.2M
  size_t size() const {
179
27.2M
    return size_;
180
27.2M
  }
181
182
145M
  int Compare(const RefCntPrefix& rhs) const {
183
145M
    auto my_size = size_;
184
145M
    auto rhs_size = rhs.size_;
185
145M
    const size_t min_len = std::min(my_size, rhs_size);
186
145M
    int r = strings::fastmemcmp_inlined(bytes_.data(), rhs.bytes_.data(), min_len);
187
145M
    if (r == 0) {
188
50.5M
      if (my_size < rhs_size) { return -1; }
189
33.0M
      if (my_size > rhs_size) { return 1; }
190
111M
    }
191
111M
    return r;
192
111M
  }
193
194
  std::string ShortDebugString() const;
195
196
 private:
197
  RefCntBuffer bytes_;
198
  size_t size_;
199
200
145M
  friend inline bool operator<(const RefCntPrefix& lhs, const RefCntPrefix& rhs) {
201
145M
    return lhs.Compare(rhs) < 0;
202
145M
  }
203
204
38.2M
  friend inline bool operator==(const RefCntPrefix& lhs, const RefCntPrefix& rhs) {
205
38.2M
    return lhs.size_ == rhs.size_ && strings::memeq(lhs.data(), rhs.data(), lhs.size_);
206
38.2M
  }
207
};
208
209
struct RefCntPrefixHash {
210
28.2M
  size_t operator()(const RefCntPrefix& inp) const {
211
28.2M
    return inp.as_slice().hash();
212
28.2M
  }
213
};
214
215
} // namespace yb
216
217
#endif // YB_UTIL_REF_CNT_BUFFER_H