1.1 --- a/pipes/pipe_paging.cc Sat Mar 27 01:42:25 2021 +0100
1.2 +++ b/pipes/pipe_paging.cc Sat Mar 27 22:12:12 2021 +0100
1.3 @@ -1,16 +1,35 @@
1.4 +#include "memory_incremental.h"
1.5 +#include "memory_preallocated.h"
1.6 +#include "page_queue_partitioned.h"
1.7 #include "pipe_paging.h"
1.8
1.9 +
1.10 +
1.11 PipePaging::PipePaging(Memory *memory, offset_t size)
1.12 : _memory(NULL), _size(size)
1.13 {
1.14 - _pages = new PagesConserving(memory);
1.15 + /* Reserve space for two pipe regions. */
1.16 +
1.17 + _memory = new MemoryPreallocated(memory, size * 2);
1.18 + _queue = new PageQueuePartitioned();
1.19 + _pages = new Pages(_memory, _queue);
1.20 +
1.21 + for (unsigned int i = 0; i < 2; i++)
1.22 + _regions[i] = NULL;
1.23 }
1.24
1.25 PipePaging::PipePaging(offset_t size)
1.26 : _size(size)
1.27 {
1.28 - _memory = new Memory();
1.29 - _pages = new PagesConserving(_memory);
1.30 + /* Reserve space for two pipe regions. */
1.31 +
1.32 + _memory_base = new MemoryIncremental();
1.33 + _memory = new MemoryPreallocated(_memory_base, size * 2);
1.34 + _queue = new PageQueuePartitioned();
1.35 + _pages = new Pages(_memory, _queue);
1.36 +
1.37 + for (unsigned int i = 0; i < 2; i++)
1.38 + _regions[i] = NULL;
1.39 }
1.40
1.41 /* Detach one endpoint. */
1.42 @@ -29,73 +48,90 @@
1.43
1.44 /* Discard all regions from the pipe. */
1.45
1.46 - while (!_regions.empty())
1.47 + for (unsigned int i = 0; i < 2; i++)
1.48 {
1.49 - PageMapper *mapper = _regions.front();
1.50 + PageMapper *mapper = _regions[i];
1.51
1.52 - _regions.pop_front();
1.53 - mapper->detach();
1.54 - delete mapper;
1.55 + if (mapper != NULL)
1.56 + {
1.57 + mapper->detach();
1.58 + _regions[i] = NULL;
1.59 + delete mapper;
1.60 + }
1.61 }
1.62
1.63 - /* Delete the page collection and memory object. */
1.64 + /* Delete the page collection and related objects. */
1.65
1.66 delete _pages;
1.67 + delete _queue;
1.68 + delete _memory;
1.69
1.70 - if (_memory != NULL)
1.71 - delete _memory;
1.72 + /* Delete any underlying memory object. */
1.73 +
1.74 + if (_memory_base != NULL)
1.75 + delete _memory_base;
1.76 }
1.77
1.78 /* Add a region to the sequence. */
1.79
1.80 -void PipePaging::add_region(PageMapper *mapper)
1.81 +PageMapper *PipePaging::add_region()
1.82 {
1.83 - _regions.push_back(mapper);
1.84 + /* If the writer already accesses a different region to the reader, no new
1.85 + region is added. */
1.86 +
1.87 + if (_writing != _reading)
1.88 + return NULL;
1.89 +
1.90 + /* Select the other region of the pair being maintained. */
1.91 +
1.92 + _writing = 1 - _writing;
1.93 +
1.94 + /* Make a new mapper for the region. */
1.95 +
1.96 + PageMapper *mapper = new PageMapper(&_accessors[_writing], _pages);
1.97 +
1.98 + /* Initialise and record the mapper. */
1.99 +
1.100 + mapper->attach();
1.101 + mapper->set_data_size(0);
1.102 +
1.103 + _regions[_writing] = mapper;
1.104 + return mapper;
1.105 }
1.106
1.107 -/* Return the first region in the sequence. */
1.108 +/* Return the current region for reading. */
1.109
1.110 -PageMapper *PipePaging::first_region()
1.111 +PageMapper *PipePaging::current_region()
1.112 {
1.113 - return _regions.front();
1.114 + return _regions[_reading];
1.115 }
1.116
1.117 -/* Return the next region for the reader. If only a single region remains, with
1.118 - the reader wishing to move to the next, return NULL. */
1.119 +/* Return the next region for the reader if the writer is using a different one.
1.120 + Otherwise, return NULL. */
1.121
1.122 PageMapper *PipePaging::next_region()
1.123 {
1.124 - if (_regions.size() > 1)
1.125 - {
1.126 - /* Discard the accessor from the current region. */
1.127 -
1.128 - if (_accessors.size() > _regions.size())
1.129 - _accessors.pop_front();
1.130 + /* If the reader already accesses the same region to the writer, no next
1.131 + region can be obtained. */
1.132
1.133 - /* Detach and discard the current page mapper. */
1.134 -
1.135 - PageMapper *mapper = _regions.front();
1.136 + if (_reading == _writing)
1.137 + return NULL;
1.138
1.139 - _regions.pop_front();
1.140 - mapper->detach();
1.141 - delete mapper;
1.142 + /* Detach and discard the current page mapper. */
1.143 +
1.144 + PageMapper *mapper = _regions[_reading];
1.145
1.146 - /* Return the next region. */
1.147 -
1.148 - return _regions.front();
1.149 + if (mapper != NULL)
1.150 + {
1.151 + mapper->detach();
1.152 + _regions[_reading] = NULL;
1.153 + delete mapper;
1.154 }
1.155 - else
1.156 - return NULL;
1.157 -}
1.158
1.159 -/* Initialise an accessor for a region. */
1.160 + /* Return the next region. */
1.161
1.162 -PipeAccessor *PipePaging::accessor()
1.163 -{
1.164 - PipeAccessor accessor;
1.165 -
1.166 - _accessors.push_back(accessor);
1.167 - return &_accessors.back();
1.168 + _reading = 1 - _reading;
1.169 + return _regions[_reading];
1.170 }
1.171
1.172 // vim: tabstop=4 expandtab shiftwidth=4