diff options
| author | Damien George | 2014-02-11 21:42:09 +0000 |
|---|---|---|
| committer | Damien George | 2014-02-11 21:42:09 +0000 |
| commit | 323f39a2b383ba1f4b65f5cd786a2531589d1677 (patch) | |
| tree | b1c589947293ee36e00a1fe6813b5feb710126a9 /py | |
| parent | 0d14d1301667132ea12ae37ff8fe8b04842a69f1 (diff) | |
| parent | 723a6ed37175fcade87802c8ab44325971446020 (diff) | |
Merge pull request #278 from pfalcon/unix-gc
Enable GC for Unix port
Diffstat (limited to 'py')
| -rw-r--r-- | py/gc.c | 11 | ||||
| -rw-r--r-- | py/malloc.c | 16 |
2 files changed, 26 insertions, 1 deletions
@@ -240,7 +240,7 @@ void gc_info(gc_info_t *info) { void *gc_alloc(machine_uint_t n_bytes) { machine_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK; - //printf("gc_alloc(%u bytes -> %u blocks)\n", n_bytes, n_blocks); + DEBUG_printf("gc_alloc(%u bytes -> %u blocks)\n", n_bytes, n_blocks); // check for 0 allocation if (n_blocks == 0) { @@ -267,6 +267,7 @@ void *gc_alloc(machine_uint_t n_bytes) { if (collected) { return NULL; } + DEBUG_printf("gc_alloc(" UINT_FMT "): no free mem, triggering GC\n", n_bytes); gc_collect(); collected = 1; } @@ -341,6 +342,14 @@ void *gc_realloc(void *ptr, machine_uint_t n_bytes) { } } +void gc_dump_info() { + gc_info_t info; + gc_info(&info); + printf("GC: total: " UINT_FMT ", used: " UINT_FMT ", free: " UINT_FMT "\n", info.total, info.used, info.free); + printf(" No. of 1-blocks: " UINT_FMT ", 2-blocks: " UINT_FMT ", max blk sz: " UINT_FMT "\n", + info.num_1block, info.num_2block, info.max_block); +} + #if DEBUG_PRINT static void gc_dump_at(void) { for (machine_uint_t bl = 0; bl < gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) { diff --git a/py/malloc.c b/py/malloc.c index c87d91c0a..7f55fa7c8 100644 --- a/py/malloc.c +++ b/py/malloc.c @@ -19,6 +19,22 @@ static int peak_bytes_allocated = 0; #define UPDATE_PEAK() { if (current_bytes_allocated > peak_bytes_allocated) peak_bytes_allocated = current_bytes_allocated; } #endif +#if MICROPY_ENABLE_GC +#include "gc.h" + +// We redirect standard alloc functions to GC heap - just for the rest of +// this module. In the rest of micropython source, system malloc can be +// freely accessed - for interfacing with system and 3rd-party libs for +// example. On the other hand, some (e.g. bare-metal) ports may use GC +// heap as system heap, so, to avoid warnings, we do undef's first. +#undef malloc +#undef free +#undef realloc +#define malloc gc_alloc +#define free gc_free +#define realloc gc_realloc +#endif // MICROPY_ENABLE_GC + void *m_malloc(int num_bytes) { if (num_bytes == 0) { return NULL; |
