8000 shared-module: Fix m_realloc/m_free when MICROPY_MALLOC_USES_ALLOCATE… · adafruit/circuitpython@fb0aea8 · GitHub
[go: up one dir, main page]

Skip to content

Commit fb0aea8

Browse files
committed
shared-module: Fix m_realloc/m_free when MICROPY_MALLOC_USES_ALLOCATED_SIZE is set
When 'MICROPY_MALLOC_USES_ALLOCATED_SIZE' is set, we need to pass the previously allocated size to 'm_realloc', 'm_realloc_maybe' and 'm_free'. Signed-off-by: Samantaz Fox <coding@samantaz.fr>
1 parent b1a653a commit fb0aea8

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

shared-module/atexit/__init__.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ static size_t callback_len = 0;
1212
static atexit_callback_t *callback = NULL;
1313

1414
void atexit_reset(void) {
15-
callback_len = 0;
15+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
16+
m_free(callback, callback_len * sizeof(atexit_callback_t));
17+
#else
1618
m_free(callback);
19+
#endif
20+
21+
callback_len = 0;
1722
callback = NULL;
1823
}
1924

@@ -39,7 +44,13 @@ void shared_module_atexit_register(mp_obj_t *func, size_t n_args, const mp_obj_t
3944
cb.args[i] = kw_args->table[cb.n_kw].key;
4045
cb.args[i += 1] = kw_args->table[cb.n_kw].value;
4146
}
42-
callback = (atexit_callback_t *)m_realloc(callback, (callback_len + 1) * sizeof(cb));
47+
48+
callback = (atexit_callback_t *)m_realloc(callback,
49+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
50+
callback_len *sizeof(cb), // Old size
51+
#endif
52+
(callback_len + 1) * sizeof(cb));
53+
4354
callback[callback_len++] = cb;
4455
}
4556

shared-module/displayio/OnDiskBitmap.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self,
9595
for (uint16_t i = 0; i < number_of_colors; i++) {
9696
common_hal_displayio_palette_set_color(palette, i, palette_data[i]);
9797
}
98+
99+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
100+
m_free(palette_data, palette_size);
101+
#else
98102
m_free(palette_data);
103+
#endif
99104
} else {
100105
common_hal_displayio_palette_set_color(palette, 0, 0x0);
101106
common_hal_displayio_palette_set_color(palette, 1, 0xffffff);

shared-module/keypad/EventQueue.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ mp_obj_t common_hal_keypad_eventqueue_get(keypad_eventqueue_obj_t *self) {
3939
if (result) {
4040
return event;
4141
}
42+
43+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
44+
m_free(event, sizeof(keypad_event_obj_t));
45+
#else
4246
m_free(event);
47+
#endif
48+
4349
return MP_ROM_NONE;
4450
}
4551

0 commit comments

Comments
 (0)
0