int discard(int length) noexcept { // do nothing if not enough used space to discard anyway if (length > used_space()) return 0; // commit no-op write and return length this->used -= length; this->start = (this->start + length) % capacity(); return length; }
int read(char* dest, int length) noexcept { if (length > used_space()) { length = used_space(); if (length == 0) return 0; } // check if we are going around the buffer ... int wrap = (start + length) - this->cap; if (wrap > 0) { memcpy(dest, at_start(), length - wrap); memcpy(dest + length - wrap, this->buffer, wrap); } else { memcpy(dest, at_start(), length); } this->used -= length; // make sure it wraps properly around this->start = (this->start + length) % capacity(); return length; }
/* read */ static int fifo_read(ubx_block_t *i, ubx_data_t* msg) { int ret=0; unsigned long readsz=0, readsz1=0, readsz2=0, used; struct fifo_block_info *bbi; bbi = (struct fifo_block_info*) i->private_data; if((ret=pthread_mutex_lock(&bbi->mutex))!=0) { ERR2(ret, "failed to lock mutex"); goto out; } if(bbi->rdptr == bbi->wrptr) { goto out_unlock; } if(msg->type != bbi->type) { ERR("invalid read type '%s' (expected '%s'", get_typename(msg), bbi->type->name); ret=EPORT_INVALID_TYPE; goto out_unlock; } /* bytes */ used = used_space(bbi); readsz=(used<readsz) ? used : readsz; /* chunk 2*/ if(bbi->rdptr > bbi->wrptr) { readsz2 = readsz - (bbi->buff + bbi->size - bbi->wrptr); readsz1 = readsz-readsz2; } memcpy(msg->data, bbi->rdptr, readsz1); bbi->rdptr=&bbi->buff[readsz1]; if(readsz2>0) { } ret=readsz/bbi->type->size; /* compute # elements read */ out_unlock: pthread_mutex_unlock(&bbi->mutex); out: return ret; }
int free_space() const noexcept { return capacity() - used_space(); }
int size() const noexcept { return used_space(); }
bool empty() const noexcept { return used_space() == 0; }
bool full() const noexcept { return used_space() == capacity(); }
size_t file_size(file_handle_t * handle) { return used_space(handle); }