6 Fifo::Fifo(unsigned size = 18) {
7 //except at the beginning, _head and _tail must never overlap,
8 //otherwise one does not know whether the buffer is full or
9 //empty. To have nevertheless a buffer with the given capacity,
10 //a buffer with one more element is allocated.
12 _buffer = (char *) _malloc_align(_size, ALIGNMENT_FACTOR);
14 fprintf(stderr,"[FIFO] Memory allocation failure\n");
41 unsigned Fifo::read(void *destination, unsigned len) {
42 char* buffer = (char*) destination;
43 unsigned read = (len <= used() ? len : used());
45 if (_tail + read < _size) {
46 memcpy(buffer, _buffer + _tail, read);
48 memcpy(buffer, _buffer + _tail, _size - _tail);
49 memcpy(buffer + _size - _tail, _buffer, read - _size + _tail);
52 _tail = ((unsigned) (_tail + read) % _size);
60 unsigned Fifo::write(const void *source, unsigned len) {
62 char* buffer = (char*) source;
63 unsigned write = (len <= unused() ? len : unused());
64 unsigned head = (_tail + _pos) % _size;
66 if (head + write < _size) {
67 memcpy(_buffer + head, buffer, write);
69 memcpy(_buffer + head, buffer, _size - head);
70 memcpy(_buffer, buffer + _size - head, write - _size + head);
80 unsigned Fifo::size() const {
85 * How many bytes are currently free in the buffer
87 unsigned Fifo::unused() const {
88 return (_size) - used();
92 * How many data are currently stored in the fifo
94 unsigned Fifo::used() const {
99 * Get the pointer to the start of the queue
101 char *Fifo::getQueuePointer() {
106 * Has completed a dma read process, i.e. has read out of the queue
108 void Fifo::dmaRead(unsigned len) {
112 _blocked = BLOCKED_MAX_NR;
114 _tail = ((unsigned) (_tail + len) % _size);
120 * Start a DMA request, returns the current space one have
122 unsigned Fifo::dmaStart() {
125 if (_tail + _pos > _size) {
126 return _size - _tail;
133 * Is allowed to start a dma request
135 bool Fifo::dmaAllowed() {