YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/gutil/ref_counted_memory.h
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
#ifndef YB_GUTIL_REF_COUNTED_MEMORY_H_
21
#define YB_GUTIL_REF_COUNTED_MEMORY_H_
22
23
#include <string>
24
#include <vector>
25
26
#include "yb/gutil/macros.h"
27
#include "yb/gutil/ref_counted.h"
28
#include "yb/gutil/port.h"
29
30
#ifndef BASE_EXPORT
31
#define BASE_EXPORT
32
#endif
33
34
namespace yb {
35
36
// A generic interface to memory. This object is reference counted because one
37
// of its two subclasses own the data they carry, and we need to have
38
// heterogeneous containers of these two types of memory.
39
class BASE_EXPORT RefCountedMemory
40
    : public RefCountedThreadSafe<RefCountedMemory> {
41
 public:
42
  // Retrieves a pointer to the beginning of the data we point to. If the data
43
  // is empty, this will return NULL.
44
  virtual const unsigned char* front() const = 0;
45
46
  // Size of the memory pointed to.
47
  virtual size_t size() const = 0;
48
49
  // Returns true if |other| is byte for byte equal.
50
  bool Equals(const scoped_refptr<RefCountedMemory>& other) const;
51
52
  // Handy method to simplify calling front() with a reinterpret_cast.
53
  template<typename T> const T* front_as() const {
54
    return reinterpret_cast<const T*>(front());
55
  }
56
57
 protected:
58
  friend class RefCountedThreadSafe<RefCountedMemory>;
59
  RefCountedMemory();
60
  virtual ~RefCountedMemory();
61
};
62
63
// An implementation of RefCountedMemory, where the ref counting does not
64
// matter.
65
class BASE_EXPORT RefCountedStaticMemory : public RefCountedMemory {
66
 public:
67
  RefCountedStaticMemory()
68
0
      : data_(NULL), length_(0) {}
69
  RefCountedStaticMemory(const void* data, size_t length)
70
      : data_(static_cast<const unsigned char*>(length ? data : NULL)),
71
0
        length_(length) {}
72
73
  // Overridden from RefCountedMemory:
74
  virtual const unsigned char* front() const override;
75
  virtual size_t size() const override;
76
77
 private:
78
  virtual ~RefCountedStaticMemory();
79
80
  const unsigned char* data_;
81
  size_t length_;
82
83
  DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory);
84
};
85
86
// An implementation of RefCountedMemory, where we own the data in a vector.
87
class BASE_EXPORT RefCountedBytes : public RefCountedMemory {
88
 public:
89
  RefCountedBytes();
90
91
  // Constructs a RefCountedBytes object by _copying_ from |initializer|.
92
  explicit RefCountedBytes(std::vector<unsigned char> initializer);
93
94
  // Constructs a RefCountedBytes object by copying |size| bytes from |p|.
95
  RefCountedBytes(const unsigned char* p, size_t size);
96
97
  // Constructs a RefCountedBytes object by performing a swap. (To non
98
  // destructively build a RefCountedBytes, use the constructor that takes a
99
  // vector.)
100
  static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy);
101
102
  // Overridden from RefCountedMemory:
103
  virtual const unsigned char* front() const override;
104
  virtual size_t size() const override;
105
106
0
  const std::vector<unsigned char>& data() const { return data_; }
107
0
  std::vector<unsigned char>& data() { return data_; }
108
109
 private:
110
  virtual ~RefCountedBytes();
111
112
  std::vector<unsigned char> data_;
113
114
  DISALLOW_COPY_AND_ASSIGN(RefCountedBytes);
115
};
116
117
// An implementation of RefCountedMemory, where the bytes are stored in an STL
118
// string. Use this if your data naturally arrives in that format.
119
class BASE_EXPORT RefCountedString : public RefCountedMemory {
120
 public:
121
  RefCountedString();
122
123
  // Constructs a RefCountedString object by performing a swap. (To non
124
  // destructively build a RefCountedString, use the default constructor and
125
  // copy into object->data()).
126
  static RefCountedString* TakeString(std::string* to_destroy);
127
128
  // Overridden from RefCountedMemory:
129
  virtual const unsigned char* front() const override;
130
  virtual size_t size() const override;
131
132
0
  const std::string& data() const { return data_; }
133
628k
  std::string& data() { return data_; }
134
135
 private:
136
  virtual ~RefCountedString();
137
138
  std::string data_;
139
140
  DISALLOW_COPY_AND_ASSIGN(RefCountedString);
141
};
142
143
// An implementation of RefCountedMemory that holds a chunk of memory
144
// previously allocated with malloc or calloc, and that therefore must be freed
145
// using free().
146
class BASE_EXPORT RefCountedMallocedMemory : public RefCountedMemory {
147
 public:
148
  RefCountedMallocedMemory(void* data, size_t length);
149
150
  // Overridden from RefCountedMemory:
151
  virtual const unsigned char* front() const override;
152
  virtual size_t size() const override;
153
154
 private:
155
  virtual ~RefCountedMallocedMemory();
156
157
  unsigned char* data_;
158
  size_t length_;
159
160
  DISALLOW_COPY_AND_ASSIGN(RefCountedMallocedMemory);
161
};
162
163
}  // namespace yb
164
165
#endif // YB_GUTIL_REF_COUNTED_MEMORY_H_