From 1666aa2519bc2e2ba1b3acc7612d96a464908069 Mon Sep 17 00:00:00 2001 From: joq Date: Thu, 8 Jul 2004 17:21:03 +0000 Subject: [PATCH] [0.98.4] add jack_ringbuffer_peek() git-svn-id: svn+ssh://jackaudio.org/trunk/jack@729 0c269be4-1314-0410-8aa9-9f06e86f4224 --- configure.in | 2 +- jack/ringbuffer.h | 17 +++++++++++++++++ libjack/ringbuffer.c | 45 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/configure.in b/configure.in index e9d0b7a..92c46d4 100644 --- a/configure.in +++ b/configure.in @@ -15,7 +15,7 @@ dnl changes are made dnl --- JACK_MAJOR_VERSION=0 JACK_MINOR_VERSION=98 -JACK_MICRO_VERSION=3 +JACK_MICRO_VERSION=4 dnl --- dnl HOWTO: updating the jack protocol version diff --git a/jack/ringbuffer.h b/jack/ringbuffer.h index 8aeae13..810e8fe 100644 --- a/jack/ringbuffer.h +++ b/jack/ringbuffer.h @@ -137,6 +137,23 @@ void jack_ringbuffer_get_write_vector(const jack_ringbuffer_t *rb, */ size_t jack_ringbuffer_read(jack_ringbuffer_t *rb, char *dest, size_t cnt); +/** + * Read data from the ringbuffer. Opposed to jack_ringbuffer_read() + * this function does not move the read pointer. Thus it's + * a convenient way to inspect data in the ringbuffer in a + * continous fashion. The price is that the data is copied + * into a user provided buffer. For "raw" non-copy inspection + * of the data in the ringbuffer use jack_ringbuffer_get_read_vector(). + * + * @param rb a pointer to the ringbuffer structure. + * @param dest a pointer to a buffer where data read from the + * ringbuffer will go. + * @param cnt the number of bytes to read. + * + * @return the number of bytes read, which may range from 0 to cnt. + */ +size_t jack_ringbuffer_peek(jack_ringbuffer_t *rb, char *dest, size_t cnt); + /** * Advance the read pointer. * diff --git a/libjack/ringbuffer.c b/libjack/ringbuffer.c index 74ca11a..9d5f91c 100644 --- a/libjack/ringbuffer.c +++ b/libjack/ringbuffer.c @@ -170,6 +170,51 @@ jack_ringbuffer_read (jack_ringbuffer_t * rb, char *dest, size_t cnt) return to_read; } +/* The copying data reader w/o read pointer advance. Copy at most + `cnt' bytes from `rb' to `dest'. Returns the actual number of bytes +copied. */ + +size_t +jack_ringbuffer_peek (jack_ringbuffer_t * rb, char *dest, size_t cnt) +{ + size_t free_cnt; + size_t cnt2; + size_t to_read; + size_t n1, n2; + size_t tmp_read_ptr; + + tmp_read_ptr = rb->read_ptr; + + if ((free_cnt = jack_ringbuffer_read_space (rb)) == 0) { + return 0; + } + + to_read = cnt > free_cnt ? free_cnt : cnt; + + cnt2 = tmp_read_ptr + to_read; + + if (cnt2 > rb->size) { + n1 = rb->size - tmp_read_ptr; + n2 = cnt2 & rb->size_mask; + } else { + n1 = to_read; + n2 = 0; + } + + memcpy (dest, &(rb->buf[tmp_read_ptr]), n1); + tmp_read_ptr += n1; + tmp_read_ptr &= rb->size_mask; + + if (n2) { + memcpy (dest + n1, &(rb->buf[tmp_read_ptr]), n2); + tmp_read_ptr += n2; + tmp_read_ptr &= rb->size_mask; + } + + return to_read; +} + + /* The copying data writer. Copy at most `cnt' bytes to `rb' from `src'. Returns the actual number of bytes copied. */