-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbitmap.rs
More file actions
161 lines (146 loc) · 5.12 KB
/
bitmap.rs
File metadata and controls
161 lines (146 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::{
ptr::NonNull,
sync::{Arc, Mutex},
};
use super::*;
#[derive(Clone)]
pub(crate) struct UnsafeNonNullPtr(pub(crate) Arc<NonNull<[u8]>>);
unsafe impl Send for UnsafeNonNullPtr {}
unsafe impl Sync for UnsafeNonNullPtr {}
impl UnsafeNonNullPtr {
pub(crate) fn new(ptr: NonNull<[u8]>) -> Self {
Self(Arc::new(ptr))
}
pub(crate) fn as_ptr(&self) -> *const u8 {
self.0.as_ptr().cast()
}
pub(crate) fn as_mut_ptr(&mut self) -> *mut u8 {
self.0.as_ptr().cast()
}
}
#[derive(Clone)]
pub(crate) struct StaticBitmapAllocator {
pub(crate) memory: UnsafeNonNullPtr,
pub(crate) memory_size: usize,
pub(crate) block_size_in_bytes: usize,
pub(crate) bitmap: Arc<Mutex<Vec<bool>>>,
}
impl StaticBitmapAllocator {
pub(crate) fn init(
memory: NonNull<[u8]>,
num_blocks: usize,
block_size_in_bytes: usize,
) -> Self {
let memory_size_in_bytes = num_blocks * block_size_in_bytes;
Self {
memory: UnsafeNonNullPtr::new(memory),
memory_size: memory_size_in_bytes,
block_size_in_bytes,
bitmap: Arc::new(Mutex::new(vec![false; num_blocks])),
}
}
pub(crate) fn as_ptr(&self) -> *const u8 {
self.memory.as_ptr().cast()
}
pub(crate) fn find_free_block(&self) -> Option<usize> {
for (idx, entry) in self.bitmap.lock().unwrap().iter_mut().enumerate() {
if !*entry {
*entry = true;
return Some(idx);
}
}
None
}
#[allow(unreachable_code)]
pub(crate) fn find_adjacent_free_blocks(
&self,
requested_num_blocks: usize,
) -> Option<std::ops::Range<usize>> {
let mut bitmap = self.bitmap.lock().unwrap();
if requested_num_blocks > bitmap.len() {
return None;
}
let _range_of_blocks_found = false;
let _found_range = 0..0;
let mut start = 0;
let mut end = requested_num_blocks;
let mut busy_block_idx = 0;
loop {
let mut has_busy_block = false;
for (idx, sub_entry) in bitmap[start..end].iter().copied().enumerate() {
if sub_entry {
has_busy_block = true;
busy_block_idx = start + idx;
}
}
if !has_busy_block {
for entry in bitmap[start..end].iter_mut() {
*entry = true;
}
return Some(start..end);
} else {
start = busy_block_idx + 1;
end = start + requested_num_blocks;
if end > bitmap.len() {
break;
}
}
}
// panic!("not found block {} {} {}", start, end, self.bitmap.len());
None
}
pub(crate) 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(crate) fn allocate(
&self,
layout: std::alloc::Layout,
) -> CudaResult<std::ptr::NonNull<[u8]>> {
let size = layout.size();
assert!(size > 0);
assert_eq!(size % self.block_size_in_bytes, 0);
let num_blocks = size / self.block_size_in_bytes;
if size > self.block_size_in_bytes {
if let Some(range) = self.find_adjacent_free_blocks(num_blocks) {
let index = range.start;
let offset = index * self.block_size_in_bytes;
let ptr = unsafe { self.as_ptr().add(offset) };
let ptr = unsafe { NonNull::new_unchecked(ptr as _) };
return Ok(NonNull::slice_from_raw_parts(ptr, size));
}
panic!("allocation of {} blocks has failed", num_blocks);
// return Err(CudaError::AllocationError(format!(
// "allocation of {} blocks has failed",
// num_blocks
// )));
}
if let Some(index) = self.find_free_block() {
let offset = index * self.block_size_in_bytes;
let ptr = unsafe { self.as_ptr().add(offset) };
let ptr = unsafe { NonNull::new_unchecked(ptr as _) };
Ok(NonNull::slice_from_raw_parts(ptr, size))
} else {
panic!("allocation of 1 block has failed");
// return Err(CudaError::AllocationError(format!(
// "allocation of 1 block has failed",
// )));
}
}
pub(crate) fn deallocate(&self, ptr: std::ptr::NonNull<u8>, layout: std::alloc::Layout) {
let size = layout.size();
assert!(size > 0);
assert_eq!(size % self.block_size_in_bytes, 0);
let offset = unsafe { ptr.as_ptr().offset_from(self.as_ptr()) } as usize;
if offset >= self.memory_size {
return;
}
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;
self.free_blocks(index, num_blocks);
}
}