paul@72 | 1 | #include "memory_incremental.h" |
paul@72 | 2 | #include "memory_preallocated.h" |
paul@72 | 3 | #include "page_queue_partitioned.h" |
paul@65 | 4 | #include "pipe_paging.h" |
paul@65 | 5 | |
paul@72 | 6 | |
paul@72 | 7 | |
paul@70 | 8 | PipePaging::PipePaging(Memory *memory, offset_t size) |
paul@70 | 9 | : _memory(NULL), _size(size) |
paul@70 | 10 | { |
paul@72 | 11 | /* Reserve space for two pipe regions. */ |
paul@72 | 12 | |
paul@72 | 13 | _memory = new MemoryPreallocated(memory, size * 2); |
paul@72 | 14 | _queue = new PageQueuePartitioned(); |
paul@72 | 15 | _pages = new Pages(_memory, _queue); |
paul@72 | 16 | |
paul@72 | 17 | for (unsigned int i = 0; i < 2; i++) |
paul@72 | 18 | _regions[i] = NULL; |
paul@70 | 19 | } |
paul@70 | 20 | |
paul@66 | 21 | PipePaging::PipePaging(offset_t size) |
paul@66 | 22 | : _size(size) |
paul@65 | 23 | { |
paul@72 | 24 | /* Reserve space for two pipe regions. */ |
paul@72 | 25 | |
paul@72 | 26 | _memory_base = new MemoryIncremental(); |
paul@72 | 27 | _memory = new MemoryPreallocated(_memory_base, size * 2); |
paul@72 | 28 | _queue = new PageQueuePartitioned(); |
paul@72 | 29 | _pages = new Pages(_memory, _queue); |
paul@72 | 30 | |
paul@72 | 31 | for (unsigned int i = 0; i < 2; i++) |
paul@72 | 32 | _regions[i] = NULL; |
paul@65 | 33 | } |
paul@65 | 34 | |
paul@67 | 35 | /* Detach one endpoint. */ |
paul@67 | 36 | |
paul@67 | 37 | void PipePaging::detach() |
paul@67 | 38 | { |
paul@67 | 39 | if (!_endpoints) |
paul@67 | 40 | return; |
paul@67 | 41 | else |
paul@67 | 42 | _endpoints--; |
paul@67 | 43 | |
paul@69 | 44 | /* Return if the other endpoint is attached. */ |
paul@69 | 45 | |
paul@67 | 46 | if (_endpoints) |
paul@67 | 47 | return; |
paul@67 | 48 | |
paul@67 | 49 | /* Discard all regions from the pipe. */ |
paul@67 | 50 | |
paul@72 | 51 | for (unsigned int i = 0; i < 2; i++) |
paul@67 | 52 | { |
paul@72 | 53 | PageMapper *mapper = _regions[i]; |
paul@67 | 54 | |
paul@72 | 55 | if (mapper != NULL) |
paul@72 | 56 | { |
paul@72 | 57 | mapper->detach(); |
paul@72 | 58 | _regions[i] = NULL; |
paul@72 | 59 | delete mapper; |
paul@72 | 60 | } |
paul@67 | 61 | } |
paul@70 | 62 | |
paul@72 | 63 | /* Delete the page collection and related objects. */ |
paul@70 | 64 | |
paul@70 | 65 | delete _pages; |
paul@72 | 66 | delete _queue; |
paul@72 | 67 | delete _memory; |
paul@70 | 68 | |
paul@72 | 69 | /* Delete any underlying memory object. */ |
paul@72 | 70 | |
paul@72 | 71 | if (_memory_base != NULL) |
paul@72 | 72 | delete _memory_base; |
paul@67 | 73 | } |
paul@67 | 74 | |
paul@65 | 75 | /* Add a region to the sequence. */ |
paul@65 | 76 | |
paul@72 | 77 | PageMapper *PipePaging::add_region() |
paul@65 | 78 | { |
paul@72 | 79 | /* If the writer already accesses a different region to the reader, no new |
paul@72 | 80 | region is added. */ |
paul@72 | 81 | |
paul@72 | 82 | if (_writing != _reading) |
paul@72 | 83 | return NULL; |
paul@72 | 84 | |
paul@72 | 85 | /* Select the other region of the pair being maintained. */ |
paul@72 | 86 | |
paul@72 | 87 | _writing = 1 - _writing; |
paul@72 | 88 | |
paul@72 | 89 | /* Make a new mapper for the region. */ |
paul@72 | 90 | |
paul@72 | 91 | PageMapper *mapper = new PageMapper(&_accessors[_writing], _pages); |
paul@72 | 92 | |
paul@72 | 93 | /* Initialise and record the mapper. */ |
paul@72 | 94 | |
paul@72 | 95 | mapper->attach(); |
paul@72 | 96 | mapper->set_data_size(0); |
paul@72 | 97 | |
paul@72 | 98 | _regions[_writing] = mapper; |
paul@72 | 99 | return mapper; |
paul@65 | 100 | } |
paul@65 | 101 | |
paul@72 | 102 | /* Return the current region for reading. */ |
paul@65 | 103 | |
paul@72 | 104 | PageMapper *PipePaging::current_region() |
paul@65 | 105 | { |
paul@72 | 106 | return _regions[_reading]; |
paul@65 | 107 | } |
paul@65 | 108 | |
paul@72 | 109 | /* Return the next region for the reader if the writer is using a different one. |
paul@72 | 110 | Otherwise, return NULL. */ |
paul@65 | 111 | |
paul@65 | 112 | PageMapper *PipePaging::next_region() |
paul@65 | 113 | { |
paul@72 | 114 | /* If the reader already accesses the same region to the writer, no next |
paul@72 | 115 | region can be obtained. */ |
paul@66 | 116 | |
paul@72 | 117 | if (_reading == _writing) |
paul@72 | 118 | return NULL; |
paul@67 | 119 | |
paul@72 | 120 | /* Detach and discard the current page mapper. */ |
paul@72 | 121 | |
paul@72 | 122 | PageMapper *mapper = _regions[_reading]; |
paul@67 | 123 | |
paul@72 | 124 | if (mapper != NULL) |
paul@72 | 125 | { |
paul@72 | 126 | mapper->detach(); |
paul@72 | 127 | _regions[_reading] = NULL; |
paul@72 | 128 | delete mapper; |
paul@65 | 129 | } |
paul@65 | 130 | |
paul@72 | 131 | /* Return the next region. */ |
paul@66 | 132 | |
paul@72 | 133 | _reading = 1 - _reading; |
paul@72 | 134 | return _regions[_reading]; |
paul@66 | 135 | } |
paul@66 | 136 | |
paul@65 | 137 | // vim: tabstop=4 expandtab shiftwidth=4 |