Skip to content

Commit c033f1f

Browse files
committed
Move platform-specific OOM handling to functions
1 parent 747cc74 commit c033f1f

File tree

1 file changed

+58
-73
lines changed

1 file changed

+58
-73
lines changed

src/liballoc_system/lib.rs

Lines changed: 58 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -224,40 +224,7 @@ mod platform {
224224
alloc_methods_based_on_global_alloc!();
225225

226226
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()
261228
}
262229
}
263230

@@ -301,8 +268,6 @@ mod platform {
301268
#[cfg(windows)]
302269
#[allow(bad_style)]
303270
mod platform {
304-
use core::ptr;
305-
306271
use MIN_ALIGN;
307272
use System;
308273
use core::alloc::{GlobalAlloc, Alloc, Void, AllocErr, Layout, CannotReallocInPlace};
@@ -312,31 +277,19 @@ mod platform {
312277
type SIZE_T = usize;
313278
type DWORD = u32;
314279
type BOOL = i32;
315-
type LPDWORD = *mut DWORD;
316-
type LPOVERLAPPED = *mut u8;
317-
318-
const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;
319280

320281
extern "system" {
321282
fn GetProcessHeap() -> HANDLE;
322283
fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
323284
fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID;
324285
fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
325286
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;
333287
}
334288

335289
#[repr(C)]
336290
struct Header(*mut u8);
337291

338292
const HEAP_ZERO_MEMORY: DWORD = 0x00000008;
339-
const HEAP_REALLOC_IN_PLACE_ONLY: DWORD = 0x00000010;
340293

341294
unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
342295
&mut *(ptr as *mut Header).offset(-1)
@@ -438,31 +391,7 @@ mod platform {
438391
}
439392

440393
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()
466395
}
467396
}
468397
}
@@ -522,3 +451,59 @@ mod platform {
522451
alloc_methods_based_on_global_alloc!();
523452
}
524453
}
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

Comments
 (0)