@@ -224,40 +224,7 @@ mod platform {
224
224
alloc_methods_based_on_global_alloc ! ( ) ;
225
225
226
226
fn oom ( & mut self ) -> ! {
227
- use core:: fmt:: { self , Write } ;
228
-
229
- // Print a message to stderr before aborting to assist with
230
- // debugging. It is critical that this code does not allocate any
231
- // memory since we are in an OOM situation. Any errors are ignored
232
- // while printing since there's nothing we can do about them and we
233
- // are about to exit anyways.
234
- drop ( writeln ! ( Stderr , "fatal runtime error: {}" , AllocErr ) ) ;
235
- unsafe {
236
- :: core:: intrinsics:: abort ( ) ;
237
- }
238
-
239
- struct Stderr ;
240
-
241
- impl Write for Stderr {
242
- #[ cfg( target_os = "cloudabi" ) ]
243
- fn write_str ( & mut self , _: & str ) -> fmt:: Result {
244
- // CloudABI does not have any reserved file descriptor
245
- // numbers. We should not attempt to write to file
246
- // descriptor #2, as it may be associated with any kind of
247
- // resource.
248
- Ok ( ( ) )
249
- }
250
-
251
- #[ cfg( not( target_os = "cloudabi" ) ) ]
252
- fn write_str ( & mut self , s : & str ) -> fmt:: Result {
253
- unsafe {
254
- libc:: write ( libc:: STDERR_FILENO ,
255
- s. as_ptr ( ) as * const libc:: c_void ,
256
- s. len ( ) ) ;
257
- }
258
- Ok ( ( ) )
259
- }
260
- }
227
+ :: oom ( )
261
228
}
262
229
}
263
230
@@ -301,8 +268,6 @@ mod platform {
301
268
#[ cfg( windows) ]
302
269
#[ allow( bad_style) ]
303
270
mod platform {
304
- use core:: ptr;
305
-
306
271
use MIN_ALIGN ;
307
272
use System ;
308
273
use core:: alloc:: { GlobalAlloc , Alloc , Void , AllocErr , Layout , CannotReallocInPlace } ;
@@ -312,31 +277,19 @@ mod platform {
312
277
type SIZE_T = usize ;
313
278
type DWORD = u32 ;
314
279
type BOOL = i32 ;
315
- type LPDWORD = * mut DWORD ;
316
- type LPOVERLAPPED = * mut u8 ;
317
-
318
- const STD_ERROR_HANDLE : DWORD = -12i32 as DWORD ;
319
280
320
281
extern "system" {
321
282
fn GetProcessHeap ( ) -> HANDLE ;
322
283
fn HeapAlloc ( hHeap : HANDLE , dwFlags : DWORD , dwBytes : SIZE_T ) -> LPVOID ;
323
284
fn HeapReAlloc ( hHeap : HANDLE , dwFlags : DWORD , lpMem : LPVOID , dwBytes : SIZE_T ) -> LPVOID ;
324
285
fn HeapFree ( hHeap : HANDLE , dwFlags : DWORD , lpMem : LPVOID ) -> BOOL ;
325
286
fn GetLastError ( ) -> DWORD ;
326
- fn WriteFile ( hFile : HANDLE ,
327
- lpBuffer : LPVOID ,
328
- nNumberOfBytesToWrite : DWORD ,
329
- lpNumberOfBytesWritten : LPDWORD ,
330
- lpOverlapped : LPOVERLAPPED )
331
- -> BOOL ;
332
- fn GetStdHandle ( which : DWORD ) -> HANDLE ;
333
287
}
334
288
335
289
#[ repr( C ) ]
336
290
struct Header ( * mut u8 ) ;
337
291
338
292
const HEAP_ZERO_MEMORY : DWORD = 0x00000008 ;
339
- const HEAP_REALLOC_IN_PLACE_ONLY : DWORD = 0x00000010 ;
340
293
341
294
unsafe fn get_header < ' a > ( ptr : * mut u8 ) -> & ' a mut Header {
342
295
& mut * ( ptr as * mut Header ) . offset ( -1 )
@@ -438,31 +391,7 @@ mod platform {
438
391
}
439
392
440
393
fn oom ( & mut self ) -> ! {
441
- use core:: fmt:: { self , Write } ;
442
-
443
- // Same as with unix we ignore all errors here
444
- drop ( writeln ! ( Stderr , "fatal runtime error: {}" , AllocErr ) ) ;
445
- unsafe {
446
- :: core:: intrinsics:: abort ( ) ;
447
- }
448
-
449
- struct Stderr ;
450
-
451
- impl Write for Stderr {
452
- fn write_str ( & mut self , s : & str ) -> fmt:: Result {
453
- unsafe {
454
- // WriteFile silently fails if it is passed an invalid
455
- // handle, so there is no need to check the result of
456
- // GetStdHandle.
457
- WriteFile ( GetStdHandle ( STD_ERROR_HANDLE ) ,
458
- s. as_ptr ( ) as LPVOID ,
459
- s. len ( ) as DWORD ,
460
- ptr:: null_mut ( ) ,
461
- ptr:: null_mut ( ) ) ;
462
- }
463
- Ok ( ( ) )
464
- }
465
- }
394
+ :: oom ( )
466
395
}
467
396
}
468
397
}
@@ -522,3 +451,59 @@ mod platform {
522
451
alloc_methods_based_on_global_alloc ! ( ) ;
523
452
}
524
453
}
454
+
455
+ fn oom ( ) -> ! {
456
+ write_to_stderr ( "fatal runtime error: memory allocation failed" ) ;
457
+ unsafe {
458
+ :: core:: intrinsics:: abort ( ) ;
459
+ }
460
+ }
461
+
462
+ #[ cfg( any( unix, target_os = "redox" ) ) ]
463
+ fn write_to_stderr ( s : & str ) {
464
+ extern crate libc;
465
+
466
+ unsafe {
467
+ libc:: write ( libc:: STDERR_FILENO ,
468
+ s. as_ptr ( ) as * const libc:: c_void ,
469
+ s. len ( ) ) ;
470
+ }
471
+ }
472
+
473
+ #[ cfg( windows) ]
474
+ fn write_to_stderr ( s : & str ) {
475
+ use core:: ptr;
476
+
477
+ type LPVOID = * mut u8 ;
478
+ type HANDLE = LPVOID ;
479
+ type DWORD = u32 ;
480
+ type BOOL = i32 ;
481
+ type LPDWORD = * mut DWORD ;
482
+ type LPOVERLAPPED = * mut u8 ;
483
+
484
+ const STD_ERROR_HANDLE : DWORD = -12i32 as DWORD ;
485
+
486
+ extern "system" {
487
+ fn WriteFile ( hFile : HANDLE ,
488
+ lpBuffer : LPVOID ,
489
+ nNumberOfBytesToWrite : DWORD ,
490
+ lpNumberOfBytesWritten : LPDWORD ,
491
+ lpOverlapped : LPOVERLAPPED )
492
+ -> BOOL ;
493
+ fn GetStdHandle ( which : DWORD ) -> HANDLE ;
494
+ }
495
+
496
+ unsafe {
497
+ // WriteFile silently fails if it is passed an invalid
498
+ // handle, so there is no need to check the result of
499
+ // GetStdHandle.
500
+ WriteFile ( GetStdHandle ( STD_ERROR_HANDLE ) ,
501
+ s. as_ptr ( ) as LPVOID ,
502
+ s. len ( ) as DWORD ,
503
+ ptr:: null_mut ( ) ,
504
+ ptr:: null_mut ( ) ) ;
505
+ }
506
+ }
507
+
508
+ #[ cfg( not( any( windows, unix, target_os = "redox" ) ) ) ]
509
+ fn write_to_stderr ( _: & str ) { }
0 commit comments