1 /* 2 * A page collection abstraction providing pages from a queue to users. 3 * 4 * Copyright (C) 2021, 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include "pages.h" 23 24 25 26 Pages::Pages(Memory *memory, PageQueue *queue) 27 : _memory(memory), _queue(queue) 28 { 29 } 30 31 Pages::~Pages() 32 { 33 _queue->close(_memory); 34 } 35 36 /* Remove the first flexpage in the queue. If its owner exists, remove it from 37 the owner (retiring the content). Return the flexpage for reuse. */ 38 39 Flexpage *Pages::remove() 40 { 41 PageOwner *owner; 42 Flexpage *flexpage; 43 44 _queue->pop(&owner, &flexpage); 45 46 if (owner != NULL) 47 owner->remove(flexpage); 48 49 return flexpage; 50 } 51 52 /* Reserve 'flexpage' by removing it from this collection. */ 53 54 bool Pages::reserve(PageOwner *owner, Flexpage *flexpage) 55 { 56 return _queue->remove(owner, flexpage); 57 } 58 59 /* Obtain a new flexpage. Return the flexpage or None if no new flexpage could 60 be created. */ 61 62 Flexpage *Pages::flexpage() 63 { 64 Region *region = _memory->region(); 65 66 if (region != NULL) 67 return new Flexpage(region); 68 else 69 return remove(); 70 } 71 72 /* Queue an entry associating the given 'owner' and 'flexpage'. */ 73 74 void Pages::queue(PageOwner *owner, Flexpage *flexpage) 75 { 76 _queue->push(owner, flexpage); 77 } 78 79 /* Push an entry for 'flexpage' without owner association for immediate 80 reuse. */ 81 82 void Pages::release(Flexpage *flexpage) 83 { 84 _queue->push_front(NULL, flexpage); 85 } 86 87 // vim: tabstop=4 expandtab shiftwidth=4