42 inline int inc(
int& p_var,
int p_size) {
45 p_var = p_var&size_mask;
52 ERR_FAIL_COND_V(space_left() < 1, T());
53 return data[inc(read_pos, 1)];
56 int read(T* p_buf,
int p_size,
bool p_advance=
true) {
57 int left = data_left();
58 p_size = MIN(left, p_size);
63 int end = pos + to_read;
64 end = MIN(end, size());
65 int total = end - pos;
66 for (
int i=0; i<total; i++) {
67 p_buf[dst++] = data[pos + i];
73 inc(read_pos, p_size);
78 int copy(T* p_buf,
int p_offset,
int p_size) {
80 int left = data_left();
81 if ((p_offset+p_size)>left) {
82 p_size-=left-p_offset;
86 p_size = MIN(left, p_size);
92 int end = pos + to_read;
93 end = MIN(end, size());
94 int total = end - pos;
95 for (
int i=0; i<total; i++) {
96 p_buf[dst++] = data[pos + i];
104 inline int advance_read(
int p_n) {
105 p_n = MIN(p_n, data_left());
110 Error write(
const T& p_v) {
111 ERR_FAIL_COND_V( space_left() < 1, FAILED);
112 data[inc(write_pos, 1)] = p_v;
116 int write(
const T* p_buf,
int p_size) {
118 int left = space_left();
119 p_size = MIN(left, p_size);
122 int to_write = p_size;
126 int end = pos + to_write;
127 end = MIN(end, size());
128 int total = end - pos;
130 for (
int i=0; i<total; i++) {
131 data[pos+i] = p_buf[src++];
137 inc(write_pos, p_size);
141 inline int space_left() {
142 int left = read_pos - write_pos;
144 return size() + left - 1;
151 inline int data_left() {
152 return size() - space_left() - 1;
159 inline void clear() {
165 void resize(
int p_power) {
166 int old_size = size();
167 int new_size = 1<<p_power;
168 int mask = new_size - 1;
169 data.resize(1<<p_power);
170 if (old_size < new_size && read_pos > write_pos) {
171 for (
int i=0; i<write_pos; i++) {
172 data[(old_size + i)&mask] = data[i];
174 write_pos = (old_size + write_pos) & mask;
176 read_pos = read_pos & mask;
177 write_pos = write_pos & mask;
Definition: ring_buffer.h:35