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