buffer: add overwrite in circular wrap case

In case when we overwrite "old" data in buffer
(when buffer is full) we should align r_ptr
with w_ptr.

Signed-off-by: Bartosz Kokoszko <bartoszx.kokoszko@linux.intel.com>
This commit is contained in:
Bartosz Kokoszko 2019-09-18 14:56:45 +02:00 committed by Tomasz Lauda
parent c22d3c188d
commit d42c01956f
2 changed files with 12 additions and 5 deletions

View File

@ -163,6 +163,10 @@ void comp_update_buffer_produce(struct comp_buffer *buffer, uint32_t bytes)
buffer->w_ptr = buffer->addr +
(buffer->w_ptr - buffer->end_addr);
/* "overwrite" old data in circular wrap case */
if (bytes > buffer->free)
buffer->r_ptr = buffer->w_ptr;
/* calculate available bytes */
if (buffer->r_ptr < buffer->w_ptr)
buffer->avail = buffer->w_ptr - buffer->r_ptr;

View File

@ -45,12 +45,15 @@ static void test_audio_buffer_write_fill_10_bytes_and_write_5(void **state)
memcpy(buf->w_ptr, &more_bytes, 5);
comp_update_buffer_produce(buf, 5);
uint8_t ref[10] = {10, 11, 12, 13, 14, 5, 6, 7, 8, 9};
uint8_t ref_1[5] = {5, 6, 7, 8, 9};
uint8_t ref_2[5] = {10, 11, 12, 13, 14};
assert_int_equal(buf->avail, 5);
assert_int_equal(buf->free, 5);
assert_ptr_equal(buf->w_ptr, buf->r_ptr + 5);
assert_int_equal(memcmp(buf->r_ptr, &ref, 10), 0);
assert_int_equal(buf->avail, 10);
assert_int_equal(buf->free, 0);
assert_ptr_equal(buf->w_ptr, buf->r_ptr);
assert_int_equal(memcmp(buf->r_ptr, &ref_1, 5), 0);
comp_update_buffer_consume(buf, 5);
assert_int_equal(memcmp(buf->r_ptr, &ref_2, 5), 0);
buffer_free(buf);
}