diff options
Diffstat (limited to 'libjack/ringbuffer.c')
-rw-r--r-- | libjack/ringbuffer.c | 66 |
1 files changed, 34 insertions, 32 deletions
diff --git a/libjack/ringbuffer.c b/libjack/ringbuffer.c index c06a1b2..b8cf3e6 100644 --- a/libjack/ringbuffer.c +++ b/libjack/ringbuffer.c @@ -1,24 +1,24 @@ /* - 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. -*/ + 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. + */ #include <config.h> @@ -37,13 +37,13 @@ jack_ringbuffer_create (size_t sz) { int power_of_two; jack_ringbuffer_t *rb; - - if ((rb = malloc (sizeof (jack_ringbuffer_t))) == NULL) { + + if ((rb = malloc (sizeof(jack_ringbuffer_t))) == NULL) { return NULL; } - - for (power_of_two = 1; 1 << power_of_two < sz; power_of_two++); - + + for (power_of_two = 1; 1 << power_of_two < sz; power_of_two++) ; + rb->size = 1 << power_of_two; rb->size_mask = rb->size; rb->size_mask -= 1; @@ -54,7 +54,7 @@ jack_ringbuffer_create (size_t sz) return NULL; } rb->mlocked = 0; - + return rb; } @@ -67,7 +67,7 @@ jack_ringbuffer_free (jack_ringbuffer_t * rb) if (rb->mlocked) { munlock (rb->buf, rb->size); } -#endif /* USE_MLOCK */ +#endif /* USE_MLOCK */ free (rb->buf); free (rb); } @@ -81,7 +81,7 @@ jack_ringbuffer_mlock (jack_ringbuffer_t * rb) if (mlock (rb->buf, rb->size)) { return -1; } -#endif /* USE_MLOCK */ +#endif /* USE_MLOCK */ rb->mlocked = 1; return 0; } @@ -104,10 +104,10 @@ 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 { @@ -174,8 +174,8 @@ 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 +/* 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 @@ -260,6 +260,7 @@ 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; } @@ -269,6 +270,7 @@ 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; } |