Skip to content
Snippets Groups Projects
Commit 742339ca authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Hacky version of memalign

parent dabd8dc6
No related branches found
No related tags found
No related merge requests found
...@@ -323,6 +323,24 @@ pub unsafe extern "C" fn malloc(size: size_t) -> *mut c_void { ...@@ -323,6 +323,24 @@ pub unsafe extern "C" fn malloc(size: size_t) -> *mut c_void {
} }
} }
#[no_mangle]
pub unsafe extern "C" fn memalign(alignment: size_t, size: size_t) -> *mut c_void {
let mut align = 16;
while align <= alignment {
align *= 2;
}
let offset = align/2;
let ptr = ralloc::alloc(size + offset, align);
if !ptr.is_null() {
*(ptr as *mut u64) = (size + offset) as u64;
*(ptr as *mut u64).offset(1) = align as u64;
ptr.offset(offset as isize) as *mut c_void
} else {
ptr as *mut c_void
}
}
#[no_mangle] #[no_mangle]
pub extern "C" fn mblen(s: *const c_char, n: size_t) -> c_int { pub extern "C" fn mblen(s: *const c_char, n: size_t) -> c_int {
unimplemented!(); unimplemented!();
......
...@@ -10,11 +10,19 @@ int main(int argc, char ** argv) { ...@@ -10,11 +10,19 @@ int main(int argc, char ** argv) {
} }
free(ptr); free(ptr);
char * ptrc = (char *)calloc(256,1); char * ptrc = (char *)calloc(256, 1);
printf("calloc %p\n", ptrc); printf("calloc %p\n", ptrc);
for(i = 0; i < 256; i++) { for(i = 0; i < 256; i++) {
ptrc[i] = (char)i; ptrc[i] = (char)i;
} }
free(ptrc); free(ptrc);
char * ptra = (char *)memalign(256, 256);
printf("memalign %p\n", ptra);
for(i = 0; i < 256; i++) {
ptra[i] = (char)i;
}
free(ptra);
return 0; return 0;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment