blob: c698c7d21d2099c22448b17b851d0998271735bc [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python 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 */
Damien George51dfcb42015-01-01 20:27:54 +000026#ifndef __MICROPY_INCLUDED_PY_GC_H__
27#define __MICROPY_INCLUDED_PY_GC_H__
28
29#include <stdint.h>
30
31#include "py/mpconfig.h"
32#include "py/misc.h"
Damien George04b91472014-05-03 23:27:38 +010033
Damiendcced922013-10-21 23:45:08 +010034void gc_init(void *start, void *end);
Damien George443e0182014-04-08 11:31:21 +000035
36// These lock/unlock functions can be nested.
37// They can be used to prevent the GC from allocating/freeing.
38void gc_lock(void);
39void gc_unlock(void);
Dave Hylands2fe841d2014-06-30 22:49:21 -070040bool gc_is_locked(void);
Damien George443e0182014-04-08 11:31:21 +000041
Damien George109c1de2014-10-31 21:30:46 +000042// This variable controls auto garbage collection. If set to 0 then the
43// GC won't automatically run when gc_alloc can't find enough blocks. But
44// you can still allocate/free memory and also explicitly call gc_collect.
45extern uint16_t gc_auto_collect_enabled;
46
Damien George443e0182014-04-08 11:31:21 +000047// A given port must implement gc_collect by using the other collect functions.
48void gc_collect(void);
Damien8b3a7c22013-10-23 20:20:17 +010049void gc_collect_start(void);
Damien George40f3c022014-07-03 13:25:24 +010050void gc_collect_root(void **ptrs, mp_uint_t len);
Damien8b3a7c22013-10-23 20:20:17 +010051void gc_collect_end(void);
Damien George443e0182014-04-08 11:31:21 +000052
Damien George40f3c022014-07-03 13:25:24 +010053void *gc_alloc(mp_uint_t n_bytes, bool has_finaliser);
Damienfd8b6bc2013-10-22 20:26:36 +010054void gc_free(void *ptr);
Damien George0b13f3e2014-10-24 23:12:25 +010055mp_uint_t gc_nbytes(const void *ptr);
Damien George40f3c022014-07-03 13:25:24 +010056void *gc_realloc(void *ptr, mp_uint_t n_bytes);
Damieneefcc792013-10-22 15:25:25 +010057
58typedef struct _gc_info_t {
Damien George40f3c022014-07-03 13:25:24 +010059 mp_uint_t total;
60 mp_uint_t used;
61 mp_uint_t free;
62 mp_uint_t num_1block;
63 mp_uint_t num_2block;
64 mp_uint_t max_block;
Damieneefcc792013-10-22 15:25:25 +010065} gc_info_t;
66
67void gc_info(gc_info_t *info);
Paul Sokolovsky550d8042014-02-11 23:53:34 +020068void gc_dump_info(void);
Damien Georgece1162a2014-02-26 22:55:59 +000069void gc_dump_alloc_table(void);
Damien George51dfcb42015-01-01 20:27:54 +000070
71#endif // __MICROPY_INCLUDED_PY_GC_H__