1+ //! A Rust port of the `dlmalloc` allocator.
2+ //!
3+ //! The `dlmalloc` allocator is described at
4+ //! http://g.oswego.edu/dl/html/malloc.html and this Rust crate is a straight
5+ //! port of the C code for the allocator into Rust. The implementation is
6+ //! wrapped up in a `Dlmalloc` type and has support for Linux, OSX, and Wasm
7+ //! currently.
8+ //!
9+ //! The primary purpose of this crate is that it serves as the default memory
10+ //! allocator for the `wasm32-unknown-unknown` target in the standard library.
11+ //! Support for other platforms is largely untested and unused, but is used when
12+ //! testing this crate.
13+
114#![ cfg_attr( feature = "allocator-api" , feature( allocator_api) ) ]
215#![ cfg_attr( target_arch = "wasm32" , feature( link_llvm_intrinsics) ) ]
316#![ cfg_attr( not( feature = "allocator-api" ) , allow( dead_code) ) ]
417#![ no_std]
18+ #![ deny( missing_docs) ]
519
620#[ cfg( feature = "allocator-api" ) ]
721use core:: alloc:: { Alloc , Layout , AllocErr } ;
@@ -15,8 +29,14 @@ pub use self::global::GlobalDlmalloc;
1529mod global;
1630mod dlmalloc;
1731
32+ /// An allocator instance
33+ ///
34+ /// Instances of this type are used to allocate blocks of memory. For best
35+ /// results only use one of these. Currently doesn't implement `Drop` to release
36+ /// lingering memory back to the OS. That may happen eventually though!
1837pub struct Dlmalloc ( dlmalloc:: Dlmalloc ) ;
1938
39+ /// Constant initializer for `Dlmalloc` structure.
2040pub const DLMALLOC_INIT : Dlmalloc = Dlmalloc ( dlmalloc:: DLMALLOC_INIT ) ;
2141
2242#[ cfg( target_arch = "wasm32" ) ]
@@ -32,10 +52,18 @@ mod sys;
3252mod sys;
3353
3454impl Dlmalloc {
55+ /// Creates a new instance of an allocator, same as `DLMALLOC_INIT`.
3556 pub fn new ( ) -> Dlmalloc {
36- Dlmalloc ( dlmalloc :: Dlmalloc :: new ( ) )
57+ DLMALLOC_INIT
3758 }
3859
60+ /// Allocates `size` bytes with `align` align.
61+ ///
62+ /// Returns a null pointer if allocation fails. Returns a valid pointer
63+ /// otherwise.
64+ ///
65+ /// Safety and contracts are largely governed by the `GlobalAlloc::alloc`
66+ /// method contracts.
3967 #[ inline]
4068 pub unsafe fn malloc ( & mut self , size : usize , align : usize ) -> * mut u8 {
4169 if align <= self . 0 . malloc_alignment ( ) {
@@ -45,6 +73,8 @@ impl Dlmalloc {
4573 }
4674 }
4775
76+ /// Same as `malloc`, except if the allocation succeeds it's guaranteed to
77+ /// point to `size` bytes of zeros.
4878 #[ inline]
4979 pub unsafe fn calloc ( & mut self , size : usize , align : usize ) -> * mut u8 {
5080 let ptr = self . malloc ( size, align) ;
@@ -54,12 +84,26 @@ impl Dlmalloc {
5484 ptr
5585 }
5686
87+ /// Deallocates a `ptr` with `size` and `align` as the previous request used
88+ /// to allocate it.
89+ ///
90+ /// Safety and contracts are largely governed by the `GlobalAlloc::dealloc`
91+ /// method contracts.
5792 #[ inline]
5893 pub unsafe fn free ( & mut self , ptr : * mut u8 , size : usize , align : usize ) {
5994 drop ( ( size, align) ) ;
6095 self . 0 . free ( ptr)
6196 }
6297
98+ /// Reallocates `ptr`, a previous allocation with `old_size` and
99+ /// `old_align`, to have `new_size` and the same alignment as before.
100+ ///
101+ /// Returns a null pointer if the memory couldn't be reallocated, but `ptr`
102+ /// is still valid. Returns a valid pointer and frees `ptr` if the request
103+ /// is satisfied.
104+ ///
105+ /// Safety and contracts are largely governed by the `GlobalAlloc::realloc`
106+ /// method contracts.
63107 #[ inline]
64108 pub unsafe fn realloc ( & mut self ,
65109 ptr : * mut u8 ,
0 commit comments