/Users/craigcornelius/Projects/SPRING Mac Release 0.2/blockQueue.h

Go to the documentation of this file.
00001 #ifndef BLOCKQUEUE_H
00002 #define BLOCKQUEUE_H
00003 #include <string.h>
00004 
00005 class blockQueue
00006 {
00007 public:
00008   blockQueue(int blockSize = 16*16*3, int queueSize = 512);
00009   ~blockQueue();
00010 
00011   int Enqueue(void *block);
00012   void *GetTailBlock();
00013   int Dequeue();
00014   bool isEmpty();
00015   inline bool isFull();
00016 
00017 private:
00018   int updateCurrentSize();
00019 
00020   unsigned char* data;
00021   int currentSize;
00022   int blockSize;
00023   int queueSize;
00024   long int tail;
00025   long int head;
00026 };
00027 
00028 
00029 
00030 inline int blockQueue::Enqueue(void *block)
00031 {
00032   // No DeQueue / EnQueue concurency pb since only Enqueue modifies head
00033   // and only Dequeue modifies tail
00034   // Moreover, we will not write on tail (that is write on a block possibly been
00035   // read by the GetNextBlock) since the ++tail happens only after Dequeue... and
00036   // we test if queue full
00037   if (!isFull())
00038   {
00039     memcpy(data + (head%queueSize)*blockSize, block, blockSize);
00040     ++head;
00041     return updateCurrentSize();
00042   }
00043   else
00044     return -1;
00045 }
00046 
00047 inline void *blockQueue::GetTailBlock()
00048 {
00049   return (void *)(data + (tail%queueSize)*blockSize);
00050 }
00051 
00052 inline int blockQueue::Dequeue()
00053 {
00054   // Must happen AFTER reading/processing the data (thru GetNextBlock), thus preventing basic concurency
00055   // pb where, in case of a full queue, enqueue tries to write on the block currently processed, because
00056   // the ++tail has been executed too early (before processing the data)
00057   if (!isEmpty())
00058   {
00059     ++tail;
00060     return updateCurrentSize();
00061   }
00062   else
00063     return -1;
00064 }
00065 
00066 inline bool blockQueue::isEmpty()
00067 {
00068   return (currentSize <= 0);
00069 }
00070 
00071 inline bool blockQueue::isFull()
00072 {
00073   return (currentSize >= queueSize);
00074 }
00075 
00076 inline int blockQueue::updateCurrentSize()
00077 {
00078    return (currentSize = head - tail);
00079 }
00080 
00081 #endif // blockQueue.h

Generated on Thu Aug 30 11:03:13 2007 for SPRING Mac by  doxygen 1.5.3