1 /* 2 * A flexpage abstraction. 3 * 4 * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <algorithm> 23 24 #include "flexpage.h" 25 26 27 28 /* Reset the flexpage using 'offset', being the file offset. */ 29 30 void Flexpage::reset(offset_t offset) 31 { 32 _counter = 0; 33 34 /* By definition (see "Flexible-Sized Page Objects - Object-Orientation 35 in Operation Systems"), flexpages are aligned to multiples of their 36 size. 37 38 The size of the flexpage depends on the amount of space around the 39 accessed page. It cannot exceed the size of the memory region. */ 40 41 size = max_multiple(region->start, region->end, PAGE_SIZE); 42 43 /* The base address of the flexpage is computed from the region start 44 and flexpage size. It will be no lower than the region start. 45 46 Sent flexpages may use higher bases due to receive window constraints, 47 these being communicated by the "hot spot". */ 48 49 base_addr = round(region->start, size); 50 51 /* Get the file offset for the base of the flexpage. This will be a 52 multiple of the flexpage size for alignment purposes. */ 53 54 base_offset = trunc(offset, size); 55 56 /* The page being accessed is relative to the base. 57 (This is transient information recording the initialising access 58 details.) */ 59 60 page_offset = trunc(offset - base_offset, PAGE_SIZE); 61 page_addr = base_addr + page_offset; 62 } 63 64 /* Decrement the usage counter, returning whether the flexpage is now no longer 65 used. */ 66 67 bool Flexpage::decrement() 68 { 69 if (_counter) 70 { 71 _counter--; 72 return _counter == 0; 73 } 74 else 75 return 0; 76 } 77 78 /* Increment the usage counter. */ 79 80 void Flexpage::increment() 81 { 82 _counter++; 83 } 84 85 /* Invalidate the flexpage, meaning that it should not now be in use. */ 86 87 void Flexpage::invalidate() 88 { 89 _counter = 0; 90 } 91 92 /* Return whether the flexpage is in use and is therefore valid. */ 93 94 bool Flexpage::valid() 95 { 96 return _counter != 0; 97 } 98 99 /* Return whether the flexpage supports the given file 'position'. */ 100 101 bool Flexpage::supports_position(offset_t position) 102 { 103 return (base_offset <= position) && (position < (base_offset + size)); 104 } 105 106 /* Upgrade the flags involved with this flexpage. This is used to track the 107 maximal flags employed by the different pagers, with the result being used in 108 unmap operations. */ 109 110 void Flexpage::upgrade(flags_t flags) 111 { 112 if (flags && (flags != _flags)) 113 _flags |= flags; 114 } 115 116 /* Return a "send" flexpage for an access to 'offset' by positioning it relative 117 to 'hot_spot' for the receive flexpage window. */ 118 119 SendFlexpage Flexpage::to_send(offset_t offset, offset_t hot_spot, 120 flags_t flags, offset_t max_offset) 121 { 122 /* The dataspace offset of the flexpage base is a multiple of the flexpage 123 size. */ 124 125 offset_t receive_base_offset = trunc(offset, size); 126 127 /* The offset of the accessed page within the flexpage is constrained by the 128 current flexpage size. */ 129 130 offset_t page_offset = trunc(offset - receive_base_offset, PAGE_SIZE); 131 132 /* The receive flexpage offset (hot spot) must be constrained to the 133 flexpage, both the size and the start. */ 134 135 offset_t receive_size; 136 137 if (max_offset) 138 { 139 receive_size = trunc_multiple(max_offset - receive_base_offset, PAGE_SIZE); 140 receive_size = std::min(size, receive_size); 141 } 142 else 143 receive_size = size; 144 145 if (!receive_size) 146 return SendFlexpage(base_addr, page_order(0), flags); 147 148 offset_t receive_page_offset = hot_spot % receive_size; 149 150 while ((receive_size > PAGE_SIZE) && (receive_page_offset != page_offset)) 151 { 152 receive_size /= 2; 153 receive_page_offset = hot_spot % receive_size; 154 } 155 156 /* The flexpage base address is adjusted using the difference in page 157 offsets. Where the receive flexpage offset is constained further, the 158 base address will be raised to become closer to the accessed page. */ 159 160 offset_t adjustment = page_offset - receive_page_offset; 161 162 return SendFlexpage(base_addr + adjustment, page_order(receive_size), flags); 163 } 164 165 /* Return a representation of the flexpage for unmapping. */ 166 167 SendFlexpage Flexpage::to_unmap() 168 { 169 return SendFlexpage(base_addr, page_order(size), _flags); 170 } 171 172 // vim: tabstop=4 expandtab shiftwidth=4