paul@93 | 1 | /* |
paul@93 | 2 | * A page mapper providing memory pages to satisfy file accesses. |
paul@93 | 3 | * |
paul@93 | 4 | * Copyright (C) 2021 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@6 | 22 | #include "ipc.h" |
paul@6 | 23 | #include "page_mapper.h" |
paul@6 | 24 | |
paul@6 | 25 | |
paul@6 | 26 | |
paul@72 | 27 | PageMapper::PageMapper(Accessor *accessor, Pages *pages) |
paul@197 | 28 | : _accessor(accessor), _pages(pages) |
paul@6 | 29 | { |
paul@6 | 30 | } |
paul@6 | 31 | |
paul@6 | 32 | /* Accounting methods. */ |
paul@6 | 33 | |
paul@6 | 34 | /* Detach a pager, purging active pages and closing the accessor if no more |
paul@6 | 35 | pagers are attached. Return whether any pagers are still attached. */ |
paul@6 | 36 | |
paul@6 | 37 | unsigned int PageMapper::detach() |
paul@6 | 38 | { |
paul@197 | 39 | unsigned int attached = Accountable::detach(); |
paul@6 | 40 | |
paul@197 | 41 | if (!attached) |
paul@197 | 42 | { |
paul@197 | 43 | _map.purge(this, _pages); |
paul@197 | 44 | _accessor->close(); |
paul@6 | 45 | } |
paul@6 | 46 | |
paul@197 | 47 | return attached; |
paul@6 | 48 | } |
paul@6 | 49 | |
paul@6 | 50 | /* Interface for the pager. */ |
paul@6 | 51 | |
paul@6 | 52 | /* Return a flexpage providing access to the indicated file 'offset'. |
paul@6 | 53 | |
paul@6 | 54 | The returned flexpage will either be an existing, compatible flexpage or a |
paul@6 | 55 | completely new flexpage. |
paul@6 | 56 | |
paul@52 | 57 | This method locks the mapper to prevent concurrent queries with the same |
paul@52 | 58 | details, with the lock held until the queue operation releases the lock. */ |
paul@6 | 59 | |
paul@121 | 60 | Flexpage *PageMapper::get(offset_t offset, map_flags_t flags) |
paul@6 | 61 | { |
paul@6 | 62 | _lock.lock(); |
paul@6 | 63 | |
paul@6 | 64 | Flexpage *f = find(offset); |
paul@6 | 65 | |
paul@6 | 66 | if (f == NULL) |
paul@6 | 67 | f = flexpage(offset); |
paul@6 | 68 | |
paul@11 | 69 | /* Record a new user of the flexpage and upgrade the access flags. */ |
paul@11 | 70 | |
paul@6 | 71 | f->increment(); |
paul@11 | 72 | f->upgrade(flags); |
paul@6 | 73 | return f; |
paul@6 | 74 | } |
paul@6 | 75 | |
paul@6 | 76 | /* Queue the given 'flexpage' in the page collection, making it available for |
paul@6 | 77 | eventual reuse. |
paul@6 | 78 | |
paul@52 | 79 | This method unlocks the mapper. */ |
paul@6 | 80 | |
paul@6 | 81 | void PageMapper::queue(Flexpage *flexpage) |
paul@6 | 82 | { |
paul@6 | 83 | _pages->queue(this, flexpage); |
paul@6 | 84 | |
paul@6 | 85 | _lock.unlock(); |
paul@6 | 86 | } |
paul@6 | 87 | |
paul@6 | 88 | /* Flush pages in the given range from 'start' with 'size'. */ |
paul@6 | 89 | |
paul@6 | 90 | void PageMapper::flush_all(offset_t start, offset_t size) |
paul@6 | 91 | { |
paul@6 | 92 | std::lock_guard<std::mutex> guard(_lock); |
paul@6 | 93 | |
paul@6 | 94 | _map.flush_all(start, size, this, _pages); |
paul@6 | 95 | } |
paul@6 | 96 | |
paul@6 | 97 | /* Return the maximum extent of the mapped resource. */ |
paul@6 | 98 | |
paul@6 | 99 | offset_t PageMapper::get_data_size() |
paul@6 | 100 | { |
paul@6 | 101 | return _accessor->get_size(); |
paul@6 | 102 | } |
paul@6 | 103 | |
paul@53 | 104 | /* Set the maximum extent of the mapped resource. */ |
paul@53 | 105 | |
paul@53 | 106 | void PageMapper::set_data_size(offset_t size) |
paul@53 | 107 | { |
paul@53 | 108 | _accessor->set_size(size); |
paul@53 | 109 | } |
paul@53 | 110 | |
paul@6 | 111 | /* Internal flexpage retrieval methods. */ |
paul@6 | 112 | |
paul@6 | 113 | /* Find an existing flexpage for 'offset'. Where the accessor has registered a |
paul@6 | 114 | compatible flexpage, an attempt is made to reserve it in the page collection; |
paul@6 | 115 | if this succeeds, the flexpage is returned. Otherwise, NULL is returned. */ |
paul@6 | 116 | |
paul@6 | 117 | Flexpage *PageMapper::find(offset_t offset) |
paul@6 | 118 | { |
paul@6 | 119 | Flexpage *flexpage = _map.find(offset); |
paul@6 | 120 | |
paul@6 | 121 | /* Between finding and reserving a flexpage, there is a possibility that |
paul@6 | 122 | another accessor might acquire the flexpage, issue it, and even purge |
paul@6 | 123 | it. */ |
paul@6 | 124 | |
paul@6 | 125 | if ((flexpage != NULL) && _pages->reserve(this, flexpage)) |
paul@6 | 126 | return flexpage; |
paul@6 | 127 | else |
paul@6 | 128 | return NULL; |
paul@6 | 129 | } |
paul@6 | 130 | |
paul@6 | 131 | /* Obtain a new flexpage for the file 'offset'. If the page collection is unable |
paul@6 | 132 | to obtain a completely new flexpage, an existing flexpage is requested from |
paul@6 | 133 | the page collection and recycled. |
paul@6 | 134 | |
paul@6 | 135 | The obtained flexpage is filled with content. */ |
paul@6 | 136 | |
paul@6 | 137 | Flexpage *PageMapper::flexpage(offset_t offset) |
paul@6 | 138 | { |
paul@6 | 139 | Flexpage *flexpage = _pages->flexpage(); |
paul@6 | 140 | |
paul@6 | 141 | /* Obtain an existing flexpage and reuse it. */ |
paul@6 | 142 | |
paul@6 | 143 | if (flexpage == NULL) |
paul@6 | 144 | flexpage = _pages->remove(); |
paul@6 | 145 | |
paul@6 | 146 | flexpage->reset(offset); |
paul@6 | 147 | |
paul@6 | 148 | fill(flexpage); |
paul@6 | 149 | _map.insert(flexpage); |
paul@6 | 150 | return flexpage; |
paul@6 | 151 | } |
paul@6 | 152 | |
paul@6 | 153 | /* Interface for the page collection. */ |
paul@6 | 154 | |
paul@6 | 155 | /* Remove the record of 'flexpage' in this accessor, flushing its content. */ |
paul@6 | 156 | |
paul@6 | 157 | void PageMapper::remove(Flexpage *flexpage) |
paul@6 | 158 | { |
paul@6 | 159 | _map.remove(this, flexpage); |
paul@6 | 160 | } |
paul@6 | 161 | |
paul@6 | 162 | /* Data transfer methods. */ |
paul@6 | 163 | |
paul@6 | 164 | void PageMapper::fill(Flexpage *flexpage) |
paul@6 | 165 | { |
paul@6 | 166 | _accessor->fill(flexpage); |
paul@6 | 167 | } |
paul@6 | 168 | |
paul@6 | 169 | void PageMapper::flush(Flexpage *flexpage, bool purge) |
paul@6 | 170 | { |
paul@6 | 171 | if (flexpage->decrement() || purge) |
paul@6 | 172 | { |
paul@115 | 173 | if (flexpage->modified()) |
paul@115 | 174 | _accessor->flush(flexpage); |
paul@115 | 175 | |
paul@6 | 176 | ipc_unmap_flexpage(flexpage); |
paul@6 | 177 | flexpage->invalidate(); |
paul@6 | 178 | } |
paul@6 | 179 | } |
paul@6 | 180 | |
paul@6 | 181 | // vim: tabstop=4 expandtab shiftwidth=4 |