mod libc { pubuse core::ffi::c_void; pubuse core::primitive::{
i32 as c_int, i64 as c_long, i8 as c_char, u32 as c_uint, u64 as c_ulong, u8 as c_uchar,
};
}
#[macro_use] mod externs { usecrate::libc; usecrate::ops::{die, ForceAdd as _, ForceInto as _}; use alloc::alloc::{selfas rust, Layout}; use core::mem::{self, MaybeUninit}; use core::ptr; use core::slice;
const HEADER: usize = { let need_len = mem::size_of::<usize>(); // Round up to multiple of MALLOC_ALIGN.
(need_len + MALLOC_ALIGN - 1) & !(MALLOC_ALIGN - 1)
};
// `max_align_t` may be bigger than this, but libyaml does not use `long // double` or u128. const MALLOC_ALIGN: usize = { let int_align = mem::align_of::<libc::c_ulong>(); let ptr_align = mem::align_of::<usize>(); if int_align >= ptr_align {
int_align
} else {
ptr_align
}
};
pubunsafefn malloc(size: libc::c_ulong) -> *mut libc::c_void { let size = HEADER.force_add(size.force_into()); let layout = Layout::from_size_align(size, MALLOC_ALIGN)
.ok()
.unwrap_or_else(die); let memory = rust::alloc(layout); if memory.is_null() {
rust::handle_alloc_error(layout);
}
memory.cast::<usize>().write(size);
memory.add(HEADER).cast()
}
pubunsafefn realloc(ptr: *mut libc::c_void, new_size: libc::c_ulong) -> *mut libc::c_void { letmut memory = ptr.cast::<u8>().sub(HEADER); let size = memory.cast::<usize>().read(); let layout = Layout::from_size_align_unchecked(size, MALLOC_ALIGN); let new_size = HEADER.force_add(new_size.force_into()); let new_layout = Layout::from_size_align(new_size, MALLOC_ALIGN)
.ok()
.unwrap_or_else(die);
memory = rust::realloc(memory, layout, new_size); if memory.is_null() {
rust::handle_alloc_error(new_layout);
}
memory.cast::<usize>().write(new_size);
memory.add(HEADER).cast()
}
pubunsafefn free(ptr: *mut libc::c_void) { let memory = ptr.cast::<u8>().sub(HEADER); let size = memory.cast::<usize>().read(); let layout = Layout::from_size_align_unchecked(size, MALLOC_ALIGN);
rust::dealloc(memory, layout);
}
pubunsafefn memcmp(
lhs: *const libc::c_void,
rhs: *const libc::c_void,
count: libc::c_ulong,
) -> libc::c_int { let lhs = slice::from_raw_parts(lhs.cast::<u8>(), count as usize); let rhs = slice::from_raw_parts(rhs.cast::<u8>(), count as usize);
lhs.cmp(rhs) as libc::c_int
}
pubunsafefn memset(
dest: *mut libc::c_void,
ch: libc::c_int,
count: libc::c_ulong,
) -> *mut libc::c_void {
ptr::write_bytes(dest.cast::<u8>(), ch as u8, count as usize);
dest
}
pubunsafefn strcmp(lhs: *const libc::c_char, rhs: *const libc::c_char) -> libc::c_int { let lhs = slice::from_raw_parts(lhs.cast::<u8>(), strlen(lhs) as usize); let rhs = slice::from_raw_parts(rhs.cast::<u8>(), strlen(rhs) as usize);
lhs.cmp(rhs) as libc::c_int
}
pubunsafefn strdup(src: *const libc::c_char) -> *mut libc::c_char { let len = strlen(src); let dest = malloc(len + 1);
memcpy(dest, src.cast(), len + 1);
dest.cast()
}
pubunsafefn strlen(str: *const libc::c_char) -> libc::c_ulong { letmut end = str; while *end != 0 {
end = end.add(1);
}
end.offset_from(str) as libc::c_ulong
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.