Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToxAV's rb_write function is written in a strange way #963

Closed
zoff99 opened this issue Jul 3, 2018 · 5 comments
Closed

ToxAV's rb_write function is written in a strange way #963

zoff99 opened this issue Jul 3, 2018 · 5 comments
Assignees
Labels
P3 Low priority
Milestone

Comments

@zoff99
Copy link

zoff99 commented Jul 3, 2018

if (b->end == b->start) {
b->start = (b->start + 1) % b->size;

there is something i don't understand.
if this is ever reached should not also rc be set to start element. since now the start element is also pushed out?

me loves me some code without documentation ...

@zoff99
Copy link
Author

zoff99 commented Jul 3, 2018

i think this should look like this (to make it clear):

void *rb_write(RingBuffer *b, void *p)
{
    void *rc = nullptr;

    if ((b->end + 1) % b->size == b->start) { /* buffer full ? */
        rc = b->data[b->start]; // return oldest element
        b->start = (b->start + 1) % b->size; // insert an empty element (this distinguish from empty state)
    }

    b->data[b->end] = p;
    b->end = (b->end + 1) % b->size;

    return rc;
}

@sudden6
Copy link

sudden6 commented Jul 3, 2018

if this is ever reached should not also rc be set to start element. since now the start element is also pushed out?

Seems to happen a few lines above

rc = b->data[b->start];

But I agree with you, the variant you provide is more readable(!). Just the comment insert an empty element (this distinguish from empty state) is wrong. There's already something in the buffer at that position, we just let start point to the new "first" element.

I have to say this is an interesting ringbuffer implementation, I would not have expected the inserted element in the buffer and the "first" element returned when the buffer is full. Needs some docs.

@zoff99
Copy link
Author

zoff99 commented Jul 4, 2018

it allocates (size + 1) in new. so there seems to be always an empty element there. or something?
when full still writes the new element inside, but pushes the oldest out. caller needs to free it.

@sudden6
Copy link

sudden6 commented Jul 4, 2018

it allocates (size + 1) in new. so there seems to be always an empty element there. or something?

Seems so, also strange IMO, because normally you don't need an empty element. AFAICT when the buffer is full once, there's also no empty element anymore.

@iphydf iphydf modified the milestones: v0.2.x, v0.2.4 Jul 16, 2018
@iphydf iphydf changed the title rb_write bug? ToxAV's rb_write function is written in a strange way Jul 16, 2018
@iphydf
Copy link
Member

iphydf commented Jul 18, 2018

It's still strange, but we've added a test for the behaviour, so this ticket is done.

@iphydf iphydf closed this as completed Jul 18, 2018
@iphydf iphydf added the P3 Low priority label Feb 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 Low priority
Development

No branches or pull requests

3 participants