]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/windows/stack_overflow.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / libstd / sys / windows / stack_overflow.rs
CommitLineData
1a4d82fc
JJ
1// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
7453a54e
SL
11#![cfg_attr(test, allow(dead_code))]
12
e9174d1e 13use sys_common::util::report_overflow;
c1a9b12d 14use sys::c;
1a4d82fc 15
e9174d1e 16pub struct Handler;
1a4d82fc
JJ
17
18impl Handler {
19 pub unsafe fn new() -> Handler {
e9174d1e
SL
20 // This API isn't available on XP, so don't panic in that case and just
21 // pray it works out ok.
22 if c::SetThreadStackGuarantee(&mut 0x5000) == 0 {
92a42be0 23 if c::GetLastError() as u32 != c::ERROR_CALL_NOT_IMPLEMENTED as u32 {
e9174d1e
SL
24 panic!("failed to reserve stack space for exception handling");
25 }
26 }
27 Handler
1a4d82fc
JJ
28 }
29}
30
e9174d1e 31extern "system" fn vectored_handler(ExceptionInfo: *mut c::EXCEPTION_POINTERS)
92a42be0 32 -> c::LONG {
1a4d82fc
JJ
33 unsafe {
34 let rec = &(*(*ExceptionInfo).ExceptionRecord);
35 let code = rec.ExceptionCode;
36
e9174d1e
SL
37 if code == c::EXCEPTION_STACK_OVERFLOW {
38 report_overflow();
1a4d82fc 39 }
e9174d1e 40 c::EXCEPTION_CONTINUE_SEARCH
1a4d82fc
JJ
41 }
42}
43
44pub unsafe fn init() {
e9174d1e 45 if c::AddVectoredExceptionHandler(0, vectored_handler).is_null() {
1a4d82fc
JJ
46 panic!("failed to install exception handler");
47 }
e9174d1e
SL
48 // Set the thread stack guarantee for the main thread.
49 let _h = Handler::new();
1a4d82fc
JJ
50}
51
e9174d1e 52pub unsafe fn cleanup() {}