/Users/deen/code/yugabyte-db/src/yb/gutil/ref_counted_memory.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 | | // Use of this source code is governed by a BSD-style license that can be |
3 | | // found in the LICENSE file. |
4 | | // |
5 | | // The following only applies to changes made to this file as part of YugaByte development. |
6 | | // |
7 | | // Portions Copyright (c) YugaByte, Inc. |
8 | | // |
9 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
10 | | // in compliance with the License. You may obtain a copy of the License at |
11 | | // |
12 | | // http://www.apache.org/licenses/LICENSE-2.0 |
13 | | // |
14 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
15 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
16 | | // or implied. See the License for the specific language governing permissions and limitations |
17 | | // under the License. |
18 | | // |
19 | | |
20 | | #include "yb/gutil/ref_counted_memory.h" |
21 | | |
22 | | |
23 | | #include <glog/logging.h> |
24 | | |
25 | | namespace yb { |
26 | | |
27 | | bool RefCountedMemory::Equals( |
28 | 0 | const scoped_refptr<RefCountedMemory>& other) const { |
29 | 0 | return other.get() && |
30 | 0 | size() == other->size() && |
31 | 0 | (memcmp(front(), other->front(), size()) == 0); |
32 | 0 | } |
33 | | |
34 | 361 | RefCountedMemory::RefCountedMemory() {} |
35 | | |
36 | 360 | RefCountedMemory::~RefCountedMemory() {} |
37 | | |
38 | 0 | const unsigned char* RefCountedStaticMemory::front() const { |
39 | 0 | return data_; |
40 | 0 | } |
41 | | |
42 | 0 | size_t RefCountedStaticMemory::size() const { |
43 | 0 | return length_; |
44 | 0 | } |
45 | | |
46 | 0 | RefCountedStaticMemory::~RefCountedStaticMemory() {} |
47 | | |
48 | 0 | RefCountedBytes::RefCountedBytes() {} |
49 | | |
50 | | RefCountedBytes::RefCountedBytes(std::vector<unsigned char> initializer) |
51 | 0 | : data_(std::move(initializer)) {} |
52 | | |
53 | | RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size) |
54 | 0 | : data_(p, p + size) {} |
55 | | |
56 | | RefCountedBytes* RefCountedBytes::TakeVector( |
57 | 0 | std::vector<unsigned char>* to_destroy) { |
58 | 0 | auto bytes = new RefCountedBytes; |
59 | 0 | bytes->data_.swap(*to_destroy); |
60 | 0 | return bytes; |
61 | 0 | } |
62 | | |
63 | 0 | const unsigned char* RefCountedBytes::front() const { |
64 | | // STL will assert if we do front() on an empty vector, but calling code |
65 | | // expects a NULL. |
66 | 0 | return size() ? &data_.front() : nullptr; |
67 | 0 | } |
68 | | |
69 | 0 | size_t RefCountedBytes::size() const { |
70 | 0 | return data_.size(); |
71 | 0 | } |
72 | | |
73 | 0 | RefCountedBytes::~RefCountedBytes() {} |
74 | | |
75 | 361 | RefCountedString::RefCountedString() {} |
76 | | |
77 | 360 | RefCountedString::~RefCountedString() {} |
78 | | |
79 | | // static |
80 | 0 | RefCountedString* RefCountedString::TakeString(std::string* to_destroy) { |
81 | 0 | auto self = new RefCountedString; |
82 | 0 | to_destroy->swap(self->data_); |
83 | 0 | return self; |
84 | 0 | } |
85 | | |
86 | 0 | const unsigned char* RefCountedString::front() const { |
87 | 0 | return data_.empty() ? nullptr : |
88 | 0 | reinterpret_cast<const unsigned char*>(data_.data()); |
89 | 0 | } |
90 | | |
91 | 0 | size_t RefCountedString::size() const { |
92 | 0 | return data_.size(); |
93 | 0 | } |
94 | | |
95 | | RefCountedMallocedMemory::RefCountedMallocedMemory( |
96 | | void* data, size_t length) |
97 | 0 | : data_(reinterpret_cast<unsigned char*>(data)), length_(length) { |
98 | 0 | DCHECK(data || length == 0); |
99 | 0 | } |
100 | | |
101 | 0 | const unsigned char* RefCountedMallocedMemory::front() const { |
102 | 0 | return length_ ? data_ : nullptr; |
103 | 0 | } |
104 | | |
105 | 0 | size_t RefCountedMallocedMemory::size() const { |
106 | 0 | return length_; |
107 | 0 | } |
108 | | |
109 | 0 | RefCountedMallocedMemory::~RefCountedMallocedMemory() { |
110 | 0 | free(data_); |
111 | 0 | } |
112 | | |
113 | | } // namespace yb |