1 //===--- Stack.h - Utilities for dealing with stack space -------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// Defines utilities for dealing with stack allocation and stack space.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_BASIC_STACK_H
15 #define LLVM_CLANG_BASIC_STACK_H
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/Compiler.h"
23 /// The amount of stack space that Clang would like to be provided with.
24 /// If less than this much is available, we may be unable to reach our
25 /// template instantiation depth limit and other similar limits.
26 constexpr size_t DesiredStackSize = 8 << 20;
28 /// Call this once on each thread, as soon after starting the thread as
29 /// feasible, to note the approximate address of the bottom of the stack.
30 void noteBottomOfStack();
32 /// Determine whether the stack is nearly exhausted.
33 bool isStackNearlyExhausted();
35 void runWithSufficientStackSpaceSlow(llvm::function_ref<void()> Diag,
36 llvm::function_ref<void()> Fn);
38 /// Run a given function on a stack with "sufficient" space. If stack space
39 /// is insufficient, calls Diag to emit a diagnostic before calling Fn.
40 inline void runWithSufficientStackSpace(llvm::function_ref<void()> Diag,
41 llvm::function_ref<void()> Fn) {
42 #ifdef LLVM_ENABLE_THREADS
43 if (LLVM_UNLIKELY(isStackNearlyExhausted()))
44 runWithSufficientStackSpaceSlow(Diag, Fn);
48 if (LLVM_UNLIKELY(isStackNearlyExhausted()))
53 } // end namespace clang
55 #endif // LLVM_CLANG_BASIC_STACK_H