11// Copyright 2019 Octavian Oncescu
22
3+ #![ feature( no_std) ]
4+ #![ no_std]
5+
36//! A drop-in global allocator wrapper around the [mimalloc](https://github.com/microsoft/mimalloc) allocator.
47//! Mimalloc is a general purpose, performance oriented allocator built by Microsoft.
58//!
1518//! By default this library builds mimalloc in safe-mode. This means that
1619//! heap allocations are encrypted, but this results in a 3% increase in overhead.
1720//!
18- //! In `Cargo.toml`:
21+ //! To disable secure mode, in `Cargo.toml`:
1922//! ```rust,ignore
2023//! [dependencies]
21- //! mimalloc = { version = "*", features = ["no_secure"] }
24+ //! mimalloc = { version = "*", default- features = false }
2225//! ```
2326
2427extern crate libmimalloc_sys as ffi;
2528
26- use std :: alloc:: { GlobalAlloc , Layout } ;
29+ use core :: alloc:: { GlobalAlloc , Layout } ;
2730use libc:: c_void;
2831use ffi:: * ;
2932
@@ -62,24 +65,20 @@ pub struct MiMalloc;
6265unsafe impl GlobalAlloc for MiMalloc {
6366 #[ inline]
6467 unsafe fn alloc ( & self , layout : Layout ) -> * mut u8 {
65- let align = if layout. align ( ) > MIN_ALIGN {
66- layout. align ( )
68+ if layout . align ( ) <= MIN_ALIGN && layout. align ( ) <= layout . size ( ) {
69+ mi_malloc ( layout. size ( ) ) as * mut u8
6770 } else {
68- MIN_ALIGN
69- } ;
70-
71- mi_malloc_aligned ( layout. size ( ) , align) as * mut u8
71+ mi_malloc_aligned ( layout. size ( ) , layout. align ( ) ) as * mut u8
72+ }
7273 }
7374
7475 #[ inline]
7576 unsafe fn alloc_zeroed ( & self , layout : Layout ) -> * mut u8 {
76- let align = if layout. align ( ) > MIN_ALIGN {
77- layout. align ( )
77+ if layout . align ( ) <= MIN_ALIGN && layout. align ( ) <= layout . size ( ) {
78+ mi_zalloc ( layout. size ( ) ) as * mut u8
7879 } else {
79- MIN_ALIGN
80- } ;
81-
82- mi_zalloc_aligned ( layout. size ( ) , align) as * mut u8
80+ mi_zalloc_aligned ( layout. size ( ) , layout. align ( ) ) as * mut u8
81+ }
8382 }
8483
8584 #[ inline]
@@ -89,13 +88,11 @@ unsafe impl GlobalAlloc for MiMalloc {
8988
9089 #[ inline]
9190 unsafe fn realloc ( & self , ptr : * mut u8 , layout : Layout , new_size : usize ) -> * mut u8 {
92- let align = if layout. align ( ) > MIN_ALIGN {
93- layout . align ( )
91+ if layout . align ( ) <= MIN_ALIGN && layout. align ( ) <= layout . size ( ) {
92+ mi_realloc ( ptr as * const c_void , new_size ) as * mut u8
9493 } else {
95- MIN_ALIGN
96- } ;
97-
98- mi_realloc_aligned ( ptr as * const c_void , new_size, align) as * mut u8
94+ mi_realloc_aligned ( ptr as * const c_void , new_size, layout. align ( ) ) as * mut u8
95+ }
9996 }
10097}
10198
0 commit comments