forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_pool.cc
More file actions
288 lines (234 loc) · 8.34 KB
/
Copy pathmemory_pool.cc
File metadata and controls
288 lines (234 loc) · 8.34 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// 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/memory_pool.h"
#include <algorithm> // IWYU pragma: keep
#include <cstdlib> // IWYU pragma: keep
#include <cstring> // IWYU pragma: keep
#include <iostream> // IWYU pragma: keep
#include <limits>
#include <memory>
#include "arrow/status.h"
#include "arrow/util/logging.h" // IWYU pragma: keep
#ifdef ARROW_JEMALLOC
// Needed to support jemalloc 3 and 4
#define JEMALLOC_MANGLE
// Explicitly link to our version of jemalloc
#include "jemalloc_ep/dist/include/jemalloc/jemalloc.h"
#endif
namespace arrow {
constexpr size_t kAlignment = 64;
namespace {
// A static piece of memory for 0-size allocations, so as to return
// an aligned non-null pointer.
alignas(kAlignment) static uint8_t zero_size_area[1];
// Allocate memory according to the alignment requirements for Arrow
// (as of May 2016 64 bytes)
Status AllocateAligned(int64_t size, uint8_t** out) {
// TODO(emkornfield) find something compatible with windows
if (size < 0) {
return Status::Invalid("negative malloc size");
}
if (size == 0) {
*out = zero_size_area;
return Status::OK();
}
if (static_cast<uint64_t>(size) >= std::numeric_limits<size_t>::max()) {
return Status::CapacityError("malloc size overflows size_t");
}
#ifdef _WIN32
// Special code path for Windows
*out =
reinterpret_cast<uint8_t*>(_aligned_malloc(static_cast<size_t>(size), kAlignment));
if (!*out) {
return Status::OutOfMemory("malloc of size ", size, " failed");
}
#elif defined(ARROW_JEMALLOC)
*out = reinterpret_cast<uint8_t*>(
mallocx(static_cast<size_t>(size), MALLOCX_ALIGN(kAlignment)));
if (*out == NULL) {
return Status::OutOfMemory("malloc of size ", size, " failed");
}
#else
const int result = posix_memalign(reinterpret_cast<void**>(out), kAlignment,
static_cast<size_t>(size));
if (result == ENOMEM) {
return Status::OutOfMemory("malloc of size ", size, " failed");
}
if (result == EINVAL) {
return Status::Invalid("invalid alignment parameter: ", kAlignment);
}
#endif
return Status::OK();
}
void DeallocateAligned(uint8_t* ptr, int64_t size) {
if (ptr == zero_size_area) {
DCHECK_EQ(size, 0);
} else {
#ifdef _WIN32
_aligned_free(ptr);
#elif defined(ARROW_JEMALLOC)
dallocx(ptr, MALLOCX_ALIGN(kAlignment));
#else
std::free(ptr);
#endif
}
}
Status ReallocateAligned(int64_t old_size, int64_t new_size, uint8_t** ptr) {
uint8_t* previous_ptr = *ptr;
if (previous_ptr == zero_size_area) {
DCHECK_EQ(old_size, 0);
return AllocateAligned(new_size, ptr);
}
if (new_size == 0) {
DeallocateAligned(previous_ptr, old_size);
*ptr = zero_size_area;
return Status::OK();
}
#ifdef ARROW_JEMALLOC
if (new_size < 0) {
return Status::Invalid("negative realloc size");
}
if (static_cast<uint64_t>(new_size) >= std::numeric_limits<size_t>::max()) {
return Status::CapacityError("realloc overflows size_t");
}
*ptr = reinterpret_cast<uint8_t*>(
rallocx(*ptr, static_cast<size_t>(new_size), MALLOCX_ALIGN(kAlignment)));
if (*ptr == NULL) {
*ptr = previous_ptr;
return Status::OutOfMemory("realloc of size ", new_size, " failed");
}
#else
// Note: We cannot use realloc() here as it doesn't guarantee alignment.
// Allocate new chunk
uint8_t* out = nullptr;
RETURN_NOT_OK(AllocateAligned(new_size, &out));
DCHECK(out);
// Copy contents and release old memory chunk
memcpy(out, *ptr, static_cast<size_t>(std::min(new_size, old_size)));
#ifdef _WIN32
_aligned_free(*ptr);
#else
std::free(*ptr);
#endif // defined(_MSC_VER)
*ptr = out;
#endif // defined(ARROW_JEMALLOC)
return Status::OK();
}
} // namespace
MemoryPool::MemoryPool() {}
MemoryPool::~MemoryPool() {}
int64_t MemoryPool::max_memory() const { return -1; }
///////////////////////////////////////////////////////////////////////
// Default MemoryPool implementation
class DefaultMemoryPool : public MemoryPool {
public:
~DefaultMemoryPool() override {}
Status Allocate(int64_t size, uint8_t** out) override {
RETURN_NOT_OK(AllocateAligned(size, out));
stats_.UpdateAllocatedBytes(size);
return Status::OK();
}
Status Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) override {
RETURN_NOT_OK(ReallocateAligned(old_size, new_size, ptr));
stats_.UpdateAllocatedBytes(new_size - old_size);
return Status::OK();
}
int64_t bytes_allocated() const override { return stats_.bytes_allocated(); }
void Free(uint8_t* buffer, int64_t size) override {
DeallocateAligned(buffer, size);
stats_.UpdateAllocatedBytes(-size);
}
int64_t max_memory() const override { return stats_.max_memory(); }
private:
internal::MemoryPoolStats stats_;
};
std::unique_ptr<MemoryPool> MemoryPool::CreateDefault() {
return std::unique_ptr<MemoryPool>(new DefaultMemoryPool);
}
MemoryPool* default_memory_pool() {
static DefaultMemoryPool default_memory_pool_;
return &default_memory_pool_;
}
///////////////////////////////////////////////////////////////////////
// LoggingMemoryPool implementation
LoggingMemoryPool::LoggingMemoryPool(MemoryPool* pool) : pool_(pool) {}
Status LoggingMemoryPool::Allocate(int64_t size, uint8_t** out) {
Status s = pool_->Allocate(size, out);
std::cout << "Allocate: size = " << size << std::endl;
return s;
}
Status LoggingMemoryPool::Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) {
Status s = pool_->Reallocate(old_size, new_size, ptr);
std::cout << "Reallocate: old_size = " << old_size << " - new_size = " << new_size
<< std::endl;
return s;
}
void LoggingMemoryPool::Free(uint8_t* buffer, int64_t size) {
pool_->Free(buffer, size);
std::cout << "Free: size = " << size << std::endl;
}
int64_t LoggingMemoryPool::bytes_allocated() const {
int64_t nb_bytes = pool_->bytes_allocated();
std::cout << "bytes_allocated: " << nb_bytes << std::endl;
return nb_bytes;
}
int64_t LoggingMemoryPool::max_memory() const {
int64_t mem = pool_->max_memory();
std::cout << "max_memory: " << mem << std::endl;
return mem;
}
///////////////////////////////////////////////////////////////////////
// ProxyMemoryPool implementation
class ProxyMemoryPool::ProxyMemoryPoolImpl {
public:
explicit ProxyMemoryPoolImpl(MemoryPool* pool) : pool_(pool) {}
Status Allocate(int64_t size, uint8_t** out) {
RETURN_NOT_OK(pool_->Allocate(size, out));
stats_.UpdateAllocatedBytes(size);
return Status::OK();
}
Status Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) {
RETURN_NOT_OK(pool_->Reallocate(old_size, new_size, ptr));
stats_.UpdateAllocatedBytes(new_size - old_size);
return Status::OK();
}
void Free(uint8_t* buffer, int64_t size) {
pool_->Free(buffer, size);
stats_.UpdateAllocatedBytes(-size);
}
int64_t bytes_allocated() const { return stats_.bytes_allocated(); }
int64_t max_memory() const { return stats_.max_memory(); }
private:
MemoryPool* pool_;
internal::MemoryPoolStats stats_;
};
ProxyMemoryPool::ProxyMemoryPool(MemoryPool* pool) {
impl_.reset(new ProxyMemoryPoolImpl(pool));
}
ProxyMemoryPool::~ProxyMemoryPool() {}
Status ProxyMemoryPool::Allocate(int64_t size, uint8_t** out) {
return impl_->Allocate(size, out);
}
Status ProxyMemoryPool::Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) {
return impl_->Reallocate(old_size, new_size, ptr);
}
void ProxyMemoryPool::Free(uint8_t* buffer, int64_t size) {
return impl_->Free(buffer, size);
}
int64_t ProxyMemoryPool::bytes_allocated() const { return impl_->bytes_allocated(); }
int64_t ProxyMemoryPool::max_memory() const { return impl_->max_memory(); }
} // namespace arrow