/* class for managing byte positions and associated time positions Copyright (C) 1999 Martin Vogt This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation. For more information look at the file COPYRIGHT in this package */ #include #include "timeStampArray.h" using namespace std; TimeStampArray::TimeStampArray(char* aName,int entries) { writePos=0; readPos=0; fillgrade=0; lastWritePos=0; this->entries=entries; if (entries < 1) { cout << "TimeStampArray entries must be >= 1"; exit(0); } abs_thread_mutex_init(&writeInMut); abs_thread_mutex_init(&changeMut); name=strdup(aName); int i; tStampArray=new TimeStamp*[entries]; for(i=0;icopyTo(tStampArray[writePos]); tStampArray[writePos]->setKey(key,len); /* if (fillgrade > 0) { if (tStampArray[lastWritePos]->getKey() == key) { unlockStampArray(); return; } } */ lastWritePos=writePos; writePos++; fillgrade++; if (writePos == entries) { writePos=0; } if (fillgrade == entries) { cout << name<<" TimeStampArray::array overfull forward"<getKey()-key; unlockStampArray(); return back; } TimeStamp* TimeStampArray::getTimeStamp(long key) { lockStampArray(); TimeStamp* back=tStampArray[readPos]; if (key > back->getKey()+back->getKeyLen()) { if (fillgrade > 1) { internalForward(); unlockStampArray(); return getTimeStamp(key); } } /* if (back->getKey() > key) { cout << "key "<getKey()-key<print("key access"); } */ unlockStampArray(); /* maybe we should return NULL here to indicate that there is no valid timestamp */ /* This would need a check for every getTimeStamp call I think returning the last available stamp is ok */ return back; } void TimeStampArray::forward() { lockStampArray(); internalForward(); unlockStampArray(); } void TimeStampArray::clear() { lockStampArray(); writePos=0; readPos=0; fillgrade=0; unlockStampArray(); } void TimeStampArray::lockStampArray() { abs_thread_mutex_lock(&changeMut); abs_thread_mutex_lock(&writeInMut); abs_thread_mutex_unlock(&changeMut); } void TimeStampArray::unlockStampArray() { abs_thread_mutex_unlock(&writeInMut); } void TimeStampArray::internalForward() { readPos++; fillgrade--; if (readPos == entries) { readPos=0; } }