/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 | 281M | : data_(nullptr) { |
27 | 281M | } |
28 | | |
29 | 463M | size_t RefCntBuffer::GetInternalBufSize(size_t data_size) { |
30 | 463M | return data_size + sizeof(CounterType) + sizeof(size_t); |
31 | 463M | } |
32 | | |
33 | 332M | RefCntBuffer::RefCntBuffer(size_t size) { |
34 | 332M | data_ = malloc_with_check(GetInternalBufSize(size)); |
35 | 332M | size_reference() = size; |
36 | 332M | new (&counter_reference()) CounterType(1); |
37 | 332M | } |
38 | | |
39 | 47.7M | RefCntBuffer::RefCntBuffer(const char* data, size_t size) { |
40 | 47.7M | data_ = malloc_with_check(GetInternalBufSize(size)); |
41 | 47.7M | memcpy(this->data(), data, size); |
42 | 47.7M | size_reference() = size; |
43 | 47.7M | new (&counter_reference()) CounterType(1); |
44 | 47.7M | } |
45 | | |
46 | | RefCntBuffer::RefCntBuffer(const faststring& str) |
47 | 9.10M | : RefCntBuffer(str.data(), str.size()) { |
48 | 9.10M | } Unexecuted instantiation: yb::RefCntBuffer::RefCntBuffer(yb::faststring const&) yb::RefCntBuffer::RefCntBuffer(yb::faststring const&) Line | Count | Source | 47 | 9.10M | : RefCntBuffer(str.data(), str.size()) { | 48 | 9.10M | } |
|
49 | | |
50 | 1.84G | RefCntBuffer::~RefCntBuffer() { |
51 | 1.84G | Reset(); |
52 | 1.84G | } |
53 | | |
54 | | RefCntBuffer::RefCntBuffer(const RefCntBuffer& rhs) noexcept |
55 | 497M | : data_(rhs.data_) { |
56 | 497M | if (data_) |
57 | 497M | ++counter_reference(); |
58 | 497M | } |
59 | | |
60 | | RefCntBuffer::RefCntBuffer(RefCntBuffer&& rhs) noexcept |
61 | 687M | : data_(rhs.data_) { |
62 | 687M | rhs.data_ = nullptr; |
63 | 687M | } |
64 | | |
65 | 49.8M | void RefCntBuffer::operator=(const RefCntBuffer& rhs) noexcept { |
66 | 49.8M | if (rhs.data_49.8M ) { |
67 | 49.8M | ++rhs.counter_reference(); |
68 | 49.8M | } |
69 | 49.8M | DoReset(rhs.data_); |
70 | 49.8M | } |
71 | | |
72 | 686M | void RefCntBuffer::operator=(RefCntBuffer&& rhs) noexcept { |
73 | 686M | DoReset(rhs.data_); |
74 | 686M | rhs.data_ = nullptr; |
75 | 686M | } |
76 | | |
77 | 2.58G | void RefCntBuffer::DoReset(char* data) { |
78 | 2.58G | if (data_ != nullptr) { |
79 | 926M | if (--counter_reference() == 0) { |
80 | 380M | counter_reference().~CounterType(); |
81 | 380M | free(data_); |
82 | 380M | } |
83 | 926M | } |
84 | 2.58G | data_ = data; |
85 | 2.58G | } |
86 | | |
87 | 0 | std::string RefCntPrefix::ShortDebugString() const { |
88 | 0 | return Slice(data(), size()).ToDebugHexString(); |
89 | 0 | } |
90 | | |
91 | 109M | void RefCntPrefix::Resize(size_t value) { |
92 | 109M | DCHECK_LE(value, bytes_.size()); |
93 | 109M | size_ = value; |
94 | 109M | } |
95 | | |
96 | | } // namespace yb |