Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CORE] Update AllocationListener usage after successfully releasing memory #7396

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions cpp/core/memory/MemoryAllocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ 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);
if (diff > 0) {
updateUsage(diff);
}
bool succeed = delegated_->reallocate(p, size, newSize, out);
if (!succeed) {
if (succeed && diff < 0) {
updateUsage(diff);
} else if (!succeed && diff > 0) {
updateUsage(-diff);
}
return succeed;
Copy link
Member

Choose a reason for hiding this comment

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

Can we use two different code branches for diff > 0 and diff < 0 ? E.g.,

Suggested change
int64_t diff = newSize - size;
updateUsage(diff);
if (diff > 0) {
updateUsage(diff);
}
bool succeed = delegated_->reallocate(p, size, newSize, out);
if (!succeed) {
if (succeed && diff < 0) {
updateUsage(diff);
} else if (!succeed && diff > 0) {
updateUsage(-diff);
}
return succeed;
int64_t diff = newSize - size;
if (diff >= 0) {
updateUsage(diff);
bool succeeded = delegated_->reallocate(p, size, newSize, out);
if (!succeeded) {
updateUsage(-diff);
}
return succeeded;
}
// Code path for diff < 0.
bool succeeded = delegated_->reallocate(p, size, newSize, out);
if (succeeded) {
updateUsage(diff);
}
return succeed;

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks, changed

Expand All @@ -65,19 +69,22 @@ bool ListenableMemoryAllocator::reallocateAligned(
int64_t newSize,
void** out) {
int64_t diff = newSize - size;
updateUsage(diff);
if (diff > 0) {
updateUsage(diff);
}
bool succeed = delegated_->reallocateAligned(p, alignment, size, newSize, out);
if (!succeed) {
if (succeed && diff < 0) {
updateUsage(diff);
} else if (!succeed && diff > 0) {
updateUsage(-diff);
}
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
Loading