1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libmem/lib/src/region.cc Thu Apr 15 23:15:17 2021 +0200
1.3 @@ -0,0 +1,104 @@
1.4 +/*
1.5 + * Memory region abstractions.
1.6 + *
1.7 + * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <string.h>
1.26 +#include <stdlib.h>
1.27 +
1.28 +#include "region.h"
1.29 +
1.30 +
1.31 +
1.32 +/* Initialise region state, indicating the size, file and position. */
1.33 +
1.34 +RegionState::RegionState(unsigned long size, fileid_t fileid, offset_t filepos)
1.35 +: size(size), fileid(fileid), filepos(filepos)
1.36 +{
1.37 +}
1.38 +
1.39 +void RegionState::fill(fileid_t fileid, offset_t filepos)
1.40 +{
1.41 + this->fileid = fileid;
1.42 + this->filepos = filepos;
1.43 +}
1.44 +
1.45 +
1.46 +
1.47 +/* Initialise a region having the given 'start' and 'end' addresses, with the
1.48 + 'end' being one location beyond the last address in the region. */
1.49 +
1.50 +Region::Region(offset_t start, offset_t end)
1.51 +: start(start), end(end), state(end - start)
1.52 +{
1.53 + /* Content state. */
1.54 +
1.55 + memset((void *) start, 0, end - start);
1.56 +}
1.57 +
1.58 +Region::~Region()
1.59 +{
1.60 + free((void *) start);
1.61 +}
1.62 +
1.63 +offset_t Region::size()
1.64 +{
1.65 + return end - start;
1.66 +}
1.67 +
1.68 +/* Debugging methods. */
1.69 +
1.70 +int Region::compare(Region *other)
1.71 +{
1.72 + if (start < other->start)
1.73 + return -1;
1.74 + else if (start > other->start)
1.75 + return 1;
1.76 + else
1.77 + return 0;
1.78 +}
1.79 +
1.80 +void Region::fill(fileid_t fileid, offset_t filepos)
1.81 +{
1.82 + state.fill(fileid, filepos);
1.83 +}
1.84 +
1.85 +void Region::flush()
1.86 +{
1.87 +}
1.88 +
1.89 +/* Simulation methods. */
1.90 +
1.91 +char *Region::read(offset_t offset)
1.92 +{
1.93 + if (offset < size())
1.94 + return (char *) start + offset;
1.95 + else
1.96 + return NULL;
1.97 +}
1.98 +
1.99 +void Region::write(const char *data, offset_t offset)
1.100 +{
1.101 + size_t length = strlen(data);
1.102 +
1.103 + if (offset + length < size())
1.104 + memcpy((void *) (start + offset), data, length + 1);
1.105 +}
1.106 +
1.107 +// vim: tabstop=4 expandtab shiftwidth=4