@@ -188,6 +188,26 @@ bool common_hal_audiopwmio_pwmaudioout_deinited(audiopwmio_pwmaudioout_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+
191211void 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
219235void 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
284304bool common_hal_audiopwmio_pwmaudioout_get_playing (audiopwmio_pwmaudioout_obj_t * self ) {
0 commit comments