File tree Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Original file line number Diff line number Diff line change 3
3
4
4
use cfg_if:: cfg_if;
5
5
6
- pub mod memcpy_trivial;
7
-
6
+ /// The type signature of a `memcpy` function
8
7
pub type Memcpy = unsafe fn ( dst : * mut u8 , src : * const u8 , bytes : usize ) ;
9
8
10
- pub static ALL_MEMCPYS : & [ Memcpy ] = & [
9
+ /// Assert the arguments of `memcpy` are correct
10
+ fn memcpy_assert ( dst : * mut u8 , src : * const u8 , bytes : usize ) {
11
+ let src_before_dst = ( src as usize + bytes) <= dst as usize ;
12
+ let dst_before_src = ( dst as usize + bytes) <= src as usize ;
13
+ let buffers_do_not_overlap = src_before_dst || dst_before_src;
14
+ debug_assert ! ( buffers_do_not_overlap) ;
15
+
16
+ // Buffers larger than isize::max_value are bogus.
17
+ // See https://doc.rust-lang.org/std/primitive.pointer.html#method.offset
18
+
19
+ let size_fits_in_signed_offset = bytes <= isize:: max_value ( ) as usize ;
20
+ debug_assert ! ( size_fits_in_signed_offset) ;
21
+ }
22
+
23
+ pub mod memcpy_trivial;
24
+
25
+ /// Ensure sure all implementations have the same type
26
+ static ALL_MEMCPYS : & [ Memcpy ] = & [
11
27
memcpy_trivial:: memcpy,
12
28
] ;
Original file line number Diff line number Diff line change 1
1
pub unsafe fn memcpy ( dst : * mut u8 , src : * const u8 , bytes : usize ) {
2
+ crate :: memcpy_assert ( dst, src, bytes) ;
3
+
2
4
for i in 0 ..bytes {
3
5
let dst_byte = dst. add ( i) ;
4
6
let src_byte = src. add ( i) ;
You can’t perform that action at this time.
0 commit comments