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