Skip to content

Instantly share code, notes, and snippets.

@notro
Created September 28, 2018 10:55
Show Gist options
  • Save notro/2942c6cdcb818a4b6d28dde20f89ec34 to your computer and use it in GitHub Desktop.
Save notro/2942c6cdcb818a4b6d28dde20f89ec34 to your computer and use it in GitHub Desktop.
diff --git a/py/gc.c b/py/gc.c
old mode 100755
new mode 100644
index 1f5cd8d01..ba3f8d625
--- a/py/gc.c
+++ b/py/gc.c
@@ -747,6 +747,8 @@ void *gc_make_long_lived(void *old_ptr) {
// we ensure we don't delete memory that has a second reference. (Though if there is we may
// confuse things when its mutable.)
memcpy(new_ptr, old_ptr, n_bytes);
+ // Make the old object a Zombie in case someone holds a reference to it
+ mp_obj_turn_zombie(old_ptr, n_bytes);
return new_ptr;
}
diff --git a/py/obj.h b/py/obj.h
index 8b6772873..229dfe442 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -868,4 +868,7 @@ mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t
memmove(((char*)dest) + (beg + slice_len) * (item_sz), ((char*)dest) + (end) * (item_sz), ((dest_len) + (len_adj) - ((beg) + (slice_len))) * (item_sz)); \
memmove(((char*)dest) + (beg) * (item_sz), slice, slice_len * (item_sz));
+// zombie
+void mp_obj_turn_zombie(void *obj_ptr, size_t n_bytes);
+
#endif // MICROPY_INCLUDED_PY_OBJ_H
diff --git a/py/objsingleton.c b/py/objsingleton.c
index 67535391e..bcb7faebd 100644
--- a/py/objsingleton.c
+++ b/py/objsingleton.c
@@ -25,6 +25,7 @@
*/
#include <stdlib.h>
+#include <string.h>
#include <assert.h>
#include "py/obj.h"
@@ -53,3 +54,11 @@ const mp_obj_singleton_t mp_const_ellipsis_obj = {{&mp_type_singleton}, MP_QSTR_
#if MICROPY_PY_BUILTINS_NOTIMPLEMENTED
const mp_obj_singleton_t mp_const_notimplemented_obj = {{&mp_type_singleton}, MP_QSTR_NotImplemented};
#endif
+
+static const mp_obj_singleton_t mp_const_zombie_obj = {{&mp_type_singleton}, MP_QSTR_Zombie};
+
+void mp_obj_turn_zombie(void *obj_ptr, size_t n_bytes) {
+ if (n_bytes >= sizeof(mp_const_zombie_obj)) {
+ memcpy(obj_ptr, &mp_const_zombie_obj, sizeof(mp_const_zombie_obj));
+ }
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment