10000 Merge pull request #10094 from SamantazFox/fix-malloc-uses-allocated-… · adafruit/circuitpython@000600a · GitHub
[go: up one dir, main page]

Skip to content

Commit 000600a

Browse files
authored
Merge pull request #10094 from SamantazFox/fix-malloc-uses-allocated-size
Fix m_realloc/m_free when MICROPY_MALLOC_USES_ALLOCATED_SIZE is set
2 parents d857656 + fb0aea8 commit 000600a

File tree

10 files changed

+142
-21
lines changed

10 files changed

+142
-21
lines changed

ports/atmel-samd/audio_dma.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,27 @@ audio_dma_result audio_dma_setup_playback(audio_dma_t *dma,
200200
}
201201

202202

203-
dma->buffer[0] = (uint8_t *)m_realloc(dma->buffer[0], max_buffer_length);
203+
dma->buffer[0] = (uint8_t *)m_realloc(dma->buffer[0],
204+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
205+
dma->buffer_length[0], // Old size
206+
#endif
207+
max_buffer_length);
208+
204209
dma->buffer_length[0] = max_buffer_length;
210+
205211
if (dma->buffer[0] == NULL) {
206212
return AUDIO_DMA_MEMORY_ERROR;
207213
}
208214

209215
if (!dma->single_buffer) {
210-
dma->buffer[1] = (uint8_t *)m_realloc(dma->buffer[1], max_buffer_length);
216+
dma->buffer[1] = (uint8_t *)m_realloc(dma->buffer[1],
217+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
218+
dma->buffer_length[1], // Old size
219+
#endif
220+
max_buffer_length);
221+
211222
dma->buffer_length[1] = max_buffer_length;
223+
212224
if (dma->buffer[1] == NULL) {
213225
return AUDIO_DMA_MEMORY_ERROR;
214226
}

ports/nordic/common-hal/audiopwmio/PWMAudioOut.c

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,26 @@ bool common_hal_audiopwmio_pwmaudioout_deinited(audiopwmio_pwmaudioo 6D47 ut_obj_t *se
188188
return !self->pwm;
189189
}
190190

191+
static void free_buffers(audiopwmio_pwmaudioout_obj_t *self) {
192+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
193+
m_free(self->buffers[0], self->buffer_size[0]);
194+
self->buffer_size[0] = 0;
195+
#else
196+
m_free(self->buffers[0]);
197+
#endif
198+
199+
self->buffers[0] = NULL;
200+
201+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
202+
m_free(self->buffers[1], self->buffer_size[1]);
203+
self->buffer_size[1] = 0;
204+
#else
205+
m_free(self->buffers[1]);
206+
#endif
207+
208+
self->buffers[1] = NULL;
209+
}
210+
191211
void common_hal_audiopwmio_pwmaudioout_deinit(audiopwmio_pwmaudioout_obj_t *self) {
192212
if (common_hal_audiopwmio_pwmaudioout_deinited(self)) {
193213
return;
@@ -209,11 +229,7 @@ void common_hal_audiopwmio_pwmaudioout_deinit(audiopwmio_pwmaudioout_obj_t *self
209229

210230
self->pwm = NULL;
211231

212-
m_free(self->buffers[0]);
213-
self->buffers[0] = NULL;
214-
215-
m_free(self->buffers[1]);
216-
self->buffers[1] = NULL;
232+
free_buffers(self);
217233
}
218234

219235
void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t *self, mp_obj_t sample, bool loop) {
@@ -235,10 +251,18 @@ void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t *self,
235251

236252
mp_arg_validate_length_max(max_buffer_length, UINT16_MAX, MP_QSTR_buffer);
237253

238-
uint16_t buffer_length = (uint16_t)max_buffer_length;
239-
self->buffers[0] = m_malloc(buffer_length * 2 * sizeof(uint16_t));
254+
size_t buffer_size = (size_t)max_buffer_length * 2 * sizeof(uint16_t);
255+
256+
self->buffers[0] = m_malloc(buffer_size);
257+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
258+
self->buffer_size[0] = buffer_size;
259+
#endif
260+
240261
if (!self->single_buffer) {
241-
self->buffers[1] = m_malloc(buffer_length * 2 * sizeof(uint16_t));
262+
self->buffers[1] = m_malloc(buffer_size);
263+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
264+
self->buffer_size[1] = buffer_size;
265+
#endif
242266
}
243267

244268

@@ -274,11 +298,7 @@ void common_hal_audiopwmio_pwmaudioout_stop(audiopwmio_pwmaudioout_obj_t *self)
274298
self->stopping = false;
275299
self->paused = false;
276300

277-
m_free(self->buffers[0]);
278-
self->buffers[0] = NULL;
279-
280-
m_free(self->buffers[1]);
281-
self->buffers[1] = NULL;
301+
free_buffers(self);
282302
}
283303

284304
bool common_hal_audiopwmio_pwmaudioout_get_playing(audiopwmio_pwmaudioout_obj_t *self) {

ports/nordic/common-hal/audiopwmio/PWMAudioOut.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ typedef struct {
1212
mp_obj_base_t base;
1313
mp_obj_t *sample;
1414
NRF_PWM_Type *pwm;
15+
1516
uint16_t *buffers[2];
17+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
18+
size_t buffer_size[2]; // Keeps track of allocated size
19+
#endif
1620

1721
uint16_t quiescent_value;
1822
uint16_t scale;

ports/raspberrypi/audio_dma.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,27 @@ audio_dma_result audio_dma_setup_playback(
227227
max_buffer_length /= dma->sample_spacing;
228228
}
229229

230-
dma->buffer[0] = (uint8_t *)m_realloc(dma->buffer[0], max_buffer_length);
230+
dma->buffer[0] = (uint8_t *)m_realloc(dma->buffer[0],
231+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
232+
dma->buffer_length[0], // Old size
233+
#endif
234+
max_buffer_length);
235+
231236
dma->buffer_length[0] = max_buffer_length;
237+
232238
if (dma->buffer[0] == NULL) {
233239
return AUDIO_DMA_MEMORY_ERROR;
234240
}
235241

236242
if (!single_buffer) {
237-
dma->buffer[1] = (uint8_t *)m_realloc(dma->buffer[1], max_buffer_length);
243+
dma->buffer[1] = (uint8_t *)m_realloc(dma->buffer[1],
244+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
245+
dma->buffer_length[1], // Old size
246+
#endif
247+
max_buffer_length);
248+
238249
dma->buffer_length[1] = max_buffer_length;
250+
239251
if (dma->buffer[1] == NULL) {
240252
return AUDIO_DMA_MEMORY_ERROR;
241253
}
@@ -419,16 +431,31 @@ void audio_dma_init(audio_dma_t *dma) {
419431
dma->buffer[0] = NULL;
420432
dma->buffer[1] = NULL;
421433

434+
dma->buffer_length[0] = 0;
435+
dma->buffer_length[1] = 0;
436+
422437
dma->channel[0] = NUM_DMA_CHANNELS;
423438
dma->channel[1] = NUM_DMA_CHANNELS;
424439
}
425440

426441
void audio_dma_deinit(audio_dma_t *dma) {
442+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
443+
m_free(dma->buffer[0], dma->buffer_length[0]);
444+
#else
427445
m_free(dma->buffer[0]);
446+
#endif
447+
428448
dma->buffer[0] = NULL;
449+
dma->buffer_length[0] = 0;
429450

451+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
452+
m_free(dma->buffer[1], dma->buffer_length[1]);
453+
#else
430454
m_free(dma->buffer[1]);
455+
#endif
456+
431457
dma->buffer[1] = NULL;
458+
dma->buffer_length[1] = 0;
432459
}
433460

434461
bool audio_dma_get_playing(audio_dma_t *dma) {

ports/stm/common-hal/audiopwmio/PWMAudioOut.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ void common_hal_audiopwmio_pwmaudioout_construct(audiopwmio_pwmaudioout_obj_t *s
206206
self->buffer[0] = NULL;
207207
self->buffer[1] = NULL;
208208

209+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
210+
self->buffer_size[0] = 0;
211+
self->buffer_size[1] = 0;
212+
#endif
213+
209214
self->quiescent_value = quiescent_value;
210215
}
211216

@@ -214,9 +219,22 @@ bool common_hal_audiopwmio_pwmaudioout_deinited(audiopwmio_pwmaudioout_obj_t *se
214219
}
215220

216221
static void free_buffers(audiopwmio_pwmaudioout_obj_t *self) {
222+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
223+
m_free(self->buffer[0], self->buffer_size[0]);
224+
self->buffer_size[0] = 0;
225+
#else
217226
m_free(self->buffer[0]);
227+
#endif
228+
218229
self->buffer[0] = NULL;
230+
231+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
232+
m_free(self->buffer[1], self->buffer_size[1]);
233+
self->buffer_size[1] = 0;
234+
#else
219235
m_free(self->buffer[1]);
236+
#endif
237+
220238
self->buffer[1] = NULL;
221239
}
222240

@@ -257,11 +275,21 @@ void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t *self,
257275
if (max_buffer_length > UINT16_MAX) {
258276
mp_raise_ValueError_varg(MP_ERROR_TEXT("Buffer length %d too big. It must be less than %d"), max_buffer_length, UINT16_MAX);
259277
}
278+
260279
uint16_t buffer_length = (uint16_t)max_buffer_length / self->bytes_per_sample;
261-
self->buffer[0] = m_malloc(buffer_length * sizeof(uint16_t));
280+
size_t buffer_size = buffer_length * sizeof(uint16_t);
281+
282+
self->buffer[0] = m_malloc(buffer_size);
283+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
284+
self->buffer_size[0] = buffer_size;
285+
#endif
262286
self->buffer_ptr[0] = self->buffer_length[0] = 0;
287+
263288
if (self->pin[1]) {
264-
self->buffer[1] = m_malloc(buffer_length * sizeof(uint16_t));
289+
self->buffer[1] = m_malloc(buffer_size);
290+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
291+
self->buffer_size[1] = buffer_size;
292+
#endif
265293
self->buffer_ptr[1] = self->buffer_length[1] = 0;
266294
}
267295

ports/stm/common-hal/audiopwmio/PWMAudioOut.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ typedef struct {
1414
uint16_t quiescent_value;
1515

1616
uint16_t *buffer[2];
17+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
18+
uint16_t buffer_size[2]; // Keeps track of allocated size
19+
#endif
1720
uint16_t buffer_length[2];
1821
uint16_t buffer_ptr[2];
1922

ports/zephyr-cp/common-hal/wifi/ScannedNetworks.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ void wifi_scannednetworks_deinit(wifi_scannednetworks_obj_t *self) {
116116
// Free any results we don't need.
117117
while (!k_fifo_is_empty(&self->fifo)) {
118118
wifi_network_obj_t *entry = k_fifo_get(&self->fifo, K_NO_WAIT);
119+
120+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
121+
m_free(entry, sizeof(wifi_network_obj_t));
122+
#else
119123
m_free(entry);
124+
#endif
120125
}
121126
wifi_scannednetworks_done(self);
122127
}

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
@@ -129,7 +129,12 @@ void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self,
129129
for (uint16_t i = 0; i < number_of_colors; i++) {
130130
common_hal_displayio_palette_set_color(palette, i, palette_data[i]);
131131
}
132+
133+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
134+
m_free(palette_data, palette_size);
135+
#else
132136
m_free(palette_data);
137+
#endif
133138
} else {
134139
common_hal_displayio_palette_set_color(palette, 0, 0x0);
135140
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