File tree Expand file tree Collapse file tree 1 file changed +11
-4
lines changed
gpu/src/metal/abstractions Expand file tree Collapse file tree 1 file changed +11
-4
lines changed Original file line number Diff line number Diff line change @@ -95,14 +95,21 @@ impl MetalState {
95
95
/// Returns a vector of a copy of the data that `buffer` holds, interpreting it into a specific
96
96
/// type `T`.
97
97
///
98
- /// BEWARE : this function uses an unsafe function for retrieveing the data, if the buffer's
98
+ /// SAFETY : this function uses an unsafe function for retrieveing the data, if the buffer's
99
99
/// contents don't match the specified `T`, expect undefined behaviour. Always make sure the
100
100
/// buffer you are retreiving from holds data of type `T`.
101
101
pub fn retrieve_contents < T : Clone > ( buffer : & Buffer ) -> Vec < T > {
102
102
let ptr = buffer. contents ( ) as * const T ;
103
103
let len = buffer. length ( ) as usize / mem:: size_of :: < T > ( ) ;
104
- let slice = unsafe { std:: slice:: from_raw_parts ( ptr, len) } ;
105
-
106
- slice. to_vec ( )
104
+ let mut contents = Vec :: with_capacity ( len) ;
105
+ for i in 0 ..len {
106
+ // 1. Read possibly unaligned data producing a bitwise copy
107
+ let val = unsafe { ptr. add ( i) . read_unaligned ( ) } ;
108
+ // 2. Clone into the vector to avoid both `contents` and `buffer` dropping it
109
+ contents. push ( val. clone ( ) ) ;
110
+ // 3. Forget the bitwise copy to avoid both `val` and `buffer` dropping it
111
+ core:: mem:: forget ( val) ;
112
+ }
113
+ contents
107
114
}
108
115
}
You can’t perform that action at this time.
0 commit comments