Allow (read-only) access to a fiber's stack details
authorBrian Watling <bwatling@fb.com>
Thu, 3 Sep 2015 20:13:26 +0000 (13:13 -0700)
committerfacebook-github-bot-9 <folly-bot@fb.com>
Thu, 3 Sep 2015 20:20:19 +0000 (13:20 -0700)
Summary: Allow users to access the stack pointer and stack size

Reviewed By: @alikhtarov

Differential Revision: D2407252

folly/experimental/fibers/Fiber.h

index 7862488277c82eea1c71f5057a74224f90c3e485..0732dd71b0a1727f431da359063076db9b7e934c 100644 (file)
@@ -54,6 +54,21 @@ class Fiber {
   Fiber& operator=(const Fiber&) = delete;
 
   ~Fiber();
+
+  /**
+   * Retrieve this fiber's base stack and stack size.
+   *
+   * @return This fiber's stack pointer and stack size.
+   */
+  std::pair<void*, size_t> getStack() const {
+    void* const stack =
+      std::min<void*>(fcontext_.stackLimit(), fcontext_.stackBase());
+    const size_t size = std::abs<intptr_t>(
+        reinterpret_cast<intptr_t>(fcontext_.stackBase()) -
+        reinterpret_cast<intptr_t>(fcontext_.stackLimit()));
+    return { stack, size };
+  }
+
  private:
   enum State {
     INVALID,                    /**< Does't have task function */