FreeStack.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Copyright (c) 2011-2022 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #define FREE_STACK_CPP
  26. #include "FreeStack.h"
  27. #if defined(HAS_UNUSED_STACK) && HAS_UNUSED_STACK
  28. //------------------------------------------------------------------------------
  29. inline char* stackBegin() {
  30. #if defined(__AVR__)
  31. return __brkval ? __brkval : &__bss_end;
  32. #elif defined(__IMXRT1062__)
  33. return reinterpret_cast<char*>(&_ebss);
  34. #elif defined(__arm__)
  35. return reinterpret_cast<char*>(sbrk(0));
  36. #else // defined(__AVR__)
  37. #error "undefined stackBegin"
  38. #endif // defined(__AVR__)
  39. }
  40. //------------------------------------------------------------------------------
  41. inline char* stackPointer() {
  42. #if defined(__AVR__)
  43. return reinterpret_cast<char*>(SP);
  44. #elif defined(__arm__)
  45. register uint32_t sp asm("sp");
  46. return reinterpret_cast<char*>(sp);
  47. #else // defined(__AVR__)
  48. #error "undefined stackPointer"
  49. #endif // defined(__AVR__)
  50. }
  51. //------------------------------------------------------------------------------
  52. /** Stack fill pattern. */
  53. const char FILL = 0x55;
  54. void FillStack() {
  55. char* p = stackBegin();
  56. char* top = stackPointer();
  57. while (p < top) {
  58. *p++ = FILL;
  59. }
  60. }
  61. //------------------------------------------------------------------------------
  62. // May fail if malloc or new is used.
  63. int UnusedStack() {
  64. char* h = stackBegin();
  65. char* top = stackPointer();
  66. int n;
  67. for (n = 0; (h + n) < top; n++) {
  68. if (h[n] != FILL) {
  69. if (n >= 16) {
  70. break;
  71. }
  72. // Attempt to skip used heap.
  73. h += n;
  74. n = 0;
  75. }
  76. }
  77. return n;
  78. }
  79. #endif // defined(HAS_UNUSED_STACK) && HAS_UNUSED_STACK