Commit 3a5a73b
[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
1 file changed
+12
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
58 | 65 | | |
59 | | - | |
60 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
61 | 69 | | |
| 70 | + | |
62 | 71 | | |
63 | 72 | | |
64 | 73 | | |
| |||
0 commit comments