mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-15 20:45:45 +03:00
feat: add support for libc::memset
Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
This commit is contained in:
@@ -827,6 +827,22 @@ fn emulate_foreign_item_inner(
|
||||
this.mem_copy(ptr_src, ptr_dest, Size::from_bytes(n), true)?;
|
||||
this.write_pointer(ptr_dest, dest)?;
|
||||
}
|
||||
"memset" => {
|
||||
let [ptr_dest, val, n] =
|
||||
this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
|
||||
let ptr_dest = this.read_pointer(ptr_dest)?;
|
||||
let val = this.read_scalar(val)?.to_i32()?;
|
||||
let n = this.read_target_usize(n)?;
|
||||
// The docs say val is "interpreted as unsigned char".
|
||||
#[expect(clippy::as_conversions)]
|
||||
let val = val as u8;
|
||||
|
||||
this.ptr_get_alloc_id(ptr_dest, 0)?;
|
||||
|
||||
let bytes = std::iter::repeat_n(val, n.try_into().unwrap());
|
||||
this.write_bytes_ptr(ptr_dest, bytes)?;
|
||||
this.write_pointer(ptr_dest, dest)?;
|
||||
}
|
||||
|
||||
// LLVM intrinsics
|
||||
"llvm.prefetch" => {
|
||||
|
||||
@@ -78,6 +78,47 @@ fn test_strcpy() {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_memset() {
|
||||
unsafe {
|
||||
let val = 1;
|
||||
let dest = libc::calloc(3, 1);
|
||||
libc::memset(dest, val, 3);
|
||||
let slc = std::slice::from_raw_parts(dest as *const i8, 3);
|
||||
assert_eq!(*slc, [1i8, 1, 1]);
|
||||
libc::free(dest);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let val = 1;
|
||||
let dest = libc::calloc(4, 1);
|
||||
libc::memset(dest, val, 3);
|
||||
let slc = std::slice::from_raw_parts(dest as *const i8, 4);
|
||||
assert_eq!(*slc, [1i8, 1, 1, 0]);
|
||||
libc::free(dest);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let val = 1;
|
||||
let mut dest = 0_i8;
|
||||
libc::memset(&mut dest as *mut i8 as *mut libc::c_void, val, mem::size_of::<i8>());
|
||||
assert_eq!(dest, val as i8);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let val = 1;
|
||||
let mut dest = 0_i16;
|
||||
libc::memset(&mut dest as *mut i16 as *mut libc::c_void, val, mem::size_of::<i16>());
|
||||
assert_eq!(dest, 257);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let val = 257;
|
||||
let mut dest = 0_i16;
|
||||
libc::memset(&mut dest as *mut i16 as *mut libc::c_void, val, mem::size_of::<i16>());
|
||||
assert_eq!(dest, 257);
|
||||
}
|
||||
}
|
||||
|
||||
fn test_malloc() {
|
||||
// Test that small allocations sometimes *are* not very aligned.
|
||||
let saw_unaligned = (0..64).any(|_| unsafe {
|
||||
@@ -310,4 +351,5 @@ fn main() {
|
||||
|
||||
test_memcpy();
|
||||
test_strcpy();
|
||||
test_memset();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user