Commit dab6530f authored by jD91mZM2's avatar jD91mZM2

Merge branch 'valloc_pagesize' into 'master'

Make valloc() get page size through sysconf(), add tests

See merge request !216
parents f1b88d9e f8cf25d7
Pipeline #4162 passed with stages
in 18 minutes and 58 seconds
//! stdlib implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/stdlib.h.html
use core::{intrinsics, iter, mem, ptr, slice};
use core::convert::TryFrom;
use rand::distributions::{Alphanumeric, Distribution, Uniform};
use rand::prng::XorShiftRng;
use rand::rngs::JitterRng;
......@@ -14,6 +15,7 @@ use header::limits;
use header::string::*;
use header::time::constants::CLOCK_MONOTONIC;
use header::time::timespec;
use header::unistd::{sysconf, _SC_PAGESIZE};
use header::wchar::*;
use header::{ctype, errno, unistd};
use platform;
......@@ -1016,7 +1018,23 @@ pub unsafe extern "C" fn unsetenv(key: *const c_char) -> c_int {
#[no_mangle]
pub unsafe extern "C" fn valloc(size: size_t) -> *mut c_void {
memalign(4096, size)
/* sysconf(_SC_PAGESIZE) is a c_long and may in principle not
* convert correctly to a size_t. */
match size_t::try_from(sysconf(_SC_PAGESIZE)) {
Ok(page_size) => {
/* valloc() is not supposed to be able to set errno to
* EINVAL, hence no call to memalign(). */
let ptr = platform::alloc_align(size, page_size);
if ptr.is_null() {
platform::errno = errno::ENOMEM;
}
ptr
}
Err(_) => {
// A corner case. No errno setting.
ptr::null_mut()
}
}
}
#[no_mangle]
......
......@@ -126,4 +126,17 @@ int main(void) {
ptr_aligned_alloc_badsize, aligned_alloc_badsize_errno,
strerror(aligned_alloc_badsize_errno));
free(ptr_aligned_alloc_badsize);
errno = 0;
char * ptr_valloc = (char *)valloc(sample_alloc_size);
int valloc_errno = errno;
printf("valloc : %p, errno: %d = %s\n",
ptr_valloc, valloc_errno, strerror(valloc_errno));
errno = 0;
char * ptr_valloc_maxsize = (char *)valloc(max_size);
int valloc_maxsize_errno = errno;
printf("valloc (SIZE_MAX) : %p, errno: %d = %s\n",
ptr_valloc_maxsize, valloc_maxsize_errno,
strerror(valloc_maxsize_errno));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment