diff options
author | Damien George <damien.p.george@gmail.com> | 2015-01-14 00:11:09 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-01-14 00:11:09 +0000 |
commit | 2127e9a844ae46cb2149e7c38eb9f4345075f6e4 (patch) | |
tree | 6f2acc16b4df40c34782b262541de420a7c4115c /unix/alloc.c | |
parent | c935d69f743af14bc1ddedf1d4123f2c6d110f05 (diff) | |
download | micropython-2127e9a844ae46cb2149e7c38eb9f4345075f6e4.tar.gz micropython-2127e9a844ae46cb2149e7c38eb9f4345075f6e4.zip |
py, unix: Trace root pointers with native emitter under unix port.
Native code has GC-heap pointers in it so it must be scanned. But on
unix port memory for native functions is mmap'd, and so it must have
explicit code to scan it for root pointers.
Diffstat (limited to 'unix/alloc.c')
-rw-r--r-- | unix/alloc.c | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/unix/alloc.c b/unix/alloc.c index cb866c78a2..44a84437ec 100644 --- a/unix/alloc.c +++ b/unix/alloc.c @@ -24,16 +24,29 @@ * THE SOFTWARE. */ +#include <stdio.h> #include <stdint.h> #include <stdlib.h> +#include <string.h> #include <sys/mman.h> -#include "mpconfigport.h" +#include "py/mpstate.h" +#include "py/gc.h" #if defined(__OpenBSD__) || defined(__MACH__) #define MAP_ANONYMOUS MAP_ANON #endif +// The memory allocated here is not on the GC heap (and it may contain pointers +// that need to be GC'd) so we must somehow trace this memory. We do it by +// keeping a linked list of all mmap'd regions, and tracing them explicitly. + +typedef struct _mmap_region_t { + void *ptr; + size_t len; + struct _mmap_region_t *next; +} mmap_region_t; + void mp_unix_alloc_exec(mp_uint_t min_size, void **ptr, mp_uint_t *size) { // size needs to be a multiple of the page size *size = (min_size + 0xfff) & (~0xfff); @@ -41,8 +54,31 @@ void mp_unix_alloc_exec(mp_uint_t min_size, void **ptr, mp_uint_t *size) { if (*ptr == MAP_FAILED) { *ptr = NULL; } + + // add new link to the list of mmap'd regions + mmap_region_t *rg = m_new_obj(mmap_region_t); + rg->ptr = *ptr; + rg->len = min_size; + rg->next = MP_STATE_VM(mmap_region_head); + MP_STATE_VM(mmap_region_head) = rg; } void mp_unix_free_exec(void *ptr, mp_uint_t size) { munmap(ptr, size); + + // unlink the mmap'd region from the list + for (mmap_region_t **rg = (mmap_region_t**)&MP_STATE_VM(mmap_region_head); *rg != NULL; *rg = (*rg)->next) { + if ((*rg)->ptr == ptr) { + mmap_region_t *next = (*rg)->next; + m_del_obj(mmap_region_t, *rg); + *rg = next; + return; + } + } +} + +void mp_unix_mark_exec(void) { + for (mmap_region_t *rg = MP_STATE_VM(mmap_region_head); rg != NULL; rg = rg->next) { + gc_collect_root(rg->ptr, rg->len / sizeof(mp_uint_t)); + } } |