zml1206 commented on code in PR #13668:
URL: https://github.com/apache/gluten/pull/13668#discussion_r4640946341


##########
cpp/core/memory/MemoryAllocator.cc:
##########
@@ -166,9 +166,21 @@ bool StdMemoryAllocator::reallocate(void* p, int64_t size, 
int64_t newSize, void
 
 bool StdMemoryAllocator::reallocateAligned(void* p, uint64_t alignment, 
int64_t size, int64_t newSize, void** out) {
   GLUTEN_CHECK(p != nullptr, "reallocate with nullptr");
-  if (newSize <= 0) {
+  if (newSize < 0) {
     return false;
   }
+  if (newSize == 0) {
+    // Keep a freeable, aligned pointer without relying on realloc(p, 0) or 
aligned_alloc(alignment, 0).
+    // The allocation has zero logical size, so only the old size is removed 
from accounting.
+    void* empty = std::aligned_alloc(alignment, alignment);

Review Comment:
   Line 179 frees the original allocation. The new block is returned through 
*out, and the Arrow buffer owns it from that point on. When the buffer is 
destroyed, Arrow calls Free even if its capacity is zero. 
StdMemoryAllocator::free(ptr, 0) then calls std::free(ptr), which releases the 
entire block. Growing the buffer also frees the placeholder.
   
   There is a small memory cost here: each live buffer shrunk to zero holds 
alignment bytes (64 in this case), while bytes_ tracks its logical size of 
zero. Repeated resizes don’t accumulate these blocks.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to