From c8f969feb5f8dcdbe7716403e0a48d63ebb19352 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 27 Jan 2020 08:49:41 -0600 Subject: samd: audio-dma: avoid memory allocations With the previous change, stereo mp3 playback changed from needing 4 2304-byte allocations to needing 2 4604-byte allocations. This was enough to cause MemoryErrors with regularity. By using m_realloc() here, the existing memory region can be used. m_realloc() also works on the first invocation, because m_realloc(NULL, sz) just calls m_malloc of sz. --- ports/atmel-samd/audio_dma.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/atmel-samd/audio_dma.c b/ports/atmel-samd/audio_dma.c index 0478cc9bf..352af8980 100644 --- a/ports/atmel-samd/audio_dma.c +++ b/ports/atmel-samd/audio_dma.c @@ -203,13 +203,13 @@ audio_dma_result audio_dma_setup_playback(audio_dma_t* dma, if (output_signed != samples_signed) { output_spacing = 1; max_buffer_length /= dma->spacing; - dma->first_buffer = (uint8_t*) m_malloc(max_buffer_length, false); + dma->first_buffer = (uint8_t*) m_realloc(dma->first_buffer, max_buffer_length); if (dma->first_buffer == NULL) { return AUDIO_DMA_MEMORY_ERROR; } dma->first_buffer_free = true; if (!single_buffer) { - dma->second_buffer = (uint8_t*) m_malloc(max_buffer_length, false); + dma->second_buffer = (uint8_t*) m_realloc(dma->second_buffer, max_buffer_length); if (dma->second_buffer == NULL) { return AUDIO_DMA_MEMORY_ERROR; } -- cgit v1.2.3