00001 /* 00002 * Copyright (C) 2009, Edmundo Albuquerque de Souza e Silva. 00003 * 00004 * This file may be distributed under the terms of the Q Public License 00005 * as defined by Trolltech AS of Norway and appearing in the file 00006 * LICENSE.QPL included in the packaging of this file. 00007 * 00008 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING 00009 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00010 * PURPOSE. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, 00011 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 00012 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00013 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00014 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00015 * 00016 * Thanks: Jose Renato Santos 00017 * 00018 */ 00019 00020 /////////////////////////////////////////////////////////////////////////////// 00021 // localqueue.cpp: singlely linked fifo queue primitives to be used 00022 // by a single thread (i.e. no mutex) 00023 /////////////////////////////////////////////////////////////////////////////// 00024 00025 #include "localqueue.h" 00026 #include "td4list.h" 00027 00028 #include <stddef.h> // for offsetof macro 00029 #include <iostream> 00030 00031 /////////////////////////////////////////////////////////////////////////////// 00032 LocalQueue::LocalQueue() 00033 { 00034 m_Head.linkf = 0; 00035 m_Head.linkb = &m_Head; 00036 } 00037 /////////////////////////////////////////////////////////////////////////////// 00038 LocalQueue::~LocalQueue() 00039 { 00040 } 00041 00042 /////////////////////////////////////////////////////////////////////////////// 00043 // First -- Return First element on queue (Do not remove element) 00044 LocalQueueEle *LocalQueue::First() 00045 { 00046 return( m_Head.linkf ); 00047 } 00048 00049 /////////////////////////////////////////////////////////////////////////////// 00050 // Put -- add element to end of queue 00051 void LocalQueue::Put( LocalQueueEle *ep ) 00052 { 00053 ep->linkf = 0; 00054 ep->linkb = ( m_Head.linkb ); 00055 ( m_Head.linkb )->linkf = ep; 00056 m_Head.linkb = ep; 00057 } 00058 00059 00060 /////////////////////////////////////////////////////////////////////////////// 00061 // Get -- Get first element on queue if available or return null otherwise 00062 LocalQueueEle *LocalQueue::Get() 00063 { 00064 LocalQueueEle *ep; 00065 ep = m_Head.linkf; 00066 00067 if( ep != 0 ) 00068 { 00069 Remove( ep ); 00070 } 00071 00072 return ep; 00073 } 00074 00075 /////////////////////////////////////////////////////////////////////////////// 00076 // Remove -- Remove an element from queue (element can be at any position 00077 // on queue) (Undefined behaviour if element is not in the queue) 00078 void LocalQueue::Remove( LocalQueueEle *ep ) 00079 { 00080 LocalQueueEle *prev = ep->linkb; 00081 LocalQueueEle *next = ep->linkf; 00082 00083 prev->linkf = next; 00084 if( next == 0 ) // check if last element on queue 00085 { 00086 m_Head.linkb = ep->linkb; // change last element pointer 00087 } 00088 else 00089 { 00090 next->linkb = ep->linkb; // change next back pointer 00091 } 00092 }