Skip to content

Commit

Permalink
Fixed incorrect rollover after copy
Browse files Browse the repository at this point in the history
If the copy operation ended at the last spot in a write buffer, it would be equal to `size` instead of rolled back to zero. 

Now while copying bytes, watches for rollover after the copy operation.
  • Loading branch information
jnz86 authored Jan 16, 2023
1 parent 7c044b5 commit 0fe17b4
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lwrb/src/lwrb/lwrb_ex.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,13 @@ size_t lwrb_copy(lwrb_t* dest, lwrb_t* src)
for (i = 0; i < len_to_copy; i++)
{

dest->buff[dest_w++] = src->buff[src_r++];

/* Handle roll-over / wrap for both source and destination indexes */
if (dest_w >= dest->size)
dest_w = 0;

if (src_r >= src->size)
src_r = 0;

dest->buff[dest_w++] = src->buff[src_r++];
}

atomic_store_explicit(&dest->w, dest_w, memory_order_release);
Expand Down

0 comments on commit 0fe17b4

Please sign in to comment.