3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2008 Christian Schoenebeck * |
* Copyright (C) 2005 - 2012 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
68 |
{ |
{ |
69 |
public: |
public: |
70 |
RingBuffer (int sz, int wrap_elements = DEFAULT_WRAP_ELEMENTS) : |
RingBuffer (int sz, int wrap_elements = DEFAULT_WRAP_ELEMENTS) : |
71 |
write_ptr(0), read_ptr(0) { |
write_ptr(0), read_ptr(0) |
72 |
int power_of_two; |
{ |
73 |
|
_allocBuffer(sz, wrap_elements); |
74 |
this->wrap_elements = wrap_elements; |
} |
75 |
|
|
76 |
// the write-with-wrap functions need wrap_elements extra |
/** |
77 |
// space in the buffer to be able to copy the wrap space |
* Resize this ring buffer to the given size. This operation |
78 |
sz += wrap_elements; |
* is not thread safe! Any operations using this RingBuffer |
79 |
|
* have to be stopped before calling this method. |
80 |
for (power_of_two = 1; |
* |
81 |
1<<power_of_two < sz; |
* @param sz - new size (amount of elements) |
82 |
power_of_two++); |
* @param wrap_elements - (optional) if supplied, the new amount |
83 |
|
* of wrap elements to be used beyond |
84 |
size = 1<<power_of_two; |
* official buffer end, if not provided |
85 |
size_mask = size; |
* the amount wrap_elements remains as it was |
86 |
size_mask -= 1; |
* before |
87 |
buf = new T[size + wrap_elements]; |
*/ |
88 |
}; |
void resize(int sz, int wrap_elements = -1) { |
89 |
|
if (wrap_elements == -1) |
90 |
|
wrap_elements = this->wrap_elements; |
91 |
|
|
92 |
|
delete [] buf; |
93 |
|
|
94 |
|
_allocBuffer(sz, wrap_elements); |
95 |
|
} |
96 |
|
|
97 |
virtual ~RingBuffer() { |
virtual ~RingBuffer() { |
98 |
delete [] buf; |
delete [] buf; |
437 |
* \a pSrc to the buffer given by \a pDst. |
* \a pSrc to the buffer given by \a pDst. |
438 |
*/ |
*/ |
439 |
inline static void copy(T* pDst, T* pSrc, int n); |
inline static void copy(T* pDst, T* pSrc, int n); |
440 |
|
|
441 |
|
void _allocBuffer(int sz, int wrap_elements) { |
442 |
|
this->wrap_elements = wrap_elements; |
443 |
|
|
444 |
|
// the write-with-wrap functions need wrap_elements extra |
445 |
|
// space in the buffer to be able to copy the wrap space |
446 |
|
sz += wrap_elements; |
447 |
|
|
448 |
|
int power_of_two; |
449 |
|
for (power_of_two = 1; |
450 |
|
1<<power_of_two < sz; |
451 |
|
power_of_two++); |
452 |
|
|
453 |
|
size = 1<<power_of_two; |
454 |
|
size_mask = size; |
455 |
|
size_mask -= 1; |
456 |
|
buf = new T[size + wrap_elements]; |
457 |
|
} |
458 |
|
|
459 |
friend class _NonVolatileReader<T,T_DEEP_COPY>; |
friend class _NonVolatileReader<T,T_DEEP_COPY>; |
460 |
}; |
}; |