dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * |
| 5 | * This software is distributed under the terms of the GNU Affero Public License. |
| 6 | * See the COPYING file in the main directory for details. |
| 7 | * |
| 8 | * This use of this software may be subject to additional restrictions. |
| 9 | * See the LEGAL file in the main directory for details. |
| 10 | |
| 11 | This program is free software: you can redistribute it and/or modify |
| 12 | it under the terms of the GNU Affero General Public License as published by |
| 13 | the Free Software Foundation, either version 3 of the License, or |
| 14 | (at your option) any later version. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU Affero General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU Affero General Public License |
| 22 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | #include "LinkedLists.h" |
| 30 | |
| 31 | |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 32 | void PointerFIFO::push_front(void* val) // by pat |
| 33 | { |
| 34 | // Pat added this routine for completeness, but never used or tested. |
| 35 | // The first person to use this routine should remove this assert. |
| 36 | ListNode *node = allocate(); |
| 37 | node->data(val); |
| 38 | node->next(mHead); |
| 39 | mHead = node; |
| 40 | if (!mTail) mTail=node; |
| 41 | mSize++; |
| 42 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 43 | |
| 44 | void PointerFIFO::put(void* val) |
| 45 | { |
| 46 | ListNode *node = allocate(); |
| 47 | node->data(val); |
| 48 | node->next(NULL); |
| 49 | if (mTail!=NULL) mTail->next(node); |
| 50 | mTail=node; |
| 51 | if (mHead==NULL) mHead=node; |
| 52 | mSize++; |
| 53 | } |
| 54 | |
| 55 | /** Take an item from the FIFO. */ |
| 56 | void* PointerFIFO::get() |
| 57 | { |
| 58 | // empty list? |
| 59 | if (mHead==NULL) return NULL; |
| 60 | // normal case |
| 61 | ListNode* next = mHead->next(); |
| 62 | void* retVal = mHead->data(); |
| 63 | release(mHead); |
| 64 | mHead = next; |
| 65 | if (next==NULL) mTail=NULL; |
| 66 | mSize--; |
| 67 | return retVal; |
| 68 | } |
| 69 | |
| 70 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 71 | ListNode *PointerFIFO::allocate() |
| 72 | { |
| 73 | if (mFreeList==NULL) return new ListNode; |
| 74 | ListNode* retVal = mFreeList; |
| 75 | mFreeList = mFreeList->next(); |
| 76 | return retVal; |
| 77 | } |
| 78 | |
| 79 | void PointerFIFO::release(ListNode* wNode) |
| 80 | { |
| 81 | wNode->next(mFreeList); |
| 82 | mFreeList = wNode; |
| 83 | } |