paul@93 | 1 | /* |
paul@93 | 2 | * A pipe paging coordinator, permitting memory sharing pipe endpoints. |
paul@93 | 3 | * |
paul@93 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@93 | 5 | * |
paul@93 | 6 | * This program is free software; you can redistribute it and/or |
paul@93 | 7 | * modify it under the terms of the GNU General Public License as |
paul@93 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@93 | 9 | * the License, or (at your option) any later version. |
paul@93 | 10 | * |
paul@93 | 11 | * This program is distributed in the hope that it will be useful, |
paul@93 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@93 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@93 | 14 | * GNU General Public License for more details. |
paul@93 | 15 | * |
paul@93 | 16 | * You should have received a copy of the GNU General Public License |
paul@93 | 17 | * along with this program; if not, write to the Free Software |
paul@93 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@93 | 19 | * Boston, MA 02110-1301, USA |
paul@93 | 20 | */ |
paul@93 | 21 | |
paul@94 | 22 | #include <mem/memory_incremental.h> |
paul@94 | 23 | #include <mem/memory_preallocated.h> |
paul@94 | 24 | |
paul@72 | 25 | #include "page_queue_partitioned.h" |
paul@65 | 26 | #include "pipe_paging.h" |
paul@65 | 27 | |
paul@72 | 28 | |
paul@72 | 29 | |
paul@70 | 30 | PipePaging::PipePaging(Memory *memory, offset_t size) |
paul@70 | 31 | : _memory(NULL), _size(size) |
paul@70 | 32 | { |
paul@72 | 33 | /* Reserve space for two pipe regions. */ |
paul@72 | 34 | |
paul@89 | 35 | _memory = new MemoryPreallocated(memory, round(size, memory->region_size()) * 2); |
paul@72 | 36 | _queue = new PageQueuePartitioned(); |
paul@72 | 37 | _pages = new Pages(_memory, _queue); |
paul@72 | 38 | |
paul@114 | 39 | /* Reset the mappers associated with the regions. */ |
paul@114 | 40 | |
paul@72 | 41 | for (unsigned int i = 0; i < 2; i++) |
paul@72 | 42 | _regions[i] = NULL; |
paul@70 | 43 | } |
paul@70 | 44 | |
paul@114 | 45 | /* Return whether one or more endpoints have detached. */ |
paul@114 | 46 | |
paul@114 | 47 | int PipePaging::closed() |
paul@114 | 48 | { |
paul@114 | 49 | return _endpoints < 2; |
paul@114 | 50 | } |
paul@114 | 51 | |
paul@67 | 52 | /* Detach one endpoint. */ |
paul@67 | 53 | |
paul@67 | 54 | void PipePaging::detach() |
paul@67 | 55 | { |
paul@67 | 56 | if (!_endpoints) |
paul@67 | 57 | return; |
paul@67 | 58 | else |
paul@67 | 59 | _endpoints--; |
paul@67 | 60 | |
paul@69 | 61 | /* Return if the other endpoint is attached. */ |
paul@69 | 62 | |
paul@67 | 63 | if (_endpoints) |
paul@67 | 64 | return; |
paul@67 | 65 | |
paul@67 | 66 | /* Discard all regions from the pipe. */ |
paul@67 | 67 | |
paul@72 | 68 | for (unsigned int i = 0; i < 2; i++) |
paul@67 | 69 | { |
paul@72 | 70 | PageMapper *mapper = _regions[i]; |
paul@67 | 71 | |
paul@72 | 72 | if (mapper != NULL) |
paul@72 | 73 | { |
paul@72 | 74 | mapper->detach(); |
paul@72 | 75 | _regions[i] = NULL; |
paul@72 | 76 | delete mapper; |
paul@72 | 77 | } |
paul@67 | 78 | } |
paul@70 | 79 | |
paul@72 | 80 | /* Delete the page collection and related objects. */ |
paul@70 | 81 | |
paul@70 | 82 | delete _pages; |
paul@72 | 83 | delete _queue; |
paul@72 | 84 | delete _memory; |
paul@67 | 85 | } |
paul@67 | 86 | |
paul@65 | 87 | /* Add a region to the sequence. */ |
paul@65 | 88 | |
paul@72 | 89 | PageMapper *PipePaging::add_region() |
paul@65 | 90 | { |
paul@72 | 91 | /* If the writer already accesses a different region to the reader, no new |
paul@72 | 92 | region is added. */ |
paul@72 | 93 | |
paul@72 | 94 | if (_writing != _reading) |
paul@72 | 95 | return NULL; |
paul@72 | 96 | |
paul@72 | 97 | /* Select the other region of the pair being maintained. */ |
paul@72 | 98 | |
paul@72 | 99 | _writing = 1 - _writing; |
paul@72 | 100 | |
paul@72 | 101 | /* Make a new mapper for the region. */ |
paul@72 | 102 | |
paul@72 | 103 | PageMapper *mapper = new PageMapper(&_accessors[_writing], _pages); |
paul@72 | 104 | |
paul@72 | 105 | /* Initialise and record the mapper. */ |
paul@72 | 106 | |
paul@72 | 107 | mapper->attach(); |
paul@72 | 108 | mapper->set_data_size(0); |
paul@72 | 109 | |
paul@72 | 110 | _regions[_writing] = mapper; |
paul@72 | 111 | return mapper; |
paul@65 | 112 | } |
paul@65 | 113 | |
paul@72 | 114 | /* Return the current region for reading. */ |
paul@65 | 115 | |
paul@72 | 116 | PageMapper *PipePaging::current_region() |
paul@65 | 117 | { |
paul@72 | 118 | return _regions[_reading]; |
paul@65 | 119 | } |
paul@65 | 120 | |
paul@72 | 121 | /* Return the next region for the reader if the writer is using a different one. |
paul@72 | 122 | Otherwise, return NULL. */ |
paul@65 | 123 | |
paul@65 | 124 | PageMapper *PipePaging::next_region() |
paul@65 | 125 | { |
paul@72 | 126 | /* If the reader already accesses the same region to the writer, no next |
paul@72 | 127 | region can be obtained. */ |
paul@66 | 128 | |
paul@72 | 129 | if (_reading == _writing) |
paul@72 | 130 | return NULL; |
paul@67 | 131 | |
paul@72 | 132 | /* Detach and discard the current page mapper. */ |
paul@72 | 133 | |
paul@72 | 134 | PageMapper *mapper = _regions[_reading]; |
paul@67 | 135 | |
paul@72 | 136 | if (mapper != NULL) |
paul@72 | 137 | { |
paul@72 | 138 | mapper->detach(); |
paul@72 | 139 | _regions[_reading] = NULL; |
paul@72 | 140 | delete mapper; |
paul@65 | 141 | } |
paul@65 | 142 | |
paul@114 | 143 | /* Select the next region. */ |
paul@66 | 144 | |
paul@72 | 145 | _reading = 1 - _reading; |
paul@114 | 146 | |
paul@114 | 147 | /* Return the next region's mapper. */ |
paul@114 | 148 | |
paul@72 | 149 | return _regions[_reading]; |
paul@66 | 150 | } |
paul@66 | 151 | |
paul@65 | 152 | // vim: tabstop=4 expandtab shiftwidth=4 |