Skip to content

Commit

Permalink
Avoid zero-length copies
Browse files Browse the repository at this point in the history
Return early if there is nothing to read/write.
  • Loading branch information
rom1v committed May 30, 2024
1 parent 09e8c20 commit fd9498e
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/src/util/audiobuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ sc_audiobuf_read(struct sc_audiobuf *buf, void *to_, uint32_t samples_count) {
uint32_t head = atomic_load_explicit(&buf->head, memory_order_acquire);

uint32_t can_read = (buf->alloc_size + head - tail) % buf->alloc_size;
if (!can_read) {
return 0;
}
if (samples_count > can_read) {
samples_count = can_read;
}
Expand Down Expand Up @@ -86,6 +89,9 @@ sc_audiobuf_write(struct sc_audiobuf *buf, const void *from_,
uint32_t tail = atomic_load_explicit(&buf->tail, memory_order_acquire);

uint32_t can_write = (buf->alloc_size + tail - head - 1) % buf->alloc_size;
if (!can_write) {
return 0;
}
if (samples_count > can_write) {
samples_count = can_write;
}
Expand Down

0 comments on commit fd9498e

Please sign in to comment.