15 #include "pv_circular_buffer.h"
33 if (element_size <= 0) {
47 o->
buffer = malloc(capacity * element_size);
78 if ((length <= 0) || (length >= object->
capacity)) {
82 void *dst_ptr = buffer;
85 const int32_t available =
object->capacity -
object->read_index;
86 const int32_t max_copy = (
object->count < length) ? object->
count : length;
87 const int32_t to_copy = (max_copy < available) ? max_copy : available;
89 memcpy(dst_ptr, src_ptr, to_copy * object->
element_size);
91 object->read_index = (
object->read_index + to_copy) % object->
capacity;
93 const int32_t remaining = max_copy - to_copy;
95 dst_ptr = (
char *) buffer + (to_copy * object->
element_size);
96 src_ptr =
object->buffer;
98 memcpy(dst_ptr, src_ptr, remaining * object->
element_size);
100 object->read_index = remaining;
103 object->count -= max_copy;
115 if ((length <= 0) || (length >= object->
capacity)) {
122 const void *src_ptr = buffer;
124 const int32_t available =
object->capacity -
object->write_index;
125 const int32_t to_copy = (length < available) ? length : available;
127 memcpy(dst_ptr, src_ptr, to_copy * object->
element_size);
129 object->write_index = (
object->write_index + to_copy) % object->
capacity;
130 object->
count += to_copy;
132 const int32_t remaining = length - to_copy;
134 dst_ptr =
object->buffer;
135 src_ptr = (
char *) buffer + (to_copy * object->
element_size);
137 memcpy(dst_ptr, src_ptr, remaining * object->
element_size);
139 object->write_index = remaining;
140 object->count += remaining;
145 object->count =
object->capacity;
146 object->read_index = (
object->write_index + 1) % object->
capacity;
154 object->read_index = 0;
155 object->write_index = 0;
159 static const char *
const STRINGS[] = {
165 int32_t size =
sizeof(STRINGS) /
sizeof(STRINGS[0]);