use crate::ffi::{c_char, c_int};
use ::alloc::alloc;
use core::{alloc::Layout, ffi::c_void, ptr};
#[allow(non_camel_case_types)]
pub type size_t = usize;
#[cfg_attr(not(feature = "std"), no_mangle)]
pub unsafe extern "C" fn strlen(s: *const c_char) -> size_t {
let mut cursor = s;
unsafe {
while *cursor != 0 {
cursor = cursor.add(1);
}
}
(cursor as size_t) - (s as size_t)
}
#[cfg_attr(not(feature = "std"), no_mangle)]
pub unsafe extern "C" fn strchr(mut s: *const c_char, c: c_int) -> *mut c_char {
loop {
unsafe {
match *s {
x if x == c as c_char => return s as *mut _,
0 => return ptr::null_mut(),
_ => s = s.add(1),
}
}
}
}
#[cfg_attr(not(feature = "std"), no_mangle)]
pub unsafe extern "C" fn strcmp(mut s1: *const c_char, mut s2: *const c_char) -> c_int {
unsafe {
while *s1 != 0 && *s1 == *s2 {
s1 = s1.add(1);
s2 = s2.add(1);
}
c_int::from(*s1) - c_int::from(*s2)
}
}
#[cfg_attr(not(feature = "std"), no_mangle)]
pub unsafe extern "C" fn malloc(size: size_t) -> *mut c_void {
unsafe { alloc::alloc(Layout::from_size_align_unchecked(size, 1)).cast::<c_void>() }
}
#[cfg_attr(not(feature = "std"), no_mangle)]
pub unsafe extern "C" fn calloc(nmemb: size_t, size: size_t) -> *mut c_void {
unsafe {
alloc::alloc_zeroed(Layout::from_size_align_unchecked(nmemb * size, 1)).cast::<c_void>()
}
}
#[cfg_attr(not(feature = "std"), no_mangle)]
pub unsafe extern "C" fn realloc(ptr: *mut c_void, size: size_t) -> *mut c_void {
unsafe {
alloc::realloc(ptr.cast::<u8>(), Layout::from_size_align_unchecked(1, 1), size)
.cast::<c_void>()
}
}
#[cfg_attr(not(feature = "std"), no_mangle)]
pub unsafe extern "C" fn free(ptr: *mut c_void) {
unsafe { alloc::dealloc(ptr.cast::<u8>(), Layout::from_size_align_unchecked(1, 1)) }
}