-
Notifications
You must be signed in to change notification settings - Fork 2
/
buffer.cpp
234 lines (196 loc) · 7.5 KB
/
buffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <cassert>
#include "memory_pool.h"
#include "buffer.h"
#include "bit_utils.h"
namespace pil {
int Buffer::Copy(const int64_t start,
const int64_t nbytes,
MemoryPool* pool,
std::shared_ptr<Buffer>* out) const
{
// Sanity checks
assert(start < size_);
assert(nbytes <= size_ - start);
//DCHECK_LT(start, size_);
//DCHECK_LE(nbytes, size_ - start);
std::shared_ptr<ResizableBuffer> new_buffer;
int ret = AllocateResizableBuffer(pool, nbytes, &new_buffer);
if(ret != 1) return(-1);
std::memcpy(new_buffer->mutable_data(), data() + start, static_cast<size_t>(nbytes));
*out = new_buffer;
//return Status::OK();
return 1;
}
int Buffer::Copy(const int64_t start,
const int64_t nbytes,
std::shared_ptr<Buffer>* out) const
{
return Copy(start, nbytes, default_memory_pool(), out);
}
bool Buffer::Equals(const Buffer& other, const int64_t nbytes) const {
return this == &other || (size_ >= nbytes && other.size_ >= nbytes
&& (data_ == other.data_
|| !memcmp(data_, other.data_, static_cast<size_t>(nbytes))));
}
bool Buffer::Equals(const Buffer& other) const {
return this == &other || (size_ == other.size_
&& (data_ == other.data_
|| !memcmp(data_, other.data_, static_cast<size_t>(size_))));
}
int Buffer::FromString(const std::string& data,
MemoryPool* pool,
std::shared_ptr<Buffer>* out)
{
auto size = static_cast<int64_t>(data.size());
int ret = AllocateBuffer(pool, size, out);
if(ret != 1) return(-1);
std::copy(data.c_str(), data.c_str() + size, (*out)->mutable_data());
return 1;
//return Status::OK();
}
int Buffer::FromString(const std::string& data, std::shared_ptr<Buffer>* out) {
return FromString(data, default_memory_pool(), out);
}
class StlStringBuffer : public Buffer {
public:
explicit StlStringBuffer(std::string&& data) :
Buffer(nullptr, 0), input_(data)
{
data_ = reinterpret_cast<const uint8_t*>(input_.c_str());
size_ = static_cast<int64_t>(input_.size());
capacity_ = size_;
}
private:
std::string input_;
};
std::shared_ptr<Buffer> Buffer::FromString(std::string&& data) {
return std::make_shared<StlStringBuffer>(std::move(data));
}
std::string Buffer::ToString() const {
return std::string(reinterpret_cast<const char*>(data_), static_cast<size_t>(size_));
}
void Buffer::CheckMutable() const { assert(is_mutable()); }
/// A Buffer whose lifetime is tied to a particular MemoryPool
class PoolBuffer : public ResizableBuffer {
public:
explicit PoolBuffer(MemoryPool* pool) :
ResizableBuffer(nullptr, 0)
{
if (pool == nullptr) {
pool = default_memory_pool();
}
pool_ = pool;
}
~PoolBuffer() override {
if (mutable_data_ != nullptr) {
pool_->Free(mutable_data_, capacity_);
}
}
int Reserve(const int64_t capacity) override {
if (!mutable_data_ || capacity > capacity_) {
uint8_t* new_data;
int64_t new_capacity = BitUtils::RoundUpToMultipleOf64(capacity);
if (mutable_data_) {
int ret = pool_->Reallocate(capacity_, new_capacity, &mutable_data_);
if(ret != 1) return(-1);
} else {
int ret = pool_->Allocate(new_capacity, &new_data);
if(ret != 1) return(-1);
mutable_data_ = new_data;
}
data_ = mutable_data_;
capacity_ = new_capacity;
}
//return Status::OK();
return 1;
}
int Resize(const int64_t new_size, bool shrink_to_fit = true) override {
if (mutable_data_ && shrink_to_fit && new_size <= size_) {
// Buffer is non-null and is not growing, so shrink to the requested size without
// excess space.
int64_t new_capacity = BitUtils::RoundUpToMultipleOf64(new_size);
if (capacity_ != new_capacity) {
// Buffer hasn't got yet the requested size.
int ret = pool_->Reallocate(capacity_, new_capacity, &mutable_data_);
if(ret != 1) return(-1);
data_ = mutable_data_;
capacity_ = new_capacity;
}
} else {
int ret = Reserve(new_size);
if(ret != 1) return(-1);
}
size_ = new_size;
//return Status::OK();
return(1);
}
private:
MemoryPool* pool_;
};
std::shared_ptr<Buffer> SliceMutableBuffer(const std::shared_ptr<Buffer>& buffer,
const int64_t offset,
const int64_t length)
{
return std::make_shared<MutableBuffer>(buffer, offset, length);
}
MutableBuffer::MutableBuffer(const std::shared_ptr<Buffer>& parent,
const int64_t offset,
const int64_t size) :
MutableBuffer(parent->mutable_data() + offset, size)
{
//DCHECK(parent->is_mutable()) << "Must pass mutable buffer";
assert(parent->is_mutable());
parent_ = parent;
}
namespace {
// A utility that does most of the work of the `AllocateBuffer` and
// `AllocateResizableBuffer` methods. The argument `buffer` should be a smart pointer to a
// PoolBuffer.
template <typename PoolBufferPtr, typename BufferPtr>
inline int ResizePoolBuffer(PoolBufferPtr&& buffer,
const int64_t size,
BufferPtr* out)
{
int ret = buffer->Resize(size);
if(ret != 1) return(-1);
buffer->ZeroPadding();
*out = std::move(buffer);
return(1);
//return Status::OK();
}
} // namespace
int AllocateBuffer(MemoryPool* pool, const int64_t size, std::shared_ptr<Buffer>* out) {
return ResizePoolBuffer(std::make_shared<PoolBuffer>(pool), size, out);
}
int AllocateBuffer(MemoryPool* pool, const int64_t size, std::unique_ptr<Buffer>* out) {
return ResizePoolBuffer(std::unique_ptr<PoolBuffer>(new PoolBuffer(pool)), size, out);
}
int AllocateBuffer(const int64_t size, std::shared_ptr<Buffer>* out) {
return AllocateBuffer(default_memory_pool(), size, out);
}
int AllocateBuffer(const int64_t size, std::unique_ptr<Buffer>* out) {
return AllocateBuffer(default_memory_pool(), size, out);
}
int AllocateResizableBuffer(MemoryPool* pool, const int64_t size, std::shared_ptr<ResizableBuffer>* out) {
return ResizePoolBuffer(std::make_shared<PoolBuffer>(pool), size, out);
}
int AllocateResizableBuffer(MemoryPool* pool, const int64_t size, std::unique_ptr<ResizableBuffer>* out) {
return ResizePoolBuffer(std::unique_ptr<PoolBuffer>(new PoolBuffer(pool)), size, out);
}
int AllocateResizableBuffer(const int64_t size, std::shared_ptr<ResizableBuffer>* out) {
return AllocateResizableBuffer(default_memory_pool(), size, out);
}
int AllocateResizableBuffer(const int64_t size, std::unique_ptr<ResizableBuffer>* out) {
return AllocateResizableBuffer(default_memory_pool(), size, out);
}
int AllocateEmptyBitmap(MemoryPool* pool, int64_t length, std::shared_ptr<Buffer>* out) {
int ret = AllocateBuffer(pool, BitUtils::BytesForBits(length), out);
if(ret != 1) return(-1);
memset((*out)->mutable_data(), 0, static_cast<size_t>((*out)->size()));
return 1;
//return Status::OK();
}
int AllocateEmptyBitmap(int64_t length, std::shared_ptr<Buffer>* out) {
return AllocateEmptyBitmap(default_memory_pool(), length, out);
}
}