/Users/deen/code/yugabyte-db/src/yb/util/ref_cnt_buffer.cc
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 | | #include <glog/logging.h> |
17 | | |
18 | | #include "yb/util/ref_cnt_buffer.h" |
19 | | |
20 | | #include "yb/util/faststring.h" |
21 | | #include "yb/util/malloc.h" |
22 | | |
23 | | namespace yb { |
24 | | |
25 | | RefCntBuffer::RefCntBuffer() |
26 | 88.8M | : data_(nullptr) { |
27 | 88.8M | } |
28 | | |
29 | 123M | size_t RefCntBuffer::GetInternalBufSize(size_t data_size) { |
30 | 123M | return data_size + sizeof(CounterType) + sizeof(size_t); |
31 | 123M | } |
32 | | |
33 | 87.3M | RefCntBuffer::RefCntBuffer(size_t size) { |
34 | 87.3M | data_ = malloc_with_check(GetInternalBufSize(size)); |
35 | 87.3M | size_reference() = size; |
36 | 87.3M | new (&counter_reference()) CounterType(1); |
37 | 87.3M | } |
38 | | |
39 | 19.2M | RefCntBuffer::RefCntBuffer(const char* data, size_t size) { |
40 | 19.2M | data_ = malloc_with_check(GetInternalBufSize(size)); |
41 | 19.2M | memcpy(this->data(), data, size); |
42 | 19.2M | size_reference() = size; |
43 | 19.2M | new (&counter_reference()) CounterType(1); |
44 | 19.2M | } |
45 | | |
46 | | RefCntBuffer::RefCntBuffer(const faststring& str) |
47 | 4.60M | : RefCntBuffer(str.data(), str.size()) { |
48 | 4.60M | } Unexecuted instantiation: _ZN2yb12RefCntBufferC2ERKNS_10faststringE _ZN2yb12RefCntBufferC1ERKNS_10faststringE Line | Count | Source | 47 | 4.60M | : RefCntBuffer(str.data(), str.size()) { | 48 | 4.60M | } |
|
49 | | |
50 | 534M | RefCntBuffer::~RefCntBuffer() { |
51 | 534M | Reset(); |
52 | 534M | } |
53 | | |
54 | | RefCntBuffer::RefCntBuffer(const RefCntBuffer& rhs) noexcept |
55 | 147M | : data_(rhs.data_) { |
56 | 147M | if (data_) |
57 | 147M | ++counter_reference(); |
58 | 147M | } |
59 | | |
60 | | RefCntBuffer::RefCntBuffer(RefCntBuffer&& rhs) noexcept |
61 | 192M | : data_(rhs.data_) { |
62 | 192M | rhs.data_ = nullptr; |
63 | 192M | } |
64 | | |
65 | 17.5M | void RefCntBuffer::operator=(const RefCntBuffer& rhs) noexcept { |
66 | 17.5M | if (rhs.data_) { |
67 | 17.5M | ++rhs.counter_reference(); |
68 | 17.5M | } |
69 | 17.5M | DoReset(rhs.data_); |
70 | 17.5M | } |
71 | | |
72 | 181M | void RefCntBuffer::operator=(RefCntBuffer&& rhs) noexcept { |
73 | 181M | DoReset(rhs.data_); |
74 | 181M | rhs.data_ = nullptr; |
75 | 181M | } |
76 | | |
77 | 734M | void RefCntBuffer::DoReset(char* data) { |
78 | 734M | if (data_ != nullptr) { |
79 | 271M | if (--counter_reference() == 0) { |
80 | 106M | counter_reference().~CounterType(); |
81 | 106M | free(data_); |
82 | 106M | } |
83 | 271M | } |
84 | 734M | data_ = data; |
85 | 734M | } |
86 | | |
87 | 0 | std::string RefCntPrefix::ShortDebugString() const { |
88 | 0 | return Slice(data(), size()).ToDebugHexString(); |
89 | 0 | } |
90 | | |
91 | 31.8M | void RefCntPrefix::Resize(size_t value) { |
92 | 31.8M | DCHECK_LE(value, bytes_.size()); |
93 | 31.8M | size_ = value; |
94 | 31.8M | } |
95 | | |
96 | | } // namespace yb |