1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/pipes/pipe_paging.cc Sat Mar 27 01:42:25 2021 +0100
1.3 @@ -0,0 +1,101 @@
1.4 +#include "pipe_paging.h"
1.5 +
1.6 +PipePaging::PipePaging(Memory *memory, offset_t size)
1.7 +: _memory(NULL), _size(size)
1.8 +{
1.9 + _pages = new PagesConserving(memory);
1.10 +}
1.11 +
1.12 +PipePaging::PipePaging(offset_t size)
1.13 +: _size(size)
1.14 +{
1.15 + _memory = new Memory();
1.16 + _pages = new PagesConserving(_memory);
1.17 +}
1.18 +
1.19 +/* Detach one endpoint. */
1.20 +
1.21 +void PipePaging::detach()
1.22 +{
1.23 + if (!_endpoints)
1.24 + return;
1.25 + else
1.26 + _endpoints--;
1.27 +
1.28 + /* Return if the other endpoint is attached. */
1.29 +
1.30 + if (_endpoints)
1.31 + return;
1.32 +
1.33 + /* Discard all regions from the pipe. */
1.34 +
1.35 + while (!_regions.empty())
1.36 + {
1.37 + PageMapper *mapper = _regions.front();
1.38 +
1.39 + _regions.pop_front();
1.40 + mapper->detach();
1.41 + delete mapper;
1.42 + }
1.43 +
1.44 + /* Delete the page collection and memory object. */
1.45 +
1.46 + delete _pages;
1.47 +
1.48 + if (_memory != NULL)
1.49 + delete _memory;
1.50 +}
1.51 +
1.52 +/* Add a region to the sequence. */
1.53 +
1.54 +void PipePaging::add_region(PageMapper *mapper)
1.55 +{
1.56 + _regions.push_back(mapper);
1.57 +}
1.58 +
1.59 +/* Return the first region in the sequence. */
1.60 +
1.61 +PageMapper *PipePaging::first_region()
1.62 +{
1.63 + return _regions.front();
1.64 +}
1.65 +
1.66 +/* Return the next region for the reader. If only a single region remains, with
1.67 + the reader wishing to move to the next, return NULL. */
1.68 +
1.69 +PageMapper *PipePaging::next_region()
1.70 +{
1.71 + if (_regions.size() > 1)
1.72 + {
1.73 + /* Discard the accessor from the current region. */
1.74 +
1.75 + if (_accessors.size() > _regions.size())
1.76 + _accessors.pop_front();
1.77 +
1.78 + /* Detach and discard the current page mapper. */
1.79 +
1.80 + PageMapper *mapper = _regions.front();
1.81 +
1.82 + _regions.pop_front();
1.83 + mapper->detach();
1.84 + delete mapper;
1.85 +
1.86 + /* Return the next region. */
1.87 +
1.88 + return _regions.front();
1.89 + }
1.90 + else
1.91 + return NULL;
1.92 +}
1.93 +
1.94 +/* Initialise an accessor for a region. */
1.95 +
1.96 +PipeAccessor *PipePaging::accessor()
1.97 +{
1.98 + PipeAccessor accessor;
1.99 +
1.100 + _accessors.push_back(accessor);
1.101 + return &_accessors.back();
1.102 +}
1.103 +
1.104 +// vim: tabstop=4 expandtab shiftwidth=4