Skip to content

Commit

Permalink
[CORE] Update AllocationListener usage after successfully releasing m…
Browse files Browse the repository at this point in the history
…emory (#7396)
  • Loading branch information
wForget authored Oct 9, 2024
1 parent f0c643d commit a6c0798
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions cpp/core/memory/MemoryAllocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
}
Expand Down

0 comments on commit a6c0798

Please sign in to comment.