Skip to content

Commit e1d5ee4

Browse files
committed
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
1 parent 186fa35 commit e1d5ee4

File tree

5 files changed

+32
-5
lines changed

5 files changed

+32
-5
lines changed

shared-module/_protomatter/Protomatter.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ void common_hal_protomatter_protomatter_reconstruct(protomatter_protomatter_obj_
7878
// verify that the matrix is big enough
7979
mp_get_index(mp_obj_get_type(self->framebuffer), self->bufinfo.len, MP_OBJ_NEW_SMALL_INT(self->bufsize-1), false);
8080
} else {
81+
_PM_FREE(self->bufinfo.buf);
82+
_PM_FREE(self->core.rgbPins);
83+
_PM_FREE(self->core.addr);
84+
_PM_FREE(self->core.screenData);
85+
8186
self->framebuffer = NULL;
8287
self->bufinfo.buf = _PM_allocator_impl(self->bufsize);
8388
self->bufinfo.len = self->bufsize;
@@ -156,6 +161,7 @@ void common_hal_protomatter_protomatter_deinit(protomatter_protomatter_obj_t* se
156161
if (self->core.rgbPins) {
157162
_PM_free(&self->core);
158163
}
164+
memset(&self->core, 0, sizeof(self->core));
159165

160166
self->base.type = NULL;
161167
}

shared-module/_protomatter/allocator.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "supervisor/memory.h"
77

88
#define _PM_ALLOCATOR _PM_allocator_impl
9-
#define _PM_FREE _PM_free_impl
9+
#define _PM_FREE(x) (_PM_free_impl((x)), (x)=NULL, (void)0)
1010

1111
static inline void *_PM_allocator_impl(size_t sz) {
1212
supervisor_allocation *allocation = allocate_memory(align32_size(sz), true);
@@ -22,8 +22,6 @@ static inline void _PM_free_impl(void *ptr_in) {
2222

2323
if (allocation) {
2424
free_memory(allocation);
25-
} else {
26-
m_free(ptr_in);
2725
}
2826
}
2927

shared-module/displayio/__init__.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@
2121

2222
primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT];
2323

24+
STATIC bool any_display_uses_this_protomatter(protomatter_protomatter_obj_t* pm) {
25+
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
26+
if (displays[i].framebuffer_display.base.type == &framebufferio_framebufferdisplay_type) {
27+
framebufferio_framebufferdisplay_obj_t* display = &displays[i].framebuffer_display;
28+
if (display->framebuffer == pm) {
29+
return true;
30+
}
31+
}
32+
}
33+
return false;
34+
}
35+
2436
// Check for recursive calls to displayio_background.
2537
bool displayio_background_in_progress = false;
2638

@@ -152,7 +164,9 @@ void reset_displays(void) {
152164
#if CIRCUITPY_PROTOMATTER
153165
} else if (displays[i].protomatter.base.type == &protomatter_Protomatter_type) {
154166
protomatter_protomatter_obj_t * pm = &displays[i].protomatter;
155-
common_hal_protomatter_protomatter_reconstruct(pm, NULL);
167+
if(!any_display_uses_this_protomatter(pm)) {
168+
common_hal_protomatter_protomatter_deinit(pm);
169+
}
156170
#endif
157171
} else {
158172
// Not an active display bus.

supervisor/shared/display.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <string.h>
3030

3131
#include "py/mpstate.h"
32+
#include "shared-module/displayio/__init__.h"
3233
#include "shared-bindings/displayio/Group.h"
3334
#include "shared-bindings/displayio/Palette.h"
3435
#include "shared-bindings/displayio/TileGrid.h"
@@ -112,6 +113,14 @@ void supervisor_display_move_memory(void) {
112113
grid->inline_tiles = false;
113114
}
114115
MP_STATE_VM(terminal_tilegrid_tiles) = NULL;
116+
#if CIRCUITPY_PROTOMATTER
117+
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
118+
if (displays[i].protomatter.base.type == &protomatter_Protomatter_type) {
119+
protomatter_protomatter_obj_t * pm = &displays[i].protomatter;
120+
common_hal_protomatter_protomatter_reconstruct(pm, NULL);
121+
}
122+
}
123+
#endif
115124
#endif
116125
}
117126

supervisor/shared/memory.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
#include "supervisor/shared/display.h"
3333

34-
#define CIRCUITPY_SUPERVISOR_ALLOC_COUNT 8
34+
#define CIRCUITPY_SUPERVISOR_ALLOC_COUNT (12)
3535

3636
static supervisor_allocation allocations[CIRCUITPY_SUPERVISOR_ALLOC_COUNT];
3737
// We use uint32_t* to ensure word (4 byte) alignment.

0 commit comments

Comments
 (0)