|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- /*
- Copyright (C) 2000 Paul Davis
- Copyright (C) 2003 Rohan Drape
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 2.1 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
- ISO/POSIX C version of Paul Davis's lock free ringbuffer C++ code.
- This is safe for the case of one read thread and one write thread.
- */
-
- #define USE_MLOCK
-
- #include <stdlib.h>
- #include <string.h>
- #ifdef USE_MLOCK
- #include <sys/mman.h>
- #endif /* USE_MLOCK */
- #include "CarlaDefines.h"
-
- typedef struct {
- char *buf;
- size_t len;
- }
- jack_ringbuffer_data_t ;
-
- typedef struct {
- char *buf;
- volatile size_t write_ptr;
- volatile size_t read_ptr;
- size_t size;
- size_t size_mask;
- int mlocked;
- }
- jack_ringbuffer_t ;
-
- CARLA_PLUGIN_EXPORT jack_ringbuffer_t *jack_ringbuffer_create(size_t sz);
- CARLA_PLUGIN_EXPORT void jack_ringbuffer_free(jack_ringbuffer_t *rb);
- CARLA_PLUGIN_EXPORT void jack_ringbuffer_get_read_vector(const jack_ringbuffer_t *rb, jack_ringbuffer_data_t *vec);
- CARLA_PLUGIN_EXPORT void jack_ringbuffer_get_write_vector(const jack_ringbuffer_t *rb, jack_ringbuffer_data_t *vec);
- CARLA_PLUGIN_EXPORT size_t jack_ringbuffer_read(jack_ringbuffer_t *rb, char *dest, size_t cnt);
- CARLA_PLUGIN_EXPORT size_t jack_ringbuffer_peek(jack_ringbuffer_t *rb, char *dest, size_t cnt);
- CARLA_PLUGIN_EXPORT void jack_ringbuffer_read_advance(jack_ringbuffer_t *rb, size_t cnt);
- CARLA_PLUGIN_EXPORT size_t jack_ringbuffer_read_space(const jack_ringbuffer_t *rb);
- CARLA_PLUGIN_EXPORT int jack_ringbuffer_mlock(jack_ringbuffer_t *rb);
- CARLA_PLUGIN_EXPORT void jack_ringbuffer_reset(jack_ringbuffer_t *rb);
- CARLA_PLUGIN_EXPORT void jack_ringbuffer_reset_size (jack_ringbuffer_t * rb, size_t sz);
- CARLA_PLUGIN_EXPORT size_t jack_ringbuffer_write(jack_ringbuffer_t *rb, const char *src, size_t cnt);
- CARLA_PLUGIN_EXPORT void jack_ringbuffer_write_advance(jack_ringbuffer_t *rb, size_t cnt);
- CARLA_PLUGIN_EXPORT size_t jack_ringbuffer_write_space(const jack_ringbuffer_t *rb);
-
- /* Create a new ringbuffer to hold at least `sz' bytes of data. The
- actual buffer size is rounded up to the next power of two. */
-
- CARLA_PLUGIN_EXPORT jack_ringbuffer_t *
- jack_ringbuffer_create (size_t sz)
- {
- unsigned int power_of_two;
- jack_ringbuffer_t *rb;
-
- if ((rb = (jack_ringbuffer_t *) malloc (sizeof (jack_ringbuffer_t))) == NULL) {
- return NULL;
- }
-
- for (power_of_two = 1U; 1U << power_of_two < sz; power_of_two++);
-
- rb->size = 1U << power_of_two;
- rb->size_mask = rb->size;
- rb->size_mask -= 1;
- rb->write_ptr = 0;
- rb->read_ptr = 0;
- if ((rb->buf = (char *) malloc (rb->size)) == NULL) {
- free (rb);
- return NULL;
- }
- rb->mlocked = 0;
-
- return rb;
- }
-
- /* Free all data associated with the ringbuffer `rb'. */
-
- CARLA_PLUGIN_EXPORT void
- jack_ringbuffer_free (jack_ringbuffer_t * rb)
- {
- #ifdef USE_MLOCK
- if (rb->mlocked) {
- munlock (rb->buf, rb->size);
- }
- #endif /* USE_MLOCK */
- free (rb->buf);
- free (rb);
- }
-
- /* Lock the data block of `rb' using the system call 'mlock'. */
-
- CARLA_PLUGIN_EXPORT int
- jack_ringbuffer_mlock (jack_ringbuffer_t * rb)
- {
- #ifdef USE_MLOCK
- if (mlock (rb->buf, rb->size)) {
- return -1;
- }
- #endif /* USE_MLOCK */
- rb->mlocked = 1;
- return 0;
- }
-
- /* Reset the read and write pointers to zero. This is not thread
- safe. */
-
- CARLA_PLUGIN_EXPORT void
- jack_ringbuffer_reset (jack_ringbuffer_t * rb)
- {
- rb->read_ptr = 0;
- rb->write_ptr = 0;
- memset(rb->buf, 0, rb->size);
- }
-
- /* Reset the read and write pointers to zero. This is not thread
- safe. */
-
- CARLA_PLUGIN_EXPORT void
- jack_ringbuffer_reset_size (jack_ringbuffer_t * rb, size_t sz)
- {
- rb->size = sz;
- rb->size_mask = rb->size;
- rb->size_mask -= 1;
- rb->read_ptr = 0;
- rb->write_ptr = 0;
- }
-
- /* Return the number of bytes available for reading. This is the
- number of bytes in front of the read pointer and behind the write
- pointer. */
-
- CARLA_PLUGIN_EXPORT size_t
- jack_ringbuffer_read_space (const jack_ringbuffer_t * rb)
- {
- size_t w, r;
-
- w = rb->write_ptr;
- r = rb->read_ptr;
-
- if (w > r) {
- return w - r;
- } else {
- return (w - r + rb->size) & rb->size_mask;
- }
- }
-
- /* Return the number of bytes available for writing. This is the
- number of bytes in front of the write pointer and behind the read
- pointer. */
-
- CARLA_PLUGIN_EXPORT size_t
- jack_ringbuffer_write_space (const jack_ringbuffer_t * rb)
- {
- size_t w, r;
-
- w = rb->write_ptr;
- r = rb->read_ptr;
-
- if (w > r) {
- return ((r - w + rb->size) & rb->size_mask) - 1;
- } else if (w < r) {
- return (r - w) - 1;
- } else {
- return rb->size - 1;
- }
- }
-
- /* The copying data reader. Copy at most `cnt' bytes from `rb' to
- `dest'. Returns the actual number of bytes copied. */
-
- CARLA_PLUGIN_EXPORT size_t
- jack_ringbuffer_read (jack_ringbuffer_t * rb, char *dest, size_t cnt)
- {
- size_t free_cnt;
- size_t cnt2;
- size_t to_read;
- size_t n1, n2;
-
- if ((free_cnt = jack_ringbuffer_read_space (rb)) == 0) {
- return 0;
- }
-
- to_read = cnt > free_cnt ? free_cnt : cnt;
-
- cnt2 = rb->read_ptr + to_read;
-
- if (cnt2 > rb->size) {
- n1 = rb->size - rb->read_ptr;
- n2 = cnt2 & rb->size_mask;
- } else {
- n1 = to_read;
- n2 = 0;
- }
-
- memcpy (dest, &(rb->buf[rb->read_ptr]), n1);
- rb->read_ptr = (rb->read_ptr + n1) & rb->size_mask;
-
- if (n2) {
- memcpy (dest + n1, &(rb->buf[rb->read_ptr]), n2);
- rb->read_ptr = (rb->read_ptr + n2) & rb->size_mask;
- }
-
- 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. */
-
- CARLA_PLUGIN_EXPORT 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 = (tmp_read_ptr + n1) & rb->size_mask;
-
- if (n2) {
- memcpy (dest + n1, &(rb->buf[tmp_read_ptr]), n2);
- }
-
- return to_read;
- }
-
- /* The copying data writer. Copy at most `cnt' bytes to `rb' from
- `src'. Returns the actual number of bytes copied. */
-
- CARLA_PLUGIN_EXPORT size_t
- jack_ringbuffer_write (jack_ringbuffer_t * rb, const char *src, size_t cnt)
- {
- size_t free_cnt;
- size_t cnt2;
- size_t to_write;
- size_t n1, n2;
-
- if ((free_cnt = jack_ringbuffer_write_space (rb)) == 0) {
- return 0;
- }
-
- to_write = cnt > free_cnt ? free_cnt : cnt;
-
- cnt2 = rb->write_ptr + to_write;
-
- if (cnt2 > rb->size) {
- n1 = rb->size - rb->write_ptr;
- n2 = cnt2 & rb->size_mask;
- } else {
- n1 = to_write;
- n2 = 0;
- }
-
- memcpy (&(rb->buf[rb->write_ptr]), src, n1);
- rb->write_ptr = (rb->write_ptr + n1) & rb->size_mask;
-
- if (n2) {
- memcpy (&(rb->buf[rb->write_ptr]), src + n1, n2);
- rb->write_ptr = (rb->write_ptr + n2) & rb->size_mask;
- }
-
- return to_write;
- }
-
- /* Advance the read pointer `cnt' places. */
-
- CARLA_PLUGIN_EXPORT void
- jack_ringbuffer_read_advance (jack_ringbuffer_t * rb, size_t cnt)
- {
- size_t tmp = (rb->read_ptr + cnt) & rb->size_mask;
- rb->read_ptr = tmp;
- }
-
- /* Advance the write pointer `cnt' places. */
-
- CARLA_PLUGIN_EXPORT void
- jack_ringbuffer_write_advance (jack_ringbuffer_t * rb, size_t cnt)
- {
- size_t tmp = (rb->write_ptr + cnt) & rb->size_mask;
- rb->write_ptr = tmp;
- }
-
- /* The non-copying data reader. `vec' is an array of two places. Set
- the values at `vec' to hold the current readable data at `rb'. If
- the readable data is in one segment the second segment has zero
- length. */
-
- CARLA_PLUGIN_EXPORT void
- jack_ringbuffer_get_read_vector (const jack_ringbuffer_t * rb,
- jack_ringbuffer_data_t * vec)
- {
- size_t free_cnt;
- size_t cnt2;
- size_t w, r;
-
- w = rb->write_ptr;
- r = rb->read_ptr;
-
- if (w > r) {
- free_cnt = w - r;
- } else {
- free_cnt = (w - r + rb->size) & rb->size_mask;
- }
-
- cnt2 = r + free_cnt;
-
- if (cnt2 > rb->size) {
-
- /* Two part vector: the rest of the buffer after the current write
- ptr, plus some from the start of the buffer. */
-
- vec[0].buf = &(rb->buf[r]);
- vec[0].len = rb->size - r;
- vec[1].buf = rb->buf;
- vec[1].len = cnt2 & rb->size_mask;
-
- } else {
-
- /* Single part vector: just the rest of the buffer */
-
- vec[0].buf = &(rb->buf[r]);
- vec[0].len = free_cnt;
- vec[1].len = 0;
- }
- }
-
- /* The non-copying data writer. `vec' is an array of two places. Set
- the values at `vec' to hold the current writeable data at `rb'. If
- the writeable data is in one segment the second segment has zero
- length. */
-
- CARLA_PLUGIN_EXPORT void
- jack_ringbuffer_get_write_vector (const jack_ringbuffer_t * rb,
- jack_ringbuffer_data_t * vec)
- {
- size_t free_cnt;
- size_t cnt2;
- size_t w, r;
-
- w = rb->write_ptr;
- r = rb->read_ptr;
-
- if (w > r) {
- free_cnt = ((r - w + rb->size) & rb->size_mask) - 1;
- } else if (w < r) {
- free_cnt = (r - w) - 1;
- } else {
- free_cnt = rb->size - 1;
- }
-
- cnt2 = w + free_cnt;
-
- if (cnt2 > rb->size) {
-
- /* Two part vector: the rest of the buffer after the current write
- ptr, plus some from the start of the buffer. */
-
- vec[0].buf = &(rb->buf[w]);
- vec[0].len = rb->size - w;
- vec[1].buf = rb->buf;
- vec[1].len = cnt2 & rb->size_mask;
- } else {
- vec[0].buf = &(rb->buf[w]);
- vec[0].len = free_cnt;
- vec[1].len = 0;
- }
- }
-
|