Landfall

Annotated pkg/landfall-examples/msc_block_server/msc_region_accessor.cc

273:8f6ff113d000
8 months ago Paul Boddie Removed debugging output. cpm-library-improvements
paul@272 1
/*
paul@272 2
 * A memory card region accessor.
paul@272 3
 *
paul@272 4
 * Copyright (C) 2021, 2024 Paul Boddie <paul@boddie.org.uk>
paul@272 5
 *
paul@272 6
 * This program is free software; you can redistribute it and/or
paul@272 7
 * modify it under the terms of the GNU General Public License as
paul@272 8
 * published by the Free Software Foundation; either version 2 of
paul@272 9
 * the License, or (at your option) any later version.
paul@272 10
 *
paul@272 11
 * This program is distributed in the hope that it will be useful,
paul@272 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@272 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@272 14
 * GNU General Public License for more details.
paul@272 15
 *
paul@272 16
 * You should have received a copy of the GNU General Public License
paul@272 17
 * along with this program; if not, write to the Free Software
paul@272 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@272 19
 * Boston, MA  02110-1301, USA
paul@272 20
 */
paul@272 21
paul@272 22
#include <string.h>
paul@272 23
paul@272 24
#include <algorithm>
paul@272 25
paul@272 26
#include "msc_region_accessor.h"
paul@272 27
paul@272 28
paul@272 29
paul@272 30
MscRegionAccessor::MscRegionAccessor(MscRegionOperations *ops,
paul@272 31
                                     uint32_t start_block, uint32_t num_blocks,
paul@272 32
                                     fileid_t fileid)
paul@272 33
: Accessor(fileid), _ops(ops)
paul@272 34
{
paul@272 35
    // NOTE: Block size assumption employed.
paul@272 36
paul@272 37
    _start = start_block * 512;
paul@272 38
    _size = num_blocks * 512;
paul@272 39
}
paul@272 40
paul@272 41
/* Return the size of the file. */
paul@272 42
paul@272 43
offset_t MscRegionAccessor::get_size()
paul@272 44
{
paul@272 45
    return _size;
paul@272 46
}
paul@272 47
paul@272 48
/* Update the size of the file. */
paul@272 49
paul@272 50
void MscRegionAccessor::set_size(offset_t size)
paul@272 51
{
paul@272 52
    (void) size;
paul@272 53
}
paul@272 54
paul@272 55
/* Data transfer helper methods. */
paul@272 56
paul@272 57
/* Fill the populated portion of a flexpage. */
paul@272 58
paul@272 59
void MscRegionAccessor::fill_populated(Flexpage *flexpage)
paul@272 60
{
paul@272 61
    offset_t filepos = flexpage->base_offset;
paul@272 62
    offset_t populated_size = std::min(flexpage->size, _size - filepos);
paul@272 63
paul@272 64
    /* Tag the region with file state. */
paul@272 65
paul@272 66
    flexpage->region->fill(fileid, filepos);
paul@272 67
paul@272 68
    /* Fill the region with file content. */
paul@272 69
paul@272 70
    _ops->read(flexpage->base_addr, flexpage->base_addr_physical,
paul@272 71
               _start + filepos, populated_size);
paul@272 72
paul@272 73
    /* Pad the flexpage with zero. */
paul@272 74
paul@272 75
    if (populated_size < flexpage->size)
paul@272 76
        memset((void *) (flexpage->base_addr + populated_size), 0,
paul@272 77
               flexpage->size - populated_size);
paul@272 78
}
paul@272 79
paul@272 80
/* Flush the populated portion of a flexpage. */
paul@272 81
paul@272 82
void MscRegionAccessor::flush_populated(Flexpage *flexpage)
paul@272 83
{
paul@272 84
    offset_t filepos = flexpage->base_offset;
paul@272 85
    offset_t populated_size = std::min(flexpage->size, _size - filepos);
paul@272 86
paul@272 87
    /* Remove the file state tag from the region. */
paul@272 88
paul@272 89
    flexpage->region->flush();
paul@272 90
paul@272 91
    /* Flush the file content. */
paul@272 92
paul@272 93
    _ops->write(flexpage->base_addr, flexpage->base_addr_physical,
paul@272 94
                _start + filepos, populated_size);
paul@272 95
}
paul@272 96
paul@272 97
// vim: tabstop=4 expandtab shiftwidth=4