@@ -31,27 +31,26 @@ mod dlmalloc;
31
31
mod global;
32
32
33
33
/// A platform interface
34
- pub trait System : Send {
34
+ pub unsafe trait System : Send {
35
35
/// Allocates system memory region of at least `size` bytes
36
36
/// Returns a triple of `(base, size, flags)` where `base` is a pointer to the beginning of the
37
37
/// allocated memory region. `size` is the actual size of the region while `flags` specifies
38
38
/// properties of the allocated region. If `EXTERN_BIT` (bit 0) set in flags, then we did not
39
39
/// allocate this segment and so should not try to deallocate or merge with others.
40
- unsafe fn alloc ( & self , size : usize ) -> ( * mut u8 , usize , u32 ) ;
40
+ fn alloc ( & self , size : usize ) -> ( * mut u8 , usize , u32 ) ;
41
41
42
42
/// Remaps system memory region at `ptr` with size `oldsize` to a potential new location with
43
43
/// size `newsize`. `can_move` indicates if the location is allowed to move to a completely new
44
44
/// location, or that it is only allowed to change in size. Returns a pointer to the new
45
45
/// location in memory.
46
- unsafe fn remap ( & self , ptr : * mut u8 , oldsize : usize , newsize : usize , can_move : bool )
47
- -> * mut u8 ;
46
+ fn remap ( & self , ptr : * mut u8 , oldsize : usize , newsize : usize , can_move : bool ) -> * mut u8 ;
48
47
49
48
/// Resizes a memory chunk starting at `ptr` with size `oldsize` to a memory region of
50
49
/// `newsize` bytes. Returns `true` if the memory region could be resized.
51
- unsafe fn free_part ( & self , ptr : * mut u8 , oldsize : usize , newsize : usize ) -> bool ;
50
+ fn free_part ( & self , ptr : * mut u8 , oldsize : usize , newsize : usize ) -> bool ;
52
51
53
52
/// Frees an entire memory region. Returns `true` if the operation succeeded
54
- unsafe fn free ( & self , ptr : * mut u8 , size : usize ) -> bool ;
53
+ fn free ( & self , ptr : * mut u8 , size : usize ) -> bool ;
55
54
56
55
/// Indicates if the platform can release a part of memory. For the `flags` argument, see
57
56
/// `System::alloc`
@@ -92,18 +91,59 @@ mod sys;
92
91
#[ path = "linux.rs" ]
93
92
mod sys;
94
93
95
- impl Dlmalloc {
94
+ #[ cfg( not( any( target_os = "linux" , target_os = "macos" , target_arch = "wasm32" ) ) ) ]
95
+ pub struct Platform {
96
+ _priv : ( ) ,
97
+ }
98
+
99
+ #[ cfg( not( any( target_os = "linux" , target_os = "macos" , target_arch = "wasm32" ) ) ) ]
100
+ impl Platform {
101
+ const fn new ( ) -> Platform {
102
+ Platform { _priv : ( ) }
103
+ }
104
+ }
105
+
106
+ #[ cfg( not( any( target_os = "linux" , target_os = "macos" , target_arch = "wasm32" ) ) ) ]
107
+ unsafe impl System for Platform {
108
+ fn alloc ( & self , size : usize ) -> ( * mut u8 , usize , u32 ) {
109
+ panic ! ( "Please implement the `System` interface and use the `Dlmalloc::new_with_platform` method" ) ;
110
+ }
111
+
112
+ fn remap ( & self , ptr : * mut u8 , oldsize : usize , newsize : usize , can_move : bool ) -> * mut u8 {
113
+ panic ! ( "Please implement the `System` interface and use the `Dlmalloc::new_with_platform` method" ) ;
114
+ }
115
+
116
+ fn free_part ( & self , ptr : * mut u8 , oldsize : usize , newsize : usize ) -> bool {
117
+ panic ! ( "Please implement the `System` interface and use the `Dlmalloc::new_with_platform` method" ) ;
118
+ }
119
+
120
+ fn free ( & self , ptr : * mut u8 , size : usize ) -> bool {
121
+ panic ! ( "Please implement the `System` interface and use the `Dlmalloc::new_with_platform` method" ) ;
122
+ }
123
+
124
+ fn can_release_part ( & self , flags : u32 ) -> bool {
125
+ panic ! ( "Please implement the `System` interface and use the `Dlmalloc::new_with_platform` method" ) ;
126
+ }
127
+
128
+ fn allocates_zeros ( & self ) -> bool {
129
+ panic ! ( "Please implement the `System` interface and use the `Dlmalloc::new_with_platform` method" ) ;
130
+ }
131
+
132
+ fn page_size ( & self ) -> usize {
133
+ panic ! ( "Please implement the `System` interface and use the `Dlmalloc::new_with_platform` method" ) ;
134
+ }
135
+ }
136
+
137
+ impl Dlmalloc < Platform > {
96
138
/// Creates a new instance of an allocator
97
- #[ cfg( any( target_os = "linux" , target_arch = "wasm32" , target_os = "macos" ) ) ]
98
139
pub const fn new ( ) -> Dlmalloc < Platform > {
99
140
Dlmalloc ( dlmalloc:: Dlmalloc :: new ( Platform :: new ( ) ) )
100
141
}
101
142
}
102
143
103
144
impl < S > Dlmalloc < S > {
104
145
/// Creates a new instance of an allocator
105
- #[ cfg( not( any( target_os = "linux" , target_arch = "wasm32" , target_os = "macos" ) ) ) ]
106
- pub const fn new ( sys_allocator : S ) -> Dlmalloc < S > {
146
+ pub const fn new_with_platform ( sys_allocator : S ) -> Dlmalloc < S > {
107
147
Dlmalloc ( dlmalloc:: Dlmalloc :: new ( sys_allocator) )
108
148
}
109
149
}
0 commit comments