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@89 | 13 | _memory = new MemoryPreallocated(memory, round(size, memory->region_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@67 | 21 | /* Detach one endpoint. */ |
paul@67 | 22 | |
paul@67 | 23 | void PipePaging::detach() |
paul@67 | 24 | { |
paul@67 | 25 | if (!_endpoints) |
paul@67 | 26 | return; |
paul@67 | 27 | else |
paul@67 | 28 | _endpoints--; |
paul@67 | 29 | |
paul@69 | 30 | /* Return if the other endpoint is attached. */ |
paul@69 | 31 | |
paul@67 | 32 | if (_endpoints) |
paul@67 | 33 | return; |
paul@67 | 34 | |
paul@67 | 35 | /* Discard all regions from the pipe. */ |
paul@67 | 36 | |
paul@72 | 37 | for (unsigned int i = 0; i < 2; i++) |
paul@67 | 38 | { |
paul@72 | 39 | PageMapper *mapper = _regions[i]; |
paul@67 | 40 | |
paul@72 | 41 | if (mapper != NULL) |
paul@72 | 42 | { |
paul@72 | 43 | mapper->detach(); |
paul@72 | 44 | _regions[i] = NULL; |
paul@72 | 45 | delete mapper; |
paul@72 | 46 | } |
paul@67 | 47 | } |
paul@70 | 48 | |
paul@72 | 49 | /* Delete the page collection and related objects. */ |
paul@70 | 50 | |
paul@70 | 51 | delete _pages; |
paul@72 | 52 | delete _queue; |
paul@72 | 53 | delete _memory; |
paul@67 | 54 | } |
paul@67 | 55 | |
paul@65 | 56 | /* Add a region to the sequence. */ |
paul@65 | 57 | |
paul@72 | 58 | PageMapper *PipePaging::add_region() |
paul@65 | 59 | { |
paul@72 | 60 | /* If the writer already accesses a different region to the reader, no new |
paul@72 | 61 | region is added. */ |
paul@72 | 62 | |
paul@72 | 63 | if (_writing != _reading) |
paul@72 | 64 | return NULL; |
paul@72 | 65 | |
paul@72 | 66 | /* Select the other region of the pair being maintained. */ |
paul@72 | 67 | |
paul@72 | 68 | _writing = 1 - _writing; |
paul@72 | 69 | |
paul@72 | 70 | /* Make a new mapper for the region. */ |
paul@72 | 71 | |
paul@72 | 72 | PageMapper *mapper = new PageMapper(&_accessors[_writing], _pages); |
paul@72 | 73 | |
paul@72 | 74 | /* Initialise and record the mapper. */ |
paul@72 | 75 | |
paul@72 | 76 | mapper->attach(); |
paul@72 | 77 | mapper->set_data_size(0); |
paul@72 | 78 | |
paul@72 | 79 | _regions[_writing] = mapper; |
paul@72 | 80 | return mapper; |
paul@65 | 81 | } |
paul@65 | 82 | |
paul@72 | 83 | /* Return the current region for reading. */ |
paul@65 | 84 | |
paul@72 | 85 | PageMapper *PipePaging::current_region() |
paul@65 | 86 | { |
paul@72 | 87 | return _regions[_reading]; |
paul@65 | 88 | } |
paul@65 | 89 | |
paul@72 | 90 | /* Return the next region for the reader if the writer is using a different one. |
paul@72 | 91 | Otherwise, return NULL. */ |
paul@65 | 92 | |
paul@65 | 93 | PageMapper *PipePaging::next_region() |
paul@65 | 94 | { |
paul@72 | 95 | /* If the reader already accesses the same region to the writer, no next |
paul@72 | 96 | region can be obtained. */ |
paul@66 | 97 | |
paul@72 | 98 | if (_reading == _writing) |
paul@72 | 99 | return NULL; |
paul@67 | 100 | |
paul@72 | 101 | /* Detach and discard the current page mapper. */ |
paul@72 | 102 | |
paul@72 | 103 | PageMapper *mapper = _regions[_reading]; |
paul@67 | 104 | |
paul@72 | 105 | if (mapper != NULL) |
paul@72 | 106 | { |
paul@72 | 107 | mapper->detach(); |
paul@72 | 108 | _regions[_reading] = NULL; |
paul@72 | 109 | delete mapper; |
paul@65 | 110 | } |
paul@65 | 111 | |
paul@72 | 112 | /* Return the next region. */ |
paul@66 | 113 | |
paul@72 | 114 | _reading = 1 - _reading; |
paul@72 | 115 | return _regions[_reading]; |
paul@66 | 116 | } |
paul@66 | 117 | |
paul@65 | 118 | // vim: tabstop=4 expandtab shiftwidth=4 |