1
- // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
1
+ // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2
2
// file at the top-level directory of this distribution and at
3
3
// http://rust-lang.org/COPYRIGHT.
4
4
//
@@ -44,6 +44,11 @@ pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
44
44
unsafe { imp:: allocate ( size, align) }
45
45
}
46
46
47
+ #[ no_mangle]
48
+ pub extern "C" fn __rust_allocate_zeroed ( size : usize , align : usize ) -> * mut u8 {
49
+ unsafe { imp:: allocate_zeroed ( size, align) }
50
+ }
51
+
47
52
#[ no_mangle]
48
53
pub extern "C" fn __rust_deallocate ( ptr : * mut u8 , old_size : usize , align : usize ) {
49
54
unsafe { imp:: deallocate ( ptr, old_size, align) }
@@ -121,6 +126,18 @@ mod imp {
121
126
}
122
127
}
123
128
129
+ pub unsafe fn allocate_zeroed ( size : usize , align : usize ) -> * mut u8 {
130
+ if align <= MIN_ALIGN {
131
+ libc:: calloc ( size as libc:: size_t , 1 ) as * mut u8
132
+ } else {
133
+ let ptr = aligned_malloc ( size, align) ;
134
+ if !ptr. is_null ( ) {
135
+ ptr:: write_bytes ( ptr, 0 , size) ;
136
+ }
137
+ ptr
138
+ }
139
+ }
140
+
124
141
pub unsafe fn reallocate ( ptr : * mut u8 , old_size : usize , size : usize , align : usize ) -> * mut u8 {
125
142
if align <= MIN_ALIGN {
126
143
libc:: realloc ( ptr as * mut libc:: c_void , size as libc:: size_t ) as * mut u8
@@ -173,6 +190,8 @@ mod imp {
173
190
#[ repr( C ) ]
174
191
struct Header ( * mut u8 ) ;
175
192
193
+
194
+ const HEAP_ZERO_MEMORY : DWORD = 0x00000008 ;
176
195
const HEAP_REALLOC_IN_PLACE_ONLY : DWORD = 0x00000010 ;
177
196
178
197
unsafe fn get_header < ' a > ( ptr : * mut u8 ) -> & ' a mut Header {
@@ -185,18 +204,27 @@ mod imp {
185
204
aligned
186
205
}
187
206
188
- pub unsafe fn allocate ( size : usize , align : usize ) -> * mut u8 {
207
+ #[ inline]
208
+ unsafe fn allocate_with_flags ( size : usize , align : usize , flags : DWORD ) -> * mut u8 {
189
209
if align <= MIN_ALIGN {
190
- HeapAlloc ( GetProcessHeap ( ) , 0 , size as SIZE_T ) as * mut u8
210
+ HeapAlloc ( GetProcessHeap ( ) , flags , size as SIZE_T ) as * mut u8
191
211
} else {
192
- let ptr = HeapAlloc ( GetProcessHeap ( ) , 0 , ( size + align) as SIZE_T ) as * mut u8 ;
212
+ let ptr = HeapAlloc ( GetProcessHeap ( ) , flags , ( size + align) as SIZE_T ) as * mut u8 ;
193
213
if ptr. is_null ( ) {
194
214
return ptr;
195
215
}
196
216
align_ptr ( ptr, align)
197
217
}
198
218
}
199
219
220
+ pub unsafe fn allocate ( size : usize , align : usize ) -> * mut u8 {
221
+ allocate_with_flags ( size, align, 0 )
222
+ }
223
+
224
+ pub unsafe fn allocate_zeroed ( size : usize , align : usize ) -> * mut u8 {
225
+ allocate_with_flags ( size, align, HEAP_ZERO_MEMORY )
226
+ }
227
+
200
228
pub unsafe fn reallocate ( ptr : * mut u8 , _old_size : usize , size : usize , align : usize ) -> * mut u8 {
201
229
if align <= MIN_ALIGN {
202
230
HeapReAlloc ( GetProcessHeap ( ) , 0 , ptr as LPVOID , size as SIZE_T ) as * mut u8
0 commit comments