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