modgc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 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. #include "py/mpstate.h"
  27. #include "py/obj.h"
  28. #include "py/gc.h"
  29. #if MICROPY_PY_GC && MICROPY_ENABLE_GC
  30. // collect(): run a garbage collection
  31. static mp_obj_t py_gc_collect(void) {
  32. gc_collect();
  33. #if MICROPY_PY_GC_COLLECT_RETVAL
  34. return MP_OBJ_NEW_SMALL_INT(MP_STATE_MEM(gc_collected));
  35. #else
  36. return mp_const_none;
  37. #endif
  38. }
  39. MP_DEFINE_CONST_FUN_OBJ_0(gc_collect_obj, py_gc_collect);
  40. // disable(): disable the garbage collector
  41. static mp_obj_t gc_disable(void) {
  42. MP_STATE_MEM(gc_auto_collect_enabled) = 0;
  43. return mp_const_none;
  44. }
  45. MP_DEFINE_CONST_FUN_OBJ_0(gc_disable_obj, gc_disable);
  46. // enable(): enable the garbage collector
  47. static mp_obj_t gc_enable(void) {
  48. MP_STATE_MEM(gc_auto_collect_enabled) = 1;
  49. return mp_const_none;
  50. }
  51. MP_DEFINE_CONST_FUN_OBJ_0(gc_enable_obj, gc_enable);
  52. static mp_obj_t gc_isenabled(void) {
  53. return mp_obj_new_bool(MP_STATE_MEM(gc_auto_collect_enabled));
  54. }
  55. MP_DEFINE_CONST_FUN_OBJ_0(gc_isenabled_obj, gc_isenabled);
  56. // mem_free(): return the number of bytes of available heap RAM
  57. static mp_obj_t gc_mem_free(void) {
  58. gc_info_t info;
  59. gc_info(&info);
  60. #if MICROPY_GC_SPLIT_HEAP_AUTO
  61. // Include max_new_split value here as a more useful heuristic
  62. return MP_OBJ_NEW_SMALL_INT(info.free + info.max_new_split);
  63. #else
  64. return MP_OBJ_NEW_SMALL_INT(info.free);
  65. #endif
  66. }
  67. MP_DEFINE_CONST_FUN_OBJ_0(gc_mem_free_obj, gc_mem_free);
  68. // mem_alloc(): return the number of bytes of heap RAM that are allocated
  69. static mp_obj_t gc_mem_alloc(void) {
  70. gc_info_t info;
  71. gc_info(&info);
  72. return MP_OBJ_NEW_SMALL_INT(info.used);
  73. }
  74. MP_DEFINE_CONST_FUN_OBJ_0(gc_mem_alloc_obj, gc_mem_alloc);
  75. #if MICROPY_GC_ALLOC_THRESHOLD
  76. static mp_obj_t gc_threshold(size_t n_args, const mp_obj_t *args) {
  77. if (n_args == 0) {
  78. if (MP_STATE_MEM(gc_alloc_threshold) == (size_t)-1) {
  79. return MP_OBJ_NEW_SMALL_INT(-1);
  80. }
  81. return mp_obj_new_int(MP_STATE_MEM(gc_alloc_threshold) * MICROPY_BYTES_PER_GC_BLOCK);
  82. }
  83. mp_int_t val = mp_obj_get_int(args[0]);
  84. if (val < 0) {
  85. MP_STATE_MEM(gc_alloc_threshold) = (size_t)-1;
  86. } else {
  87. MP_STATE_MEM(gc_alloc_threshold) = val / MICROPY_BYTES_PER_GC_BLOCK;
  88. }
  89. return mp_const_none;
  90. }
  91. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gc_threshold_obj, 0, 1, gc_threshold);
  92. #endif
  93. static const mp_rom_map_elem_t mp_module_gc_globals_table[] = {
  94. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_gc) },
  95. { MP_ROM_QSTR(MP_QSTR_collect), MP_ROM_PTR(&gc_collect_obj) },
  96. { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&gc_disable_obj) },
  97. { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&gc_enable_obj) },
  98. { MP_ROM_QSTR(MP_QSTR_isenabled), MP_ROM_PTR(&gc_isenabled_obj) },
  99. { MP_ROM_QSTR(MP_QSTR_mem_free), MP_ROM_PTR(&gc_mem_free_obj) },
  100. { MP_ROM_QSTR(MP_QSTR_mem_alloc), MP_ROM_PTR(&gc_mem_alloc_obj) },
  101. #if MICROPY_GC_ALLOC_THRESHOLD
  102. { MP_ROM_QSTR(MP_QSTR_threshold), MP_ROM_PTR(&gc_threshold_obj) },
  103. #endif
  104. };
  105. static MP_DEFINE_CONST_DICT(mp_module_gc_globals, mp_module_gc_globals_table);
  106. const mp_obj_module_t mp_module_gc = {
  107. .base = { &mp_type_module },
  108. .globals = (mp_obj_dict_t *)&mp_module_gc_globals,
  109. };
  110. MP_REGISTER_MODULE(MP_QSTR_gc, mp_module_gc);
  111. #endif