Skip to content

Commit 3a5a73b

Browse files
[rocm-libraries] ROCm/rocm-libraries#3672 (commit 2ab01d6)
[rocBLAS] Refactor memory management in host_alloc.cpp (#3672) ## Summary Improved memory management in `host_alloc.cpp` by refactoring the deallocation logic for better efficiency and adding diagnostic warnings for untracked pointer frees. ## Changes 1. **Refactored `free_ptr_use()` to use iterator-based access** (reduces redundant map lookups) 2. **Added warning for freeing untracked pointers** (helps detect potential memory corruption issues) ## Motivation The current implementation of `free_ptr_use()` in `host_alloc.cpp` had two issues: 1. **Performance:** Redundant map lookups when deallocating tracked memory 2. **Correctness:** Using `operator[]` on a map can unintentionally insert entries for non-existent keys 3. **Diagnostics:** Silent failures when attempting to free untracked pointers made debugging difficult This PR addresses all three issues by refactoring the lookup logic and adding diagnostic warnings. ## Technical Details **File:** `projects/rocblas/clients/common/host_alloc.cpp` ### Change 1: Iterator-based memory deallocation **Before:** ```cpp if(ptr && mem_allocated[ptr]) { // Lookup #1 (may insert!) mem_used -= mem_allocated[ptr]; // Lookup #2 mem_allocated.erase(ptr); // Lookup #3 } ``` **After:** ```cpp auto it = mem_allocated.find(ptr); // Single lookup if(ptr && it != mem_allocated.end()) { mem_used -= it->second; // Use cached iterator mem_allocated.erase(it); // Use cached iterator } ``` **Benefits:** - Reduces map lookups from 3 to 1 - Prevents unintended map insertions via `operator[]` - More efficient memory tracking ### Change 2: Diagnostic warning for untracked pointers **Added:** ```cpp else if(ptr && call_free) { rocblas_cerr << "Warning: Freeing untracked pointer " << ptr << " - untracked memory released (potential double-free or memory corruption)" << std::endl; } ``` **Benefits:** - Helps detect double-free bugs - Identifies potential memory corruption issues - Provides actionable diagnostic information during testing ## Test Plan - Built rocBLAS with no compilation errors - Existing client test suite exercises memory allocation/deallocation paths - No functional behavior changes to normal operation - Warning messages help identify issues during debugging ## Test Result - All existing tests continue to pass - Memory tracking functionality unchanged - Warning properly triggers for untracked pointer frees - No behavioral differences for correctly tracked memory ## Submission Checklist - [x] Look over the contributing guidelines at https://github.com/ROCm/ROCm/blob/develop/CONTRIBUTING.md#pull-requests.
1 parent 1027f15 commit 3a5a73b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

clients/common/host_alloc.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,20 @@ void alloc_ptr_use(void* ptr, size_t size)
5454
void free_ptr_use(void* ptr, bool call_free)
5555
{
5656
std::lock_guard<std::mutex> lock(mem_mutex);
57-
if(ptr && mem_allocated[ptr])
57+
auto it = mem_allocated.find(ptr);
58+
59+
if(ptr && it != mem_allocated.end())
60+
{
61+
mem_used -= it->second;
62+
mem_allocated.erase(it);
63+
}
64+
else if(ptr && call_free)
5865
{
59-
mem_used -= mem_allocated[ptr];
60-
mem_allocated.erase(ptr);
66+
rocblas_cerr << "Warning: Freeing untracked pointer " << ptr
67+
<< " - untracked memory released (potential double-free or memory corruption)"
68+
<< std::endl;
6169
}
70+
6271
if(call_free)
6372
free(ptr);
6473
}

0 commit comments

Comments
 (0)