gccollect.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * Development of the code in this file was sponsored by Microbric Pty Ltd
  5. *
  6. * The MIT License (MIT)
  7. *
  8. * Copyright (c) 2014 Damien P. George
  9. * Copyright (c) 2017 Pycom Limited
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. */
  29. #include <stdio.h>
  30. #include "py/mpconfig.h"
  31. #include "py/mpstate.h"
  32. #include "py/gc.h"
  33. #include "py/mpthread.h"
  34. #include "gccollect.h"
  35. #include "soc/cpu.h"
  36. #include "xtensa/hal.h"
  37. static void gc_collect_inner(int level) {
  38. if (level < XCHAL_NUM_AREGS / 8) {
  39. gc_collect_inner(level + 1);
  40. if (level != 0) {
  41. return;
  42. }
  43. }
  44. if (level == XCHAL_NUM_AREGS / 8) {
  45. // get the sp
  46. volatile uint32_t sp = (uint32_t)get_sp();
  47. gc_collect_root((void**)sp, ((mp_uint_t)MP_STATE_THREAD(stack_top) - sp) / sizeof(uint32_t));
  48. return;
  49. }
  50. // trace root pointers from any threads
  51. #if MICROPY_PY_THREAD
  52. mp_thread_gc_others();
  53. #endif
  54. }
  55. void gc_collect(void) {
  56. gc_collect_start();
  57. gc_collect_inner(0);
  58. gc_collect_end();
  59. }