1 /* 2 * A page collection abstraction providing pages from a queue to users. 3 * 4 * Copyright (C) 2021 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 "memory_incremental.h" 23 #include "pages.h" 24 25 26 27 Pages::Pages(Memory *memory, PageQueue *queue) 28 : _memory(memory), _queue(queue) 29 { 30 } 31 32 Pages::~Pages() 33 { 34 _queue->close(_memory); 35 } 36 37 /* Remove the first flexpage in the queue. If its owner exists, remove it from 38 the owner (retiring the content). Return the flexpage for reuse. */ 39 40 Flexpage *Pages::remove() 41 { 42 PageOwner *owner; 43 Flexpage *flexpage; 44 45 _queue->pop(&owner, &flexpage); 46 47 if (owner != NULL) 48 owner->remove(flexpage); 49 50 return flexpage; 51 } 52 53 /* Reserve 'flexpage' by removing it from this collection. */ 54 55 bool Pages::reserve(PageOwner *owner, Flexpage *flexpage) 56 { 57 return _queue->remove(owner, flexpage); 58 } 59 60 /* Obtain a new flexpage. Return the flexpage or None if no new flexpage could 61 be created. */ 62 63 Flexpage *Pages::flexpage() 64 { 65 Region *region = _memory->region(); 66 67 if (region != NULL) 68 return new Flexpage(region); 69 else 70 return NULL; 71 } 72 73 /* Queue an entry associating the given 'owner' and 'flexpage'. */ 74 75 void Pages::queue(PageOwner *owner, Flexpage *flexpage) 76 { 77 _queue->push(owner, flexpage); 78 } 79 80 /* Push an entry for 'flexpage' without owner association for immediate 81 reuse. */ 82 83 void Pages::release(Flexpage *flexpage) 84 { 85 _queue->push_front(NULL, flexpage); 86 } 87 88 // vim: tabstop=4 expandtab shiftwidth=4