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