esrf changes: slsReceiver: extend CircularFifo to support blocking/non-blocking transfers:

* Add blocking push (for performance) and non-blocking pop (for symmetry), default to blocking operations
* Fix memory fault if Fifo allocation fails
* Fix fifoFree initialisation to fifoSize elements (was fifoSize - 1)
This commit is contained in:
2018-04-23 17:32:40 +02:00
parent 8f9c657fa0
commit d38108e527
3 changed files with 60 additions and 51 deletions

View File

@ -50,28 +50,24 @@ int Fifo::CreateFifos(uint32_t fifoItemSize) {
fifoFree = new CircularFifo<char>(fifoDepth);
fifoStream = new CircularFifo<char>(fifoDepth);
//allocate memory
memory = (char*) calloc (fifoItemSize * fifoDepth, sizeof(char));
memset(memory,0,fifoItemSize * fifoDepth* sizeof(char));
size_t mem_len = fifoItemSize * fifoDepth * sizeof(char);
memory = (char*) malloc (mem_len);
if (memory == NULL){
FILE_LOG(logERROR) << "Could not allocate memory for fifos";
memory = 0;
return FAIL;
}
FILE_LOG(logDEBUG) << "Memory Allocated " << index << ": " << (fifoItemSize * fifoDepth) << " bytes";
memset(memory, 0, mem_len);
FILE_LOG(logDEBUG) << "Memory Allocated " << index << ": " << mem_len << " bytes";
{ //push free addresses into fifoFree fifo
char* buffer = memory;
while (buffer < (memory + fifoItemSize * (fifoDepth-1))) {
for (int i = 0; i < fifoDepth; ++i) {
//sprintf(buffer,"memory");
#ifdef FIFODEBUG
cprintf(MAGENTA,"Fifofree %d: value:%d, pop 0x%p\n", index, fifoFree->getSemValue(), (void*)(buffer));
#endif
FreeAddress(buffer);
buffer += fifoItemSize;
}
}
FILE_LOG(logDEBUG) << "Fifo Reconstructed Depth " << index << ": " << fifoDepth;
FILE_LOG(logINFO) << "Fifo " << index << " reconstructed Depth (rx_fifodepth): " << fifoFree->getDataValue();
return OK;
}
@ -79,7 +75,6 @@ int Fifo::CreateFifos(uint32_t fifoItemSize) {
void Fifo::DestroyFifos(){
FILE_LOG(logDEBUG) << __AT__ << " called";
if(memory) {
free(memory);
memory = 0;
@ -100,25 +95,22 @@ void Fifo::DestroyFifos(){
void Fifo::FreeAddress(char*& address) {
while(!fifoFree->push(address));
fifoFree->push(address);
}
void Fifo::GetNewAddress(char*& address) {
int temp = fifoFree->getSemValue();
int temp = fifoFree->getDataValue();
if (temp < status_fifoFree)
status_fifoFree = temp;
fifoFree->pop(address);
/*temp = fifoFree->getSemValue();
if (temp < status_fifoFree)
status_fifoFree = temp;*/
}
void Fifo::PushAddress(char*& address) {
int temp = fifoBound->getSemValue();
int temp = fifoBound->getDataValue();
if (temp > status_fifoBound)
status_fifoBound = temp;
while(!fifoBound->push(address));
/*temp = fifoBound->getSemValue();
/*temp = fifoBound->getDataValue();
if (temp > status_fifoBound)
status_fifoBound = temp;*/
}
@ -128,7 +120,7 @@ void Fifo::PopAddress(char*& address) {
}
void Fifo::PushAddressToStream(char*& address) {
while(!fifoStream->push(address));
fifoStream->push(address);
}
void Fifo::PopAddressToStream(char*& address) {
@ -141,7 +133,6 @@ int Fifo::GetMaxLevelForFifoBound() {
return temp;
}
int Fifo::GetMinLevelForFifoFree() {
int temp = status_fifoFree;
status_fifoFree = fifoDepth;