read oldest item from buffer
| 169 | |
| 170 | // read oldest item from buffer |
| 171 | void *CircularBuffer_Read |
| 172 | ( |
| 173 | CircularBuffer cb, // buffer to read item from |
| 174 | void *item // [optional] pointer populated with removed item |
| 175 | ) { |
| 176 | ASSERT(cb != NULL); |
| 177 | |
| 178 | // make sure there's data to return |
| 179 | if(unlikely(CircularBuffer_Empty(cb))) { |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | void *read = cb->read; |
| 184 | |
| 185 | // update buffer item count |
| 186 | cb->item_count--; |
| 187 | |
| 188 | // copy item from buffer to output |
| 189 | if(item != NULL) { |
| 190 | memcpy(item, cb->read, cb->item_size); |
| 191 | } |
| 192 | |
| 193 | // advance read position |
| 194 | // circle back if read reached the end of the buffer |
| 195 | cb->read += cb->item_size; |
| 196 | if(unlikely(cb->read >= cb->end_marker)) { |
| 197 | cb->read = cb->data; |
| 198 | } |
| 199 | |
| 200 | // return original read position |
| 201 | return read; |
| 202 | } |
| 203 | |
| 204 | // free buffer (does not free its elements if its free callback is NULL) |
| 205 | void CircularBuffer_Free |