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