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
This commit is contained in:
Jeff Epler 2020-04-01 11:59:15 -05:00
parent 1d8a073c05
commit 3a94412cd3
5 changed files with 32 additions and 5 deletions

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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.

View File

@ -29,6 +29,7 @@
#include <string.h>
#include "py/mpstate.h"
#include "shared-module/displayio/__init__.h"
#include "shared-bindings/displayio/Group.h"
#include "shared-bindings/displayio/Palette.h"
#include "shared-bindings/displayio/TileGrid.h"
@ -112,6 +113,14 @@ void supervisor_display_move_memory(void) {
grid->inline_tiles = false;
}
MP_STATE_VM(terminal_tilegrid_tiles) = NULL;
#if CIRCUITPY_PROTOMATTER
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
if (displays[i].protomatter.base.type == &protomatter_Protomatter_type) {
protomatter_protomatter_obj_t * pm = &displays[i].protomatter;
common_hal_protomatter_protomatter_reconstruct(pm, NULL);
}
}
#endif
#endif
}

View File

@ -31,7 +31,7 @@
#include "supervisor/shared/display.h"
#define CIRCUITPY_SUPERVISOR_ALLOC_COUNT 8
#define CIRCUITPY_SUPERVISOR_ALLOC_COUNT (12)
static supervisor_allocation allocations[CIRCUITPY_SUPERVISOR_ALLOC_COUNT];
// We use uint32_t* to ensure word (4 byte) alignment.