| 128 | |
| 129 | template <typename T> |
| 130 | void RingBuffer<T>::SetCapacity(uint32_t capacity) |
| 131 | { |
| 132 | size_t old_capacity = Capacity(); |
| 133 | if (capacity == old_capacity) |
| 134 | return; |
| 135 | |
| 136 | if (capacity == 0) |
| 137 | { |
| 138 | Free(); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | T* newbuffer = newbuffer = (T*)malloc(capacity*sizeof(T)); |
| 143 | |
| 144 | uint32_t size = Size(); |
| 145 | if (size > capacity) |
| 146 | { |
| 147 | size = capacity; |
| 148 | } |
| 149 | |
| 150 | // copy elements in reverse in order to keep the last inserted items |
| 151 | // E.g. resize from cap=6 to cap=4: 123456 -> 3456 |
| 152 | for (int i = ((int)size)-1; i >= 0; --i) |
| 153 | { |
| 154 | newbuffer[i] = (*this)[(uint32_t)i]; |
| 155 | } |
| 156 | |
| 157 | T* tmp = m_Buffer; |
| 158 | m_Buffer = newbuffer; |
| 159 | free(tmp); |
| 160 | |
| 161 | m_Tail = 0; |
| 162 | m_Head = size; |
| 163 | m_Max = capacity; |
| 164 | m_Full = capacity == size ? 1 : 0; |
| 165 | } |
| 166 | |
| 167 | template <typename T> |
| 168 | void RingBuffer<T>::OffsetCapacity(uint32_t grow) |