Add pvalloc()

This commit is contained in:
Peter Limkilde Svendsen
2025-02-11 21:13:05 +01:00
parent 7edc231f31
commit b3f36faf87
7 changed files with 99 additions and 3 deletions
+10
View File
@@ -0,0 +1,10 @@
sys_includes = ["features.h", "stddef.h"]
include_guard = "_RELIBC_MALLOC_H"
trailer = "#include <bits/malloc.h>"
language = "C"
style = "Type"
no_includes = true
cpp_compat = true
[enum]
prefix_with_name = true
+39
View File
@@ -0,0 +1,39 @@
//! `malloc.h` implementation.
//!
//! Non-POSIX, see <https://man7.org/linux/man-pages/man3/posix_memalign.3.html>.
use crate::{
header::errno::*,
platform::{self, types::*, Pal, Sys, ERRNO},
};
use core::ptr;
/// See <https://man7.org/linux/man-pages/man3/posix_memalign.3.html>.
#[deprecated]
#[no_mangle]
pub unsafe extern "C" fn pvalloc(size: size_t) -> *mut c_void {
let page_size = Sys::getpagesize();
// Find the smallest multiple of the page size in which the requested size
// will fit. The result of the division will always be less than or equal
// to size_t::MAX - 1, and the num_pages calculation will therefore never
// overflow.
let num_pages = if size != 0 {
(size - 1) / page_size + 1
} else {
0
};
match num_pages.checked_mul(page_size) {
Some(alloc_size) => {
let ptr = platform::alloc_align(alloc_size, page_size);
if ptr.is_null() {
platform::ERRNO.set(ENOMEM);
}
ptr
}
None => {
platform::ERRNO.set(ENOMEM);
ptr::null_mut()
}
}
}