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
+
1
14
#![ cfg_attr( feature = "allocator-api" , feature( allocator_api) ) ]
2
15
#![ cfg_attr( target_arch = "wasm32" , feature( link_llvm_intrinsics) ) ]
3
16
#![ cfg_attr( not( feature = "allocator-api" ) , allow( dead_code) ) ]
4
17
#![ no_std]
18
+ #![ deny( missing_docs) ]
5
19
6
20
#[ cfg( feature = "allocator-api" ) ]
7
21
use core:: alloc:: { Alloc , Layout , AllocErr } ;
@@ -15,8 +29,14 @@ pub use self::global::GlobalDlmalloc;
15
29
mod global;
16
30
mod dlmalloc;
17
31
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!
18
37
pub struct Dlmalloc ( dlmalloc:: Dlmalloc ) ;
19
38
39
+ /// Constant initializer for `Dlmalloc` structure.
20
40
pub const DLMALLOC_INIT : Dlmalloc = Dlmalloc ( dlmalloc:: DLMALLOC_INIT ) ;
21
41
22
42
#[ cfg( target_arch = "wasm32" ) ]
@@ -32,10 +52,18 @@ mod sys;
32
52
mod sys;
33
53
34
54
impl Dlmalloc {
55
+ /// Creates a new instance of an allocator, same as `DLMALLOC_INIT`.
35
56
pub fn new ( ) -> Dlmalloc {
36
- Dlmalloc ( dlmalloc :: Dlmalloc :: new ( ) )
57
+ DLMALLOC_INIT
37
58
}
38
59
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.
39
67
#[ inline]
40
68
pub unsafe fn malloc ( & mut self , size : usize , align : usize ) -> * mut u8 {
41
69
if align <= self . 0 . malloc_alignment ( ) {
@@ -45,6 +73,8 @@ impl Dlmalloc {
45
73
}
46
74
}
47
75
76
+ /// Same as `malloc`, except if the allocation succeeds it's guaranteed to
77
+ /// point to `size` bytes of zeros.
48
78
#[ inline]
49
79
pub unsafe fn calloc ( & mut self , size : usize , align : usize ) -> * mut u8 {
50
80
let ptr = self . malloc ( size, align) ;
@@ -54,12 +84,26 @@ impl Dlmalloc {
54
84
ptr
55
85
}
56
86
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.
57
92
#[ inline]
58
93
pub unsafe fn free ( & mut self , ptr : * mut u8 , size : usize , align : usize ) {
59
94
drop ( ( size, align) ) ;
60
95
self . 0 . free ( ptr)
61
96
}
62
97
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.
63
107
#[ inline]
64
108
pub unsafe fn realloc ( & mut self ,
65
109
ptr : * mut u8 ,
0 commit comments