Skip to content

Commit

Permalink
Merge pull request #20 from jnz86/fix_rollover_bug
Browse files Browse the repository at this point in the history
Fixed incorrect rollover after copy
  • Loading branch information
MaJerle authored Jan 19, 2023
2 parents b056597 + d7507c4 commit e1dae3b
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lwrb/src/lwrb/lwrb_ex.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,16 @@ lwrb_copy(lwrb_t* dest, lwrb_t* src) {
/* For the lesser amount in source or free in destination, copy byte by byte */
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) {
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);
atomic_store_explicit(&src->r, src_r, memory_order_release);

Expand Down

0 comments on commit e1dae3b

Please sign in to comment.