Skip to content

Commit 12d99e9

Browse files
committed
fix(memory): handle allocation exceptions
1 parent 40ac4ea commit 12d99e9

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

include/paimon/memory/memory_pool.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class PAIMON_EXPORT MemoryPool {
5555
///
5656
/// @param size Number of bytes to allocate.
5757
/// @param alignment Memory alignment requirement (0 for default alignment).
58-
/// @return Pointer to allocated memory, or nullptr on failure.
58+
/// @return Pointer to allocated memory.
59+
/// @throws std::bad_alloc if the allocation fails.
5960
virtual void* Malloc(uint64_t size, uint64_t alignment = 0) = 0;
6061

6162
/// Reallocate memory to a new size.

src/paimon/common/utils/arrow/mem_utils.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <cstdint>
2020
#include <memory>
21+
#include <new>
2122
#include <string>
2223

2324
#include "arrow/memory_pool.h"
@@ -33,18 +34,30 @@ class ArrowMemPoolAdaptor : public arrow::MemoryPool {
3334
: pool_(*pool), life_holder_(pool) {}
3435

3536
arrow::Status Allocate(int64_t size, int64_t alignment, uint8_t** out) override {
36-
*out = reinterpret_cast<uint8_t*>(pool_.Malloc(size, alignment));
37-
if (size > 0 && *out == nullptr) {
37+
uint8_t* new_out = nullptr;
38+
try {
39+
new_out = reinterpret_cast<uint8_t*>(pool_.Malloc(size, alignment));
40+
} catch (const std::bad_alloc&) {
3841
return arrow::Status::OutOfMemory(fmt::format("failed to allocate {} bytes", size));
3942
}
43+
if (size > 0 && new_out == nullptr) {
44+
return arrow::Status::OutOfMemory(fmt::format("failed to allocate {} bytes", size));
45+
}
46+
*out = new_out;
4047
stats_.DidAllocateBytes(size);
4148
return arrow::Status::OK();
4249
}
4350

4451
arrow::Status Reallocate(int64_t old_size, int64_t new_size, int64_t alignment,
4552
uint8_t** ptr) override {
46-
auto* new_ptr =
47-
reinterpret_cast<uint8_t*>(pool_.Realloc(*ptr, old_size, new_size, alignment));
53+
uint8_t* new_ptr = nullptr;
54+
try {
55+
new_ptr =
56+
reinterpret_cast<uint8_t*>(pool_.Realloc(*ptr, old_size, new_size, alignment));
57+
} catch (const std::bad_alloc&) {
58+
return arrow::Status::OutOfMemory(
59+
fmt::format("failed to reallocate memory from {} to {} bytes", old_size, new_size));
60+
}
4861
if (new_size > 0 && new_ptr == nullptr) {
4962
return arrow::Status::OutOfMemory(
5063
fmt::format("failed to reallocate memory from {} to {} bytes", old_size, new_size));

0 commit comments

Comments
 (0)