LCOV - code coverage report
Current view: top level - py - modgc.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-343-g7b050b366.info Lines: 27 27 100.0 %
Date: 2024-04-25 17:00:48 Functions: 7 7 100.0 %
Branches: 8 8 100.0 %

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

Generated by: LCOV version 1.15-5-g462f71d