Skip to content

Commit 6ab7e4a

Browse files
marcinszkudlinskilgirdwood
authored andcommitted
buf: buffer free "virtual destructor"
struct sof_audio_buffer is a "base class" in a meaning of C++ of all buffers in SOF (currently comp_buffer and ring_buffer). Freeing of memory was based on indirect assumption that struct sof_audio_buffer is a very first field of the buffer structure this commit moves freeing of buffer to a method specific for each buffer type, enforcing that proper pointer will be used regardless of buffer structure Signed-off-by: Marcin Szkudlinski <marcin.szkudlinski@intel.com>
1 parent 93d2756 commit 6ab7e4a

File tree

4 files changed

+8
-5
lines changed

4 files changed

+8
-5
lines changed

src/audio/buffers/audio_buffer.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <stdint.h>
88
#include <stddef.h>
99
#include <errno.h>
10+
#include <rtos/panic.h>
1011
#include <rtos/alloc.h>
1112
#include <ipc/stream.h>
1213
#include <sof/audio/audio_buffer.h>
@@ -92,9 +93,8 @@ void audio_buffer_free(struct sof_audio_buffer *buffer)
9293
audio_buffer_free(buffer->secondary_buffer_sink);
9394
audio_buffer_free(buffer->secondary_buffer_source);
9495
#endif /* CONFIG_PIPELINE_2_0 */
95-
if (buffer->ops->free)
96-
buffer->ops->free(buffer);
97-
rfree(buffer);
96+
/* "virtual destructor": free the buffer internals and buffer memory */
97+
buffer->ops->free(buffer);
9898
}
9999

100100
static
@@ -196,6 +196,7 @@ void audio_buffer_init(struct sof_audio_buffer *buffer, uint32_t buffer_type, bo
196196
CORE_CHECK_STRUCT_INIT(&buffer, is_shared);
197197
buffer->buffer_type = buffer_type;
198198
buffer->ops = audio_buffer_ops;
199+
assert(audio_buffer_ops->free);
199200
buffer->audio_stream_params = audio_stream_params;
200201
buffer->is_shared = is_shared;
201202

src/audio/buffers/comp_buffer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ static void comp_buffer_free(struct sof_audio_buffer *audio_buffer)
158158
notifier_unregister_all(NULL, buffer);
159159

160160
rfree(buffer->stream.addr);
161+
rfree(buffer);
161162
}
162163

163164
static struct source_ops comp_buffer_source_ops = {

src/audio/buffers/ring_buffer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static void ring_buffer_free(struct sof_audio_buffer *audio_buffer)
9696
container_of(audio_buffer, struct ring_buffer, audio_buffer);
9797

9898
rfree((__sparse_force void *)ring_buffer->_data_buffer);
99+
rfree(ring_buffer);
99100
}
100101

101102
static void ring_buffer_reset(struct sof_audio_buffer *audio_buffer)

src/include/sof/audio/audio_buffer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ struct sof_audio_buffer;
2121
struct audio_buffer_ops {
2222
/**
2323
* @brief this method must free all structures allocated by buffer implementation
24-
* it must not free the buffer memory itself
25-
* OPTIONAL
24+
* and the buffer itself
25+
* OBLIGATORY
2626
*/
2727
void (*free)(struct sof_audio_buffer *buffer);
2828

0 commit comments

Comments
 (0)