/* * * This file is part of bufferpool * * Copyright (C) 2007 by LScube team * See AUTHORS for more details * * bufferpool is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * bufferpool is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with bufferpool; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ #include #include #include /*! Read the next slot in buffer. * * Read the next slot in the buffer fill the parameters given as input and move * current read position. * * @param cons is the consumer. * @param timestamp return position for timestamp value of slot read. * @param marker return position for marker value of slot read. * @param data position of where to copy data filed of the read slot. * @param data_size input value for size of data buffer and return * value for size of effective size of read data. * * @return -1 on error, 1 if slot data size is bigger than the buffer * provided and then it was not possible to copy all the data, 0 otherwise. * **/ // BPSlot *bp_read(BPConsumer *cons) int bp_read(BPConsumer *cons, uint32_t *timestamp, uint8_t *marker, uint8_t * data, uint32_t* data_size) { BPSlot *last_read; BPSlot *next; uint32_t cpy_size; int ret = -1; // wrong by default bp_lock(cons->buffer); if(bp_shm_refresh(cons->buffer)) goto err_unlock; //we use BPtoSlot 'cause last_read could be NULL last_read = BPtoSlot(cons->buffer, cons->last_read_pos); next = &cons->buffer->slots[cons->read_pos]; if (!next->refs || (next->slot_seq < cons->last_seq)) { // added some slots? if (last_read && cons->buffer->slots[last_read->next].refs && (cons->buffer->slots[last_read->next].slot_seq > cons->last_seq)) next = &cons->buffer->slots[last_read->next]; else { goto err_unlock; } } else if (last_read && (cons->buffer->slots[last_read->next].slot_seq < next->slot_seq)) next = &cons->buffer->slots[last_read->next]; cpy_size = omsbuff_min(*data_size, next->data_size); next->refs--; cons->last_seq = next->slot_seq; // cons->read_pos = next; cons->read_pos = next->next; // cons->read_pos->refs--; // fill input parameters *timestamp = next->timestamp; *marker = next->marker; memcpy(data, next->data, cpy_size); *data_size = cpy_size; cons->last_read_pos = BPtoSlotPtr(cons->buffer, next); ret = (cpy_size == next->data_size) ? 0 : 1; err_unlock: bp_unlock(cons->buffer); return ret; // next; }