1 #include <algorithm> 2 #include <stdio.h> 3 4 #include "flexpage.h" 5 6 7 8 /* Reset the flexpage using 'offset', being the file offset. */ 9 10 void Flexpage::reset(offset_t offset) 11 { 12 _counter = 0; 13 14 /* By definition (see "Flexible-Sized Page Objects - Object-Orientation 15 in Operation Systems"), flexpages are aligned to multiples of their 16 size. 17 18 The size of the flexpage depends on the amount of space around the 19 accessed page. It cannot exceed the size of the memory region. */ 20 21 size = max_multiple(region->start, region->end, PAGE_SIZE); 22 23 /* The base address of the flexpage is computed from the region start 24 and flexpage size. It will be no lower than the region start. 25 26 Sent flexpages may use higher bases due to receive window constraints, 27 these being communicated by the "hot spot". */ 28 29 base_addr = round(region->start, size); 30 31 /* Get the file offset for the base of the flexpage. This will be a 32 multiple of the flexpage size for alignment purposes. */ 33 34 base_offset = trunc(offset, size); 35 36 /* The page being accessed is relative to the base. 37 (This is transient information recording the initialising access 38 details.) */ 39 40 page_offset = trunc(offset - base_offset, PAGE_SIZE); 41 page_addr = base_addr + page_offset; 42 } 43 44 bool Flexpage::decrement() 45 { 46 if (_counter) 47 { 48 _counter--; 49 return _counter == 0; 50 } 51 else 52 return 0; 53 } 54 55 void Flexpage::increment() 56 { 57 _counter++; 58 } 59 60 void Flexpage::invalidate() 61 { 62 _counter = 0; 63 } 64 65 bool Flexpage::valid() 66 { 67 return _counter != 0; 68 } 69 70 /* Return whether the flexpage supports the given file 'position'. */ 71 72 bool Flexpage::supports_position(offset_t position) 73 { 74 return (base_offset <= position) && (position < (base_offset + size)); 75 } 76 77 /* Return a "send" flexpage for an access to 'offset' by positioning it relative 78 to 'hot_spot' for the receive flexpage window. */ 79 80 SendFlexpage Flexpage::to_send(offset_t offset, offset_t hot_spot, offset_t max_offset) 81 { 82 /* The dataspace offset of the flexpage base is a multiple of the flexpage 83 size. */ 84 85 offset_t receive_base_offset = trunc(offset, size); 86 87 /* The offset of the accessed page within the flexpage is constrained by the 88 current flexpage size. */ 89 90 offset_t page_offset = trunc(offset - receive_base_offset, PAGE_SIZE); 91 92 /* The receive flexpage offset (hot spot) must be constrained to the 93 flexpage, both the size and the start. */ 94 95 offset_t receive_size; 96 97 if (max_offset) 98 receive_size = std::min(size, max_offset - receive_base_offset); 99 else 100 receive_size = size; 101 102 offset_t receive_page_offset = hot_spot % receive_size; 103 104 while ((receive_size > PAGE_SIZE) && (receive_page_offset != page_offset)) 105 { 106 receive_size /= 2; 107 receive_page_offset = hot_spot % receive_size; 108 } 109 110 /* The flexpage base address is adjusted using the difference in page 111 offsets. Where the receive flexpage offset is constained further, the 112 base address will be raised to become closer to the accessed page. */ 113 114 offset_t adjustment = page_offset - receive_page_offset; 115 116 return SendFlexpage(base_addr + adjustment, page_order(receive_size)); 117 } 118 119 /* Return a representation of the flexpage for unmapping. */ 120 121 SendFlexpage Flexpage::to_unmap() 122 { 123 return SendFlexpage(base_addr, page_order(size)); 124 } 125 126 /* Debugging methods. */ 127 128 void Flexpage::show(std::ostringstream &buffer) 129 { 130 char buf[256]; 131 132 snprintf(buf, 256, "Flexpage(base_offset=0x%lx/%ld, " 133 "base_addr=0x%lx, size=0x%lx/%ld)", 134 base_offset, base_offset, base_addr, 135 size, size); 136 137 buffer << buf; 138 } 139 140 // vim: tabstop=4 expandtab shiftwidth=4