[C++] Add external-buffer support in DataBuffer lifecycle management#2617
Open
lucasfang wants to merge 1 commit intoapache:mainfrom
Open
[C++] Add external-buffer support in DataBuffer lifecycle management#2617lucasfang wants to merge 1 commit intoapache:mainfrom
lucasfang wants to merge 1 commit intoapache:mainfrom
Conversation
There was a problem hiding this comment.
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 inDataBufferand updates destructor /reserve/resizeto 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 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 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 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); | ||
|
|
Member
|
Thanks for your contribution! Just FYI that I just fixed the Windows CI. Please rebase to avoid confusion :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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