pystack.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef MICROPY_INCLUDED_PY_PYSTACK_H
  27. #define MICROPY_INCLUDED_PY_PYSTACK_H
  28. #include "py/mpstate.h"
  29. // Enable this debugging option to check that the amount of memory freed is
  30. // consistent with amounts that were previously allocated.
  31. #define MP_PYSTACK_DEBUG (0)
  32. #if MICROPY_ENABLE_PYSTACK
  33. void mp_pystack_init(void *start, void *end);
  34. void *mp_pystack_alloc(size_t n_bytes);
  35. // This function can free multiple continuous blocks at once: just pass the
  36. // pointer to the block that was allocated first and it and all subsequently
  37. // allocated blocks will be freed.
  38. static inline void mp_pystack_free(void *ptr) {
  39. assert((uint8_t *)ptr >= MP_STATE_THREAD(pystack_start));
  40. assert((uint8_t *)ptr <= MP_STATE_THREAD(pystack_cur));
  41. #if MP_PYSTACK_DEBUG
  42. size_t n_bytes_to_free = MP_STATE_THREAD(pystack_cur) - (uint8_t *)ptr;
  43. size_t n_bytes = *(size_t *)(MP_STATE_THREAD(pystack_cur) - MICROPY_PYSTACK_ALIGN);
  44. while (n_bytes < n_bytes_to_free) {
  45. n_bytes += *(size_t *)(MP_STATE_THREAD(pystack_cur) - n_bytes - MICROPY_PYSTACK_ALIGN);
  46. }
  47. if (n_bytes != n_bytes_to_free) {
  48. mp_printf(&mp_plat_print, "mp_pystack_free() failed: %u != %u\n", (uint)n_bytes_to_free,
  49. (uint)*(size_t *)(MP_STATE_THREAD(pystack_cur) - MICROPY_PYSTACK_ALIGN));
  50. assert(0);
  51. }
  52. #endif
  53. MP_STATE_THREAD(pystack_cur) = (uint8_t *)ptr;
  54. }
  55. static inline void mp_pystack_realloc(void *ptr, size_t n_bytes) {
  56. mp_pystack_free(ptr);
  57. mp_pystack_alloc(n_bytes);
  58. }
  59. static inline size_t mp_pystack_usage(void) {
  60. return MP_STATE_THREAD(pystack_cur) - MP_STATE_THREAD(pystack_start);
  61. }
  62. static inline size_t mp_pystack_limit(void) {
  63. return MP_STATE_THREAD(pystack_end) - MP_STATE_THREAD(pystack_start);
  64. }
  65. #endif
  66. #if !MICROPY_ENABLE_PYSTACK
  67. #define mp_local_alloc(n_bytes) alloca(n_bytes)
  68. static inline void mp_local_free(void *ptr) {
  69. (void)ptr;
  70. }
  71. static inline void *mp_nonlocal_alloc(size_t n_bytes) {
  72. return m_new(uint8_t, n_bytes);
  73. }
  74. static inline void *mp_nonlocal_realloc(void *ptr, size_t old_n_bytes, size_t new_n_bytes) {
  75. return m_renew(uint8_t, ptr, old_n_bytes, new_n_bytes);
  76. }
  77. static inline void mp_nonlocal_free(void *ptr, size_t n_bytes) {
  78. m_del(uint8_t, ptr, n_bytes);
  79. }
  80. #else
  81. static inline void *mp_local_alloc(size_t n_bytes) {
  82. return mp_pystack_alloc(n_bytes);
  83. }
  84. static inline void mp_local_free(void *ptr) {
  85. mp_pystack_free(ptr);
  86. }
  87. static inline void *mp_nonlocal_alloc(size_t n_bytes) {
  88. return mp_pystack_alloc(n_bytes);
  89. }
  90. static inline void *mp_nonlocal_realloc(void *ptr, size_t old_n_bytes, size_t new_n_bytes) {
  91. (void)old_n_bytes;
  92. mp_pystack_realloc(ptr, new_n_bytes);
  93. return ptr;
  94. }
  95. static inline void mp_nonlocal_free(void *ptr, size_t n_bytes) {
  96. (void)n_bytes;
  97. mp_pystack_free(ptr);
  98. }
  99. #endif
  100. #endif // MICROPY_INCLUDED_PY_PYSTACK_H