Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef StackFrameDepth_h | |
| 6 #define StackFrameDepth_h | |
| 7 | |
| 8 #include "wtf/Assertions.h" | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 namespace blink { | |
| 12 | |
| 13 // Enable estimation of stackframe pointer on supported platforms. | |
| 14 #define ENABLE_STACKFRAME_POINTER_ESTIMATION 1 | |
| 15 | |
| 16 // StackFrameDepth keeps track of current call stack frame depth. | |
| 17 // Use isSafeToRecurse() to query if there is a room in current | |
| 18 // call stack for more recursive call. | |
| 19 class StackFrameDepth final { | |
| 20 public: | |
| 21 static bool isEstimationSupported() | |
| 22 { | |
| 23 #if ENABLE(STACKFRAME_POINTER_ESTIMATION) && (COMPILER(GCC) || COMPILER(MSVC)) | |
| 24 return true; | |
| 25 #else | |
| 26 return false; | |
| 27 #endif | |
| 28 } | |
| 29 | |
| 30 inline bool isSafeToRecurse() | |
| 31 { | |
| 32 if (isEstimationSupported()) { | |
| 33 ASSERT(m_safeStackFrame); | |
| 34 | |
| 35 // Below comparison assumes callstack to glow downward, | |
| 36 // which is true for all ABI Blink currently supports. | |
| 37 return estimateCurrentStackFrame() < m_safeStackFrame; | |
| 38 } | |
| 39 | |
| 40 return m_depth < kMaxDepth; | |
| 41 } | |
| 42 | |
| 43 inline void increment() | |
| 44 { | |
| 45 if (isEstimationSupported()) | |
| 46 return; | |
| 47 | |
| 48 ++m_depth; | |
| 49 } | |
| 50 | |
| 51 inline void decrement() | |
| 52 { | |
| 53 if (isEstimationSupported()) | |
| 54 return; | |
| 55 | |
| 56 ASSERT(m_depth > 0); | |
| 57 --m_depth; | |
| 58 } | |
| 59 | |
| 60 void configureLimit(); | |
| 61 | |
| 62 private: | |
| 63 // The maximum depth of eager, unrolled trace() calls that is | |
| 64 // considered safe and allowed. | |
| 65 const int kMaxDepth = 100; | |
| 66 | |
| 67 static uintptr_t estimateCurrentStackFrame(const char* dummy = nullptr) | |
| 68 { | |
| 69 ASSERT(isEstimationSupported()); | |
| 70 #if COMPILER(GCC) | |
| 71 return reinterpret_cast<uintptr_t>(__builtin_frame_address(0)); | |
| 72 #elif COMPILER(MSVC) | |
| 73 return reinterpret_cast<uintptr_t>(&dummy); | |
|
haraken
2015/01/07 05:04:44
Why can't we always use &dummy? If we can use &dum
kouhei (in TOK)
2015/01/07 05:06:39
Ideally, we want to have this compile to just "mov
haraken
2015/01/07 05:09:51
Makes sense.
Then why do you need m_depth? In a p
| |
| 74 #else | |
| 75 // Stack frame pointer estimation not supported on this platform. | |
| 76 ASSERT_NOT_REACHED(); | |
| 77 return 0; | |
| 78 #endif | |
| 79 } | |
| 80 | |
| 81 union { | |
| 82 int m_depth; | |
| 83 uintptr_t m_safeStackFrame; | |
| 84 }; | |
| 85 }; | |
| 86 | |
| 87 } // namespace blink | |
| 88 | |
| 89 #endif | |
| OLD | NEW |