This is an automated email from the ASF dual-hosted git repository.

yangzy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-gluten.git


The following commit(s) were added to refs/heads/main by this push:
     new a6c0798b03 [CORE] Update AllocationListener usage after successfully 
releasing memory (#7396)
a6c0798b03 is described below

commit a6c0798b031c6d4e10eb63de0a36c364489abfcb
Author: Zhen Wang <[email protected]>
AuthorDate: Wed Oct 9 22:04:53 2024 +0800

    [CORE] Update AllocationListener usage after successfully releasing memory 
(#7396)
---
 cpp/core/memory/MemoryAllocator.cc | 41 ++++++++++++++++++++++++++------------
 1 file changed, 28 insertions(+), 13 deletions(-)

diff --git a/cpp/core/memory/MemoryAllocator.cc 
b/cpp/core/memory/MemoryAllocator.cc
index e34c4c9479..dd5fb8e197 100644
--- a/cpp/core/memory/MemoryAllocator.cc
+++ b/cpp/core/memory/MemoryAllocator.cc
@@ -50,12 +50,20 @@ bool ListenableMemoryAllocator::allocateAligned(uint64_t 
alignment, int64_t size
 
 bool ListenableMemoryAllocator::reallocate(void* p, int64_t size, int64_t 
newSize, void** out) {
   int64_t diff = newSize - size;
-  updateUsage(diff);
-  bool succeed = delegated_->reallocate(p, size, newSize, out);
-  if (!succeed) {
-    updateUsage(-diff);
+  if (diff >= 0) {
+    updateUsage(diff);
+    bool succeed = delegated_->reallocate(p, size, newSize, out);
+    if (!succeed) {
+      updateUsage(-diff);
+    }
+    return succeed;
+  } else {
+    bool succeed = delegated_->reallocate(p, size, newSize, out);
+    if (succeed) {
+      updateUsage(diff);
+    }
+    return succeed;
   }
-  return succeed;
 }
 
 bool ListenableMemoryAllocator::reallocateAligned(
@@ -65,19 +73,26 @@ bool ListenableMemoryAllocator::reallocateAligned(
     int64_t newSize,
     void** out) {
   int64_t diff = newSize - size;
-  updateUsage(diff);
-  bool succeed = delegated_->reallocateAligned(p, alignment, size, newSize, 
out);
-  if (!succeed) {
-    updateUsage(-diff);
+  if (diff >= 0) {
+    updateUsage(diff);
+    bool succeed = delegated_->reallocateAligned(p, alignment, size, newSize, 
out);
+    if (!succeed) {
+      updateUsage(-diff);
+    }
+    return succeed;
+  } else {
+    bool succeed = delegated_->reallocateAligned(p, alignment, size, newSize, 
out);
+    if (succeed) {
+      updateUsage(diff);
+    }
+    return succeed;
   }
-  return succeed;
 }
 
 bool ListenableMemoryAllocator::free(void* p, int64_t size) {
-  updateUsage(-size);
   bool succeed = delegated_->free(p, size);
-  if (!succeed) {
-    updateUsage(size);
+  if (succeed) {
+    updateUsage(-size);
   }
   return succeed;
 }


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

Reply via email to