1 #include "pipe_paging.h" 2 3 PipePaging::PipePaging(Memory *memory, offset_t size) 4 : _memory(NULL), _size(size) 5 { 6 _pages = new PagesConserving(memory); 7 } 8 9 PipePaging::PipePaging(offset_t size) 10 : _size(size) 11 { 12 _memory = new Memory(); 13 _pages = new PagesConserving(_memory); 14 } 15 16 /* Detach one endpoint. */ 17 18 void PipePaging::detach() 19 { 20 if (!_endpoints) 21 return; 22 else 23 _endpoints--; 24 25 /* Return if the other endpoint is attached. */ 26 27 if (_endpoints) 28 return; 29 30 /* Discard all regions from the pipe. */ 31 32 while (!_regions.empty()) 33 { 34 PageMapper *mapper = _regions.front(); 35 36 _regions.pop_front(); 37 mapper->detach(); 38 delete mapper; 39 } 40 41 /* Delete the page collection and memory object. */ 42 43 delete _pages; 44 45 if (_memory != NULL) 46 delete _memory; 47 } 48 49 /* Add a region to the sequence. */ 50 51 void PipePaging::add_region(PageMapper *mapper) 52 { 53 _regions.push_back(mapper); 54 } 55 56 /* Return the first region in the sequence. */ 57 58 PageMapper *PipePaging::first_region() 59 { 60 return _regions.front(); 61 } 62 63 /* Return the next region for the reader. If only a single region remains, with 64 the reader wishing to move to the next, return NULL. */ 65 66 PageMapper *PipePaging::next_region() 67 { 68 if (_regions.size() > 1) 69 { 70 /* Discard the accessor from the current region. */ 71 72 if (_accessors.size() > _regions.size()) 73 _accessors.pop_front(); 74 75 /* Detach and discard the current page mapper. */ 76 77 PageMapper *mapper = _regions.front(); 78 79 _regions.pop_front(); 80 mapper->detach(); 81 delete mapper; 82 83 /* Return the next region. */ 84 85 return _regions.front(); 86 } 87 else 88 return NULL; 89 } 90 91 /* Initialise an accessor for a region. */ 92 93 PipeAccessor *PipePaging::accessor() 94 { 95 PipeAccessor accessor; 96 97 _accessors.push_back(accessor); 98 return &_accessors.back(); 99 } 100 101 // vim: tabstop=4 expandtab shiftwidth=4