summaryrefslogtreecommitdiff
path: root/py/gc.c
diff options
context:
space:
mode:
authorScott Shawcroft <scott@tannewt.org>2019-03-08 15:29:14 -0800
committerScott Shawcroft <scott@tannewt.org>2019-03-12 11:18:26 -0700
commit03be42ab847f04a89d207d98fdb28048cfedf72d (patch)
tree918df321648fc6715eee60ddf926d5dc7ee58bfe /py/gc.c
parentef4a74e201e852a32e2e1be73ed9ba85c88fb3f4 (diff)
Enter safe mode when an allocation is attempted on an uninitialized heap.
Diffstat (limited to 'py/gc.c')
-rwxr-xr-xpy/gc.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/py/gc.c b/py/gc.c
index 1f5cd8d01..e0b77c4cf 100755
--- a/py/gc.c
+++ b/py/gc.c
@@ -31,6 +31,8 @@
#include "py/gc.h"
#include "py/runtime.h"
+#include "supervisor/shared/safe_mode.h"
+
#if MICROPY_ENABLE_GC
#if MICROPY_DEBUG_VERBOSE // print debugging info
@@ -182,6 +184,13 @@ void gc_init(void *start, void *end) {
DEBUG_printf(" pool at %p, length " UINT_FMT " bytes, " UINT_FMT " blocks\n", MP_STATE_MEM(gc_pool_start), gc_pool_block_len * BYTES_PER_BLOCK, gc_pool_block_len);
}
+void gc_deinit() {
+ // Run any finalizers before we stop using the heap.
+ gc_sweep_all();
+
+ MP_STATE_MEM(gc_pool_start) = 0;
+}
+
void gc_lock(void) {
GC_ENTER();
MP_STATE_MEM(gc_lock_depth)++;
@@ -463,6 +472,10 @@ void *gc_alloc(size_t n_bytes, bool has_finaliser, bool long_lived) {
return NULL;
}
+ if (MP_STATE_MEM(gc_pool_start) == 0) {
+ reset_into_safe_mode(GC_ALLOC_OUTSIDE_VM);
+ }
+
GC_ENTER();
// check if GC is locked