chore: add rustfmt

This commit is contained in:
Evan Huff 2024-04-08 16:58:00 -04:00 committed by June
parent f5cb0d0843
commit 04ca572296
4 changed files with 59 additions and 31 deletions

28
rustfmt.toml Normal file
View file

@ -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

View file

@ -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);
}
/* 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);
}

View file

@ -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
}
}

View file

@ -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);
}