paul@93 | 1 | /* |
paul@93 | 2 | * Memory region abstractions. |
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@0 | 22 | #include <string.h> |
paul@0 | 23 | #include <stdlib.h> |
paul@0 | 24 | |
paul@0 | 25 | #include "region.h" |
paul@0 | 26 | |
paul@0 | 27 | |
paul@0 | 28 | |
paul@0 | 29 | /* Initialise region state, indicating the size, file and position. */ |
paul@0 | 30 | |
paul@0 | 31 | RegionState::RegionState(unsigned long size, fileid_t fileid, offset_t filepos) |
paul@0 | 32 | : size(size), fileid(fileid), filepos(filepos) |
paul@0 | 33 | { |
paul@0 | 34 | } |
paul@0 | 35 | |
paul@0 | 36 | void RegionState::fill(fileid_t fileid, offset_t filepos) |
paul@0 | 37 | { |
paul@0 | 38 | this->fileid = fileid; |
paul@0 | 39 | this->filepos = filepos; |
paul@0 | 40 | } |
paul@0 | 41 | |
paul@0 | 42 | |
paul@0 | 43 | |
paul@0 | 44 | /* Initialise a region having the given 'start' and 'end' addresses, with the |
paul@0 | 45 | 'end' being one location beyond the last address in the region. */ |
paul@0 | 46 | |
paul@0 | 47 | Region::Region(offset_t start, offset_t end) |
paul@0 | 48 | : start(start), end(end), state(end - start) |
paul@0 | 49 | { |
paul@0 | 50 | /* Content state. */ |
paul@0 | 51 | |
paul@0 | 52 | memset((void *) start, 0, end - start); |
paul@0 | 53 | } |
paul@0 | 54 | |
paul@0 | 55 | Region::~Region() |
paul@0 | 56 | { |
paul@0 | 57 | free((void *) start); |
paul@0 | 58 | } |
paul@0 | 59 | |
paul@0 | 60 | offset_t Region::size() |
paul@0 | 61 | { |
paul@0 | 62 | return end - start; |
paul@0 | 63 | } |
paul@0 | 64 | |
paul@0 | 65 | /* Debugging methods. */ |
paul@0 | 66 | |
paul@0 | 67 | int Region::compare(Region *other) |
paul@0 | 68 | { |
paul@0 | 69 | if (start < other->start) |
paul@0 | 70 | return -1; |
paul@0 | 71 | else if (start > other->start) |
paul@0 | 72 | return 1; |
paul@0 | 73 | else |
paul@0 | 74 | return 0; |
paul@0 | 75 | } |
paul@0 | 76 | |
paul@0 | 77 | void Region::fill(fileid_t fileid, offset_t filepos) |
paul@0 | 78 | { |
paul@0 | 79 | state.fill(fileid, filepos); |
paul@0 | 80 | } |
paul@0 | 81 | |
paul@0 | 82 | void Region::flush() |
paul@0 | 83 | { |
paul@0 | 84 | } |
paul@0 | 85 | |
paul@0 | 86 | /* Simulation methods. */ |
paul@0 | 87 | |
paul@0 | 88 | char *Region::read(offset_t offset) |
paul@0 | 89 | { |
paul@32 | 90 | if (offset < size()) |
paul@32 | 91 | return (char *) start + offset; |
paul@32 | 92 | else |
paul@32 | 93 | return NULL; |
paul@0 | 94 | } |
paul@0 | 95 | |
paul@0 | 96 | void Region::write(const char *data, offset_t offset) |
paul@0 | 97 | { |
paul@0 | 98 | size_t length = strlen(data); |
paul@0 | 99 | |
paul@0 | 100 | if (offset + length < size()) |
paul@0 | 101 | memcpy((void *) (start + offset), data, length + 1); |
paul@0 | 102 | } |
paul@0 | 103 | |
paul@0 | 104 | // vim: tabstop=4 expandtab shiftwidth=4 |