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