forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.cc
More file actions
157 lines (134 loc) · 5.22 KB
/
Copy pathbuffer.cc
File metadata and controls
157 lines (134 loc) · 5.22 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "arrow/buffer.h"
#include <cstdint>
#include "arrow/memory_pool.h"
#include "arrow/status.h"
#include "arrow/util/bit-util.h"
#include "arrow/util/logging.h"
namespace arrow {
Status Buffer::Copy(const int64_t start, const int64_t nbytes, MemoryPool* pool,
std::shared_ptr<Buffer>* out) const {
// Sanity checks
DCHECK_LT(start, size_);
DCHECK_LE(nbytes, size_ - start);
auto new_buffer = std::make_shared<PoolBuffer>(pool);
RETURN_NOT_OK(new_buffer->Resize(nbytes));
std::memcpy(new_buffer->mutable_data(), data() + start, static_cast<size_t>(nbytes));
*out = new_buffer;
return Status::OK();
}
Status 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_))));
}
Status Buffer::FromString(const std::string& data, MemoryPool* pool,
std::shared_ptr<Buffer>* out) {
auto size = static_cast<int64_t>(data.size());
RETURN_NOT_OK(AllocateBuffer(pool, size, out));
std::copy(data.c_str(), data.c_str() + size, (*out)->mutable_data());
return Status::OK();
}
Status Buffer::FromString(const std::string& data, std::shared_ptr<Buffer>* out) {
return FromString(data, default_memory_pool(), out);
}
void Buffer::CheckMutable() const { DCHECK(is_mutable()) << "buffer not mutable"; }
PoolBuffer::PoolBuffer(MemoryPool* pool) : ResizableBuffer(nullptr, 0) {
if (pool == nullptr) {
pool = default_memory_pool();
}
pool_ = pool;
}
PoolBuffer::~PoolBuffer() {
if (mutable_data_ != nullptr) {
pool_->Free(mutable_data_, capacity_);
}
}
Status PoolBuffer::Reserve(const int64_t capacity) {
if (!mutable_data_ || capacity > capacity_) {
uint8_t* new_data;
int64_t new_capacity = BitUtil::RoundUpToMultipleOf64(capacity);
if (mutable_data_) {
RETURN_NOT_OK(pool_->Reallocate(capacity_, new_capacity, &mutable_data_));
} else {
RETURN_NOT_OK(pool_->Allocate(new_capacity, &new_data));
mutable_data_ = new_data;
}
data_ = mutable_data_;
capacity_ = new_capacity;
}
return Status::OK();
}
Status PoolBuffer::Resize(const int64_t new_size, bool shrink_to_fit) {
if (!shrink_to_fit || (new_size > size_)) {
RETURN_NOT_OK(Reserve(new_size));
} else {
// Buffer is not growing, so shrink to the requested size without
// excess space.
int64_t new_capacity = BitUtil::RoundUpToMultipleOf64(new_size);
if (capacity_ != new_capacity) {
// Buffer hasn't got yet the requested size.
if (new_size == 0) {
pool_->Free(mutable_data_, capacity_);
capacity_ = 0;
mutable_data_ = nullptr;
data_ = nullptr;
} else {
RETURN_NOT_OK(pool_->Reallocate(capacity_, new_capacity, &mutable_data_));
data_ = mutable_data_;
capacity_ = new_capacity;
}
}
}
size_ = new_size;
return Status::OK();
}
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";
parent_ = parent;
}
Status AllocateBuffer(MemoryPool* pool, const int64_t size,
std::shared_ptr<Buffer>* out) {
auto buffer = std::make_shared<PoolBuffer>(pool);
RETURN_NOT_OK(buffer->Resize(size));
*out = buffer;
return Status::OK();
}
Status AllocateResizableBuffer(MemoryPool* pool, const int64_t size,
std::shared_ptr<ResizableBuffer>* out) {
auto buffer = std::make_shared<PoolBuffer>(pool);
RETURN_NOT_OK(buffer->Resize(size));
*out = buffer;
return Status::OK();
}
} // namespace arrow