/[svn]/linuxsampler/trunk/src/common/RingBuffer.h
ViewVC logotype

Diff of /linuxsampler/trunk/src/common/RingBuffer.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 242 by schoenebeck, Tue Apr 27 09:21:58 2004 UTC revision 243 by schoenebeck, Fri Sep 17 00:52:48 2004 UTC
# Line 249  public: Line 249  public:
249      int size;      int size;
250      int wrap_elements;      int wrap_elements;
251    
252        /**
253         * Independent, random access reading from a RingBuffer. This class
254         * allows to read from a RingBuffer without being forced to free read
255         * data while reading / positioning.
256         */
257        template<class _T>
258        class _NonVolatileReader {
259            public:
260                int read_space() {
261                    int r = read_ptr;
262                    int w = atomic_read(&pBuf->write_ptr);
263                    return (w >= r) ? w - r : (w - r + pBuf->size) & pBuf->size_mask;
264                }
265    
266                /**
267                 * Reads one element from the NonVolatileReader's current read
268                 * position and copies it to the variable pointed by \a dst and
269                 * finally increments the NonVolatileReader's read position by
270                 * one.
271                 *
272                 * @param dst - where the element is copied to
273                 * @returns 1 on success, 0 otherwise
274                 */
275                int pop(T* dst) { return read(dst,1); }
276    
277                /**
278                 * Reads \a cnt elements from the NonVolatileReader's current
279                 * read position and copies it to the buffer pointed by \a dest
280                 * and finally increments the NonVolatileReader's read position
281                 * by the number of read elements.
282                 *
283                 * @param dest - destination buffer
284                 * @param cnt  - number of elements to read
285                 * @returns number of read elements
286                 */
287                int read(T* dest, int cnt) {
288                    int free_cnt;
289                    int cnt2;
290                    int to_read;
291                    int n1, n2;
292                    int priv_read_ptr;
293    
294                    priv_read_ptr = read_ptr;
295    
296                    if ((free_cnt = read_space()) == 0) return 0;
297    
298                    to_read = cnt > free_cnt ? free_cnt : cnt;
299    
300                    cnt2 = priv_read_ptr + to_read;
301    
302                    if (cnt2 > pBuf->size) {
303                        n1 = pBuf->size - priv_read_ptr;
304                        n2 = cnt2 & pBuf->size_mask;
305                    } else {
306                        n1 = to_read;
307                        n2 = 0;
308                    }
309    
310                    memcpy(dest, &pBuf->buf[priv_read_ptr], n1 * sizeof(T));
311                    priv_read_ptr = (priv_read_ptr + n1) & pBuf->size_mask;
312    
313                    if (n2) {
314                        memcpy(dest+n1, pBuf->buf, n2 * sizeof(T));
315                        priv_read_ptr = n2;
316                    }
317    
318                    this->read_ptr = priv_read_ptr;
319                    return to_read;
320                }
321            protected:
322                _NonVolatileReader(RingBuffer<_T>* pBuf) {
323                    this->pBuf     = pBuf;
324                    this->read_ptr = atomic_read(&pBuf->read_ptr);
325                }
326    
327                RingBuffer<_T>* pBuf;
328                int read_ptr;
329    
330                friend class RingBuffer<_T>;
331        };
332    
333        typedef _NonVolatileReader<T> NonVolatileReader;
334    
335        NonVolatileReader get_non_volatile_reader() { return NonVolatileReader(this); }
336    
337    protected:    protected:
338      T *buf;      T *buf;
339      atomic_t write_ptr;      atomic_t write_ptr;

Legend:
Removed from v.242  
changed lines
  Added in v.243

  ViewVC Help
Powered by ViewVC