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