Stack.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- Stack.h - Utilities for dealing with stack space -------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// Defines utilities for dealing with stack allocation and stack space.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_BASIC_STACK_H
  19. #define LLVM_CLANG_BASIC_STACK_H
  20. #include <cstddef>
  21. #include "llvm/ADT/STLExtras.h"
  22. #include "llvm/Support/Compiler.h"
  23. namespace clang {
  24. /// The amount of stack space that Clang would like to be provided with.
  25. /// If less than this much is available, we may be unable to reach our
  26. /// template instantiation depth limit and other similar limits.
  27. 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();
  31. /// Determine whether the stack is nearly exhausted.
  32. bool isStackNearlyExhausted();
  33. void runWithSufficientStackSpaceSlow(llvm::function_ref<void()> Diag,
  34. llvm::function_ref<void()> Fn);
  35. /// Run a given function on a stack with "sufficient" space. If stack space
  36. /// is insufficient, calls Diag to emit a diagnostic before calling Fn.
  37. inline void runWithSufficientStackSpace(llvm::function_ref<void()> Diag,
  38. llvm::function_ref<void()> Fn) {
  39. #if LLVM_ENABLE_THREADS
  40. if (LLVM_UNLIKELY(isStackNearlyExhausted()))
  41. runWithSufficientStackSpaceSlow(Diag, Fn);
  42. else
  43. Fn();
  44. #else
  45. if (LLVM_UNLIKELY(isStackNearlyExhausted()))
  46. Diag();
  47. Fn();
  48. #endif
  49. }
  50. } // end namespace clang
  51. #endif // LLVM_CLANG_BASIC_STACK_H
  52. #ifdef __GNUC__
  53. #pragma GCC diagnostic pop
  54. #endif