summaryrefslogtreecommitdiffstatshomepage
path: root/unix/alloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'unix/alloc.c')
-rw-r--r--unix/alloc.c38
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));
+ }
}