|
| 1 | +//! Rust implementation of C library function `malloc`, `calloc`, `realloc`, and `free`. |
| 2 | +//! |
| 3 | +//! Copyright (c) Gyungmin Myung <[email protected]> |
| 4 | +//! This file is licensed under the Blue Oak Model Licence 1.0.0 |
| 5 | +
|
| 6 | +extern crate alloc; |
| 7 | +use crate::CSizeT; |
| 8 | + |
| 9 | +// The maximum alignment of any fundamental type. Equivalent to max_align_t |
| 10 | +const MAX_ALIGN: usize = 16; |
| 11 | + |
| 12 | +/// Rust implementation of C library function `malloc` |
| 13 | +/// |
| 14 | +/// See [malloc](https://linux.die.net/man/3/malloc) for alignment details. |
| 15 | +#[no_mangle] |
| 16 | +pub unsafe extern "C" fn malloc(size: CSizeT) -> *mut u8 { |
| 17 | + // size + MAX_ALIGN for to store the size of the allocated memory. |
| 18 | + let layout = alloc::alloc::Layout::from_size_align(size + MAX_ALIGN, MAX_ALIGN).unwrap(); |
| 19 | + let ptr = unsafe { alloc::alloc::alloc(layout) }; |
| 20 | + if ptr.is_null() { |
| 21 | + return ptr; |
| 22 | + } |
| 23 | + unsafe { |
| 24 | + *(ptr as *mut CSizeT) = size; |
| 25 | + } |
| 26 | + unsafe { ptr.add(MAX_ALIGN) } |
| 27 | +} |
| 28 | + |
| 29 | +/// Rust implementation of C library function `calloc` |
| 30 | +/// |
| 31 | +/// See [calloc](https://linux.die.net/man/3/calloc) for alignment details. |
| 32 | +#[no_mangle] |
| 33 | +pub unsafe extern "C" fn calloc(nmemb: CSizeT, size: CSizeT) -> *mut u8 { |
| 34 | + let total_size = nmemb * size; |
| 35 | + let layout = alloc::alloc::Layout::from_size_align(total_size + MAX_ALIGN, MAX_ALIGN).unwrap(); |
| 36 | + let ptr = unsafe { alloc::alloc::alloc_zeroed(layout) }; |
| 37 | + if ptr.is_null() { |
| 38 | + return ptr; |
| 39 | + } |
| 40 | + unsafe { |
| 41 | + *(ptr as *mut CSizeT) = total_size; |
| 42 | + } |
| 43 | + unsafe { ptr.add(MAX_ALIGN) } |
| 44 | +} |
| 45 | + |
| 46 | +/// Rust implementation of C library function `realloc` |
| 47 | +/// |
| 48 | +/// See [realloc](https://linux.die.net/man/3/realloc) for alignment details. |
| 49 | +#[no_mangle] |
| 50 | +pub unsafe extern "C" fn realloc(ptr: *mut u8, size: CSizeT) -> *mut u8 { |
| 51 | + if ptr.is_null() { |
| 52 | + return malloc(size); |
| 53 | + } |
| 54 | + let old_size = unsafe { *(ptr.sub(MAX_ALIGN) as *mut CSizeT) }; |
| 55 | + let layout = alloc::alloc::Layout::from_size_align(old_size + MAX_ALIGN, MAX_ALIGN).unwrap(); |
| 56 | + let new_ptr = unsafe { alloc::alloc::realloc(ptr.sub(MAX_ALIGN), layout, size + MAX_ALIGN) }; |
| 57 | + if new_ptr.is_null() { |
| 58 | + return new_ptr; |
| 59 | + } |
| 60 | + unsafe { |
| 61 | + *(new_ptr as *mut CSizeT) = size; |
| 62 | + } |
| 63 | + unsafe { new_ptr.add(MAX_ALIGN) } |
| 64 | +} |
| 65 | + |
| 66 | +/// Rust implementation of C library function `free` |
| 67 | +#[no_mangle] |
| 68 | +pub unsafe extern "C" fn free(ptr: *mut u8) { |
| 69 | + if ptr.is_null() { |
| 70 | + return; |
| 71 | + } |
| 72 | + let old_size = unsafe { *(ptr.sub(MAX_ALIGN) as *mut CSizeT) }; |
| 73 | + let layout = alloc::alloc::Layout::from_size_align(old_size + MAX_ALIGN, MAX_ALIGN).unwrap(); |
| 74 | + unsafe { alloc::alloc::dealloc(ptr.sub(MAX_ALIGN), layout) }; |
| 75 | +} |
| 76 | + |
| 77 | +#[cfg(test)] |
| 78 | +mod test { |
| 79 | + use super::*; |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn test_malloc() { |
| 83 | + let ptr = unsafe { malloc(10) }; |
| 84 | + assert!(!ptr.is_null()); |
| 85 | + unsafe { |
| 86 | + assert_eq!(*(ptr.sub(MAX_ALIGN) as *mut CSizeT), 10); |
| 87 | + (0..10).for_each(|i| { |
| 88 | + *ptr.add(i) = i as u8; |
| 89 | + }); |
| 90 | + (0..10).for_each(|i| { |
| 91 | + assert_eq!(*ptr.add(i), i as u8); |
| 92 | + }); |
| 93 | + } |
| 94 | + unsafe { free(ptr) }; |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn test_calloc() { |
| 99 | + let ptr = unsafe { calloc(10, 10) }; |
| 100 | + assert!(!ptr.is_null()); |
| 101 | + unsafe { |
| 102 | + assert_eq!(*(ptr.sub(MAX_ALIGN) as *mut CSizeT), 100); |
| 103 | + (0..100).for_each(|i| { |
| 104 | + assert_eq!(*ptr.add(i), 0); |
| 105 | + }); |
| 106 | + (0..100).for_each(|i| { |
| 107 | + *ptr.add(i) = i as u8; |
| 108 | + }); |
| 109 | + (0..100).for_each(|i| { |
| 110 | + assert_eq!(*ptr.add(i), i as u8); |
| 111 | + }); |
| 112 | + } |
| 113 | + unsafe { free(ptr) }; |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn test_realloc() { |
| 118 | + let ptr = unsafe { malloc(10) }; |
| 119 | + assert!(!ptr.is_null()); |
| 120 | + unsafe { |
| 121 | + assert_eq!(*(ptr.sub(MAX_ALIGN) as *mut CSizeT), 10); |
| 122 | + (0..10).for_each(|i| { |
| 123 | + *ptr.add(i) = i as u8; |
| 124 | + }); |
| 125 | + (0..10).for_each(|i| { |
| 126 | + assert_eq!(*ptr.add(i), i as u8); |
| 127 | + }); |
| 128 | + } |
| 129 | + let ptr = unsafe { realloc(ptr, 20) }; |
| 130 | + assert!(!ptr.is_null()); |
| 131 | + unsafe { |
| 132 | + assert_eq!(*(ptr.sub(MAX_ALIGN) as *mut CSizeT), 20); |
| 133 | + (0..10).for_each(|i| { |
| 134 | + assert_eq!(*ptr.add(i), i as u8); |
| 135 | + }); |
| 136 | + (10..20).for_each(|i| { |
| 137 | + *ptr.add(i) = i as u8; |
| 138 | + }); |
| 139 | + (10..20).for_each(|i| { |
| 140 | + assert_eq!(*ptr.add(i), i as u8); |
| 141 | + }); |
| 142 | + } |
| 143 | + unsafe { free(ptr) }; |
| 144 | + } |
| 145 | +} |
0 commit comments