diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..77986f3 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,28 @@ +edition = "2021" + +condense_wildcard_suffixes = true +format_code_in_doc_comments = true +format_macro_bodies = true +format_macro_matchers = true +format_strings = true +hex_literal_case = "Upper" +max_width = 120 +tab_spaces = 4 +array_width = 80 +comment_width = 80 +wrap_comments = true +fn_params_layout = "Compressed" +fn_call_width = 80 +fn_single_line = true +hard_tabs = true +match_block_trailing_comma = true +imports_granularity = "Crate" +normalize_comments = false +reorder_impl_items = true +reorder_imports = true +group_imports = "StdExternalCrate" +newline_style = "Unix" +use_field_init_shorthand = true +use_small_heuristics = "Off" +use_try_shorthand = true +chain_width = 60 \ No newline at end of file diff --git a/src/bindings.rs b/src/bindings.rs index 47164ed..6dea7f4 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -1,10 +1,10 @@ -use core::ffi::{c_void}; +use core::ffi::c_void; #[allow(dead_code)] extern "C" { - /* C standard */ - pub fn malloc(size: usize) -> *mut c_void; - pub fn calloc(nmemb: usize, size: usize) -> *mut c_void; - pub fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void; - pub fn free(ptr: *mut c_void); -} \ No newline at end of file + /* C standard */ + pub fn malloc(size: usize) -> *mut c_void; + pub fn calloc(nmemb: usize, size: usize) -> *mut c_void; + pub fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void; + pub fn free(ptr: *mut c_void); +} diff --git a/src/lib.rs b/src/lib.rs index c58b662..c29bb5e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,27 +1,27 @@ #![no_std] mod bindings; -pub use bindings::{malloc, calloc, free, realloc}; -use core::alloc::{GlobalAlloc, Layout}; -use core::ffi::c_void; +use core::{ + alloc::{GlobalAlloc, Layout}, + ffi::c_void, +}; + +pub use bindings::{calloc, free, malloc, realloc}; pub struct HardenedMalloc; unsafe impl GlobalAlloc for HardenedMalloc { - #[inline] - unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - malloc(layout.size()) as *mut u8 - } - #[inline] - unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { - calloc(layout.size(), 1) as *mut u8 - } - #[inline] - unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { - free(ptr as *mut c_void); - } - #[inline] - unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, size: usize) -> *mut u8 { - realloc(ptr as *mut c_void, size) as *mut u8 - } + #[inline] + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { malloc(layout.size()) as *mut u8 } + + #[inline] + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { calloc(layout.size(), 1) as *mut u8 } + + #[inline] + unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { free(ptr as *mut c_void); } + + #[inline] + unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, size: usize) -> *mut u8 { + realloc(ptr as *mut c_void, size) as *mut u8 + } } diff --git a/tests/src/main.rs b/tests/src/main.rs index 15a7230..9653a87 100644 --- a/tests/src/main.rs +++ b/tests/src/main.rs @@ -1,11 +1,11 @@ use hardened_malloc_rs::HardenedMalloc; #[global_allocator] -static GLOBAL: HardenedMalloc = HardenedMalloc; +static GLOBAL: HardenedMalloc = HardenedMalloc; fn main() { - let mut v = Vec::new(); - v.push(1); - v.resize(5, 0); - println!("v has value: {:?}", v); + let mut v = Vec::new(); + v.push(1); + v.resize(5, 0); + println!("v has value: {:?}", v); }