mirror of https://github.com/mpv-player/mpv
ring: implement drain in terms of read
I think this makes it easier to reason about it and avoids duplicate logic.
This commit is contained in:
parent
3238cd3dac
commit
690ea07b7a
21
misc/ring.c
21
misc/ring.c
|
@ -55,18 +55,8 @@ struct mp_ring *mp_ring_new(void *talloc_ctx, int size)
|
||||||
return ringbuffer;
|
return ringbuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_ring_drain(struct mp_ring *buffer, int len)
|
|
||||||
{
|
|
||||||
int buffered = mp_ring_buffered(buffer);
|
|
||||||
int drain_len = FFMIN(len, buffered);
|
|
||||||
atomic_fetch_add(&buffer->rpos, drain_len);
|
|
||||||
return drain_len;
|
|
||||||
}
|
|
||||||
|
|
||||||
int mp_ring_read(struct mp_ring *buffer, unsigned char *dest, int len)
|
int mp_ring_read(struct mp_ring *buffer, unsigned char *dest, int len)
|
||||||
{
|
{
|
||||||
if (!dest) return mp_ring_drain(buffer, len);
|
|
||||||
|
|
||||||
int size = mp_ring_size(buffer);
|
int size = mp_ring_size(buffer);
|
||||||
int buffered = mp_ring_buffered(buffer);
|
int buffered = mp_ring_buffered(buffer);
|
||||||
int read_len = FFMIN(len, buffered);
|
int read_len = FFMIN(len, buffered);
|
||||||
|
@ -75,14 +65,21 @@ int mp_ring_read(struct mp_ring *buffer, unsigned char *dest, int len)
|
||||||
int len1 = FFMIN(size - read_ptr, read_len);
|
int len1 = FFMIN(size - read_ptr, read_len);
|
||||||
int len2 = read_len - len1;
|
int len2 = read_len - len1;
|
||||||
|
|
||||||
memcpy(dest, buffer->buffer + read_ptr, len1);
|
if (dest) {
|
||||||
memcpy(dest + len1, buffer->buffer, len2);
|
memcpy(dest, buffer->buffer + read_ptr, len1);
|
||||||
|
memcpy(dest + len1, buffer->buffer, len2);
|
||||||
|
}
|
||||||
|
|
||||||
atomic_fetch_add(&buffer->rpos, read_len);
|
atomic_fetch_add(&buffer->rpos, read_len);
|
||||||
|
|
||||||
return read_len;
|
return read_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int mp_ring_drain(struct mp_ring *buffer, int len)
|
||||||
|
{
|
||||||
|
return mp_ring_read(buffer, NULL, len);
|
||||||
|
}
|
||||||
|
|
||||||
int mp_ring_write(struct mp_ring *buffer, unsigned char *src, int len)
|
int mp_ring_write(struct mp_ring *buffer, unsigned char *src, int len)
|
||||||
{
|
{
|
||||||
int size = mp_ring_size(buffer);
|
int size = mp_ring_size(buffer);
|
||||||
|
|
Loading…
Reference in New Issue