Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
move mutex guard from inside a loop
Browse files Browse the repository at this point in the history
  • Loading branch information
robik75 committed Feb 7, 2024
1 parent 350fbf8 commit a0f7577
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/static_allocator/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,12 @@ impl StaticDeviceAllocator {
None
}

fn free_block(&self, index: usize) {
self.bitmap.lock().unwrap()[index] = false;
fn free_blocks(&self, index: usize, num_blocks: usize) {
assert!(num_blocks > 0);
let mut guard = self.bitmap.lock().unwrap();
for i in index..index + num_blocks {
guard[i] = false;
}
}

pub fn free(self) -> CudaResult<()> {
Expand Down Expand Up @@ -351,10 +355,7 @@ unsafe impl Allocator for StaticDeviceAllocator {
assert_eq!(offset % self.block_size_in_bytes, 0);
let index = offset / self.block_size_in_bytes;
let num_blocks = size / self.block_size_in_bytes;
assert!(num_blocks > 0);
for actual_idx in index..index + num_blocks {
self.free_block(actual_idx);
}
self.free_blocks(index, num_blocks);
#[cfg(feature = "allocator_stats")]
self.stats.lock().unwrap().free(index);
}
Expand Down

0 comments on commit a0f7577

Please sign in to comment.