forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_space.cpp
49 lines (40 loc) · 1.44 KB
/
stack_space.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "explorer/interpreter/stack_space.h"
#include "common/check.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/CrashRecoveryContext.h"
namespace Carbon::Internal {
static constexpr int64_t SufficientStack = 256 << 10;
static constexpr int64_t DesiredStackSpace = 8 << 20;
static LLVM_THREAD_LOCAL intptr_t bottom_of_stack = 0;
// Returns the current bottom of stack.
LLVM_NO_SANITIZE("address")
LLVM_ATTRIBUTE_NOINLINE static auto GetStackPointer() -> intptr_t {
#if __GNUC__ || __has_builtin(__builtin_frame_address)
return reinterpret_cast<intptr_t>(__builtin_frame_address(0));
#else
char char_on_stack = 0;
char* volatile ptr = &char_on_stack;
return reinterpret_cast<intptr_t>(ptr);
#endif
}
auto IsStackSpaceNearlyExhausted() -> bool {
if (bottom_of_stack == 0) {
// Not initialized on the thread; always start a new thread.
return true;
}
return std::abs(GetStackPointer() - bottom_of_stack) >
(DesiredStackSpace - SufficientStack);
}
auto RunWithExtraStackHelper(llvm::function_ref<void()> fn) -> void {
llvm::CrashRecoveryContext context;
context.RunSafelyOnThread(
[&] {
bottom_of_stack = GetStackPointer();
fn();
},
DesiredStackSpace);
}
} // namespace Carbon::Internal