Skip to content

[C++] Add external-buffer support in DataBuffer lifecycle management#2617

Open
lucasfang wants to merge 1 commit intoapache:mainfrom
lucasfang:mempool
Open

[C++] Add external-buffer support in DataBuffer lifecycle management#2617
lucasfang wants to merge 1 commit intoapache:mainfrom
lucasfang:mempool

Conversation

@lucasfang
Copy link
Copy Markdown

What changes were proposed in this pull request?

This PR adds non-owning buffer support to DataBuffer so it can attach externally managed memory (via setData) without taking ownership of allocation/free, updates reserve/resize/destructor paths to be ownership-aware, and adds a focused unit test to verify that non-owning buffers keep external size semantics and do not free external memory during destruction.

Why are the changes needed?

These changes are needed to safely integrate with external prebuffer cache scenarios where memory is managed outside ORC, avoiding unnecessary reallocations and preventing ownership/lifecycle mismatches.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: GPT-5.3-codex

Copilot AI review requested due to automatic review settings May 7, 2026 06:19
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds non-owning (external) buffer attachment support to DataBuffer so it can reference externally managed memory without freeing it, and adjusts lifecycle methods accordingly.

Changes:

  • Introduces ownBuffer_ tracking in DataBuffer and updates destructor / reserve / resize to be ownership-aware.
  • Adds DataBuffer::setData() to attach an external raw buffer (non-owning).
  • Adds a unit test verifying external buffer size semantics and that external memory isn’t freed on destruction.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
c++/include/orc/MemoryPool.hh Extends DataBuffer API with ownership flag and setData() declaration.
c++/src/MemoryPool.cc Implements ownership-aware lifecycle behavior and setData() logic.
c++/test/TestCache.cc Adds a focused unit test for non-owning external buffers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread c++/test/TestCache.cc
Comment on lines +34 to +42
char* malloc(uint64_t size) override {
++allocCount;
return static_cast<char*>(std::malloc(size));
}

void free(char* p) override {
++freeCount;
std::free(p);
}
Comment thread c++/src/MemoryPool.cc
Comment on lines +138 to +147
void DataBuffer<T>::setData(T* buffer, size_t bufSize) {
if (ownBuffer_ && buf_) {
static_assert(std::is_trivially_copyable<T>::value,
"Only trivially copyable type is supported for DataBuffer reserve");
memoryPool_.free(reinterpret_cast<char*>(buf_));
}
ownBuffer_ = false;
buf_ = buffer;
currentSize_ = currentCapacity_ = static_cast<uint64_t>(bufSize / sizeof(T));
}
Comment thread c++/src/MemoryPool.cc
Comment on lines 82 to 93
DataBuffer<T>::~DataBuffer() {
if (!ownBuffer_) {
return;
}
for (uint64_t i = currentSize_; i > 0; --i) {
(buf_ + i - 1)->~T();
}
if (buf_) {
static_assert(std::is_trivially_copyable<T>::value,
"Only trivially copyable type is supported for DataBuffer reserve");
memoryPool_.free(reinterpret_cast<char*>(buf_));
}
Comment on lines 52 to 54
public:
DataBuffer(MemoryPool& pool, uint64_t size = 0);
DataBuffer(MemoryPool& pool, uint64_t size = 0, bool ownBuf = true);

@wgtmac
Copy link
Copy Markdown
Member

wgtmac commented May 7, 2026

Thanks for your contribution! Just FYI that I just fixed the Windows CI. Please rebase to avoid confusion :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants