summaryrefslogtreecommitdiffstatshomepage
path: root/unix
diff options
context:
space:
mode:
Diffstat (limited to 'unix')
-rw-r--r--unix/alloc.c38
-rw-r--r--unix/gccollect.c3
-rw-r--r--unix/mpconfigport.h4
3 files changed, 41 insertions, 4 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));
+ }
}
diff --git a/unix/gccollect.c b/unix/gccollect.c
index 16bfc3b486..52dac01bd2 100644
--- a/unix/gccollect.c
+++ b/unix/gccollect.c
@@ -31,8 +31,6 @@
#if MICROPY_ENABLE_GC
-extern char *stack_top;
-
#if MICROPY_GCREGS_SETJMP
#include <setjmp.h>
@@ -133,6 +131,7 @@ void gc_collect(void) {
// GC stack (and regs because we captured them)
void **regs_ptr = (void**)(void*)&regs;
gc_collect_root(regs_ptr, ((mp_uint_t)MP_STATE_VM(stack_top) - (mp_uint_t)&regs) / sizeof(mp_uint_t));
+ mp_unix_mark_exec();
gc_collect_end();
//printf("-----\n");
diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h
index 3f5d577038..a1acb5f5c4 100644
--- a/unix/mpconfigport.h
+++ b/unix/mpconfigport.h
@@ -148,6 +148,7 @@ typedef const void *machine_const_ptr_t; // must be of pointer size
void mp_unix_alloc_exec(mp_uint_t min_size, void** ptr, mp_uint_t *size);
void mp_unix_free_exec(void *ptr, mp_uint_t size);
+void mp_unix_mark_exec(void);
#define MP_PLAT_ALLOC_EXEC(min_size, ptr, size) mp_unix_alloc_exec(min_size, ptr, size)
#define MP_PLAT_FREE_EXEC(ptr, size) mp_unix_free_exec(ptr, size)
@@ -158,7 +159,8 @@ extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj;
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },
#define MICROPY_PORT_ROOT_POINTERS \
- mp_obj_t keyboard_interrupt_obj;
+ mp_obj_t keyboard_interrupt_obj; \
+ void *mmap_region_head; \
// We need to provide a declaration/definition of alloca()
#ifdef __FreeBSD__