summaryrefslogtreecommitdiff
path: root/shared-module
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2020-04-01 11:59:15 -0500
committerJeff Epler <jepler@gmail.com>2020-04-14 18:24:58 -0500
commit3a94412cd35c39bf30f29cb2b79be3870709c809 (patch)
tree8a2653aa8a7ca665f0f70b74172cc964d33504b2 /shared-module
parent1d8a073c05c36133e02d839ed187b9d7abf24d79 (diff)
protomatter: more memory allocation fixes
- bump supervisor alloc count by 4 (we actually use 5) - move reconstruct to after gc heap is reset - destroy protomatter object entirely if not used by a FramebufferDisplay - ensure previous supervisor allocations are released - zero out pointers so GC can collect them
Diffstat (limited to 'shared-module')
-rw-r--r--shared-module/_protomatter/Protomatter.c6
-rw-r--r--shared-module/_protomatter/allocator.h4
-rw-r--r--shared-module/displayio/__init__.c16
3 files changed, 22 insertions, 4 deletions
diff --git a/shared-module/_protomatter/Protomatter.c b/shared-module/_protomatter/Protomatter.c
index cbb60db6d..bec9586f8 100644
--- a/shared-module/_protomatter/Protomatter.c
+++ b/shared-module/_protomatter/Protomatter.c
@@ -78,6 +78,11 @@ void common_hal_protomatter_protomatter_reconstruct(protomatter_protomatter_obj_
// verify that the matrix is big enough
mp_get_index(mp_obj_get_type(self->framebuffer), self->bufinfo.len, MP_OBJ_NEW_SMALL_INT(self->bufsize-1), false);
} else {
+ _PM_FREE(self->bufinfo.buf);
+ _PM_FREE(self->core.rgbPins);
+ _PM_FREE(self->core.addr);
+ _PM_FREE(self->core.screenData);
+
self->framebuffer = NULL;
self->bufinfo.buf = _PM_allocator_impl(self->bufsize);
self->bufinfo.len = self->bufsize;
@@ -156,6 +161,7 @@ void common_hal_protomatter_protomatter_deinit(protomatter_protomatter_obj_t* se
if (self->core.rgbPins) {
_PM_free(&self->core);
}
+ memset(&self->core, 0, sizeof(self->core));
self->base.type = NULL;
}
diff --git a/shared-module/_protomatter/allocator.h b/shared-module/_protomatter/allocator.h
index 8d9d7ff96..9b7590df3 100644
--- a/shared-module/_protomatter/allocator.h
+++ b/shared-module/_protomatter/allocator.h
@@ -6,7 +6,7 @@
#include "supervisor/memory.h"
#define _PM_ALLOCATOR _PM_allocator_impl
-#define _PM_FREE _PM_free_impl
+#define _PM_FREE(x) (_PM_free_impl((x)), (x)=NULL, (void)0)
static inline void *_PM_allocator_impl(size_t sz) {
supervisor_allocation *allocation = allocate_memory(align32_size(sz), true);
@@ -22,8 +22,6 @@ static inline void _PM_free_impl(void *ptr_in) {
if (allocation) {
free_memory(allocation);
- } else {
- m_free(ptr_in);
}
}
diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c
index 225aa2008..dfa3e7473 100644
--- a/shared-module/displayio/__init__.c
+++ b/shared-module/displayio/__init__.c
@@ -21,6 +21,18 @@
primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT];
+STATIC bool any_display_uses_this_protomatter(protomatter_protomatter_obj_t* pm) {
+ for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
+ if (displays[i].framebuffer_display.base.type == &framebufferio_framebufferdisplay_type) {
+ framebufferio_framebufferdisplay_obj_t* display = &displays[i].framebuffer_display;
+ if (display->framebuffer == pm) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
// Check for recursive calls to displayio_background.
bool displayio_background_in_progress = false;
@@ -152,7 +164,9 @@ void reset_displays(void) {
#if CIRCUITPY_PROTOMATTER
} else if (displays[i].protomatter.base.type == &protomatter_Protomatter_type) {
protomatter_protomatter_obj_t * pm = &displays[i].protomatter;
- common_hal_protomatter_protomatter_reconstruct(pm, NULL);
+ if(!any_display_uses_this_protomatter(pm)) {
+ common_hal_protomatter_protomatter_deinit(pm);
+ }
#endif
} else {
// Not an active display bus.