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@109 | 31 | RegionState::RegionState(fileid_t fileid, offset_t filepos) |
paul@109 | 32 | : 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@109 | 42 | void RegionState::flush() |
paul@109 | 43 | { |
paul@109 | 44 | fileid = FILEID_INVALID; |
paul@109 | 45 | filepos = 0; |
paul@109 | 46 | } |
paul@109 | 47 | |
paul@0 | 48 | |
paul@0 | 49 | |
paul@0 | 50 | /* Initialise a region having the given 'start' and 'end' addresses, with the |
paul@0 | 51 | 'end' being one location beyond the last address in the region. */ |
paul@0 | 52 | |
paul@0 | 53 | Region::Region(offset_t start, offset_t end) |
paul@0 | 54 | : start(start), end(end), state(end - start) |
paul@0 | 55 | { |
paul@0 | 56 | /* Content state. */ |
paul@0 | 57 | |
paul@0 | 58 | memset((void *) start, 0, end - start); |
paul@0 | 59 | } |
paul@0 | 60 | |
paul@109 | 61 | /* Return the size of the region. */ |
paul@109 | 62 | |
paul@0 | 63 | offset_t Region::size() |
paul@0 | 64 | { |
paul@0 | 65 | return end - start; |
paul@0 | 66 | } |
paul@0 | 67 | |
paul@0 | 68 | /* Debugging methods. */ |
paul@0 | 69 | |
paul@0 | 70 | int Region::compare(Region *other) |
paul@0 | 71 | { |
paul@0 | 72 | if (start < other->start) |
paul@0 | 73 | return -1; |
paul@0 | 74 | else if (start > other->start) |
paul@0 | 75 | return 1; |
paul@0 | 76 | else |
paul@0 | 77 | return 0; |
paul@0 | 78 | } |
paul@0 | 79 | |
paul@109 | 80 | /* Tag the region with file identifier and position details. */ |
paul@109 | 81 | |
paul@0 | 82 | void Region::fill(fileid_t fileid, offset_t filepos) |
paul@0 | 83 | { |
paul@0 | 84 | state.fill(fileid, filepos); |
paul@0 | 85 | } |
paul@0 | 86 | |
paul@109 | 87 | /* Remove the file details from the region state. */ |
paul@109 | 88 | |
paul@0 | 89 | void Region::flush() |
paul@0 | 90 | { |
paul@109 | 91 | state.flush(); |
paul@0 | 92 | } |
paul@0 | 93 | |
paul@0 | 94 | /* Simulation methods. */ |
paul@0 | 95 | |
paul@0 | 96 | char *Region::read(offset_t offset) |
paul@0 | 97 | { |
paul@32 | 98 | if (offset < size()) |
paul@32 | 99 | return (char *) start + offset; |
paul@32 | 100 | else |
paul@32 | 101 | return NULL; |
paul@0 | 102 | } |
paul@0 | 103 | |
paul@0 | 104 | void Region::write(const char *data, offset_t offset) |
paul@0 | 105 | { |
paul@0 | 106 | size_t length = strlen(data); |
paul@0 | 107 | |
paul@0 | 108 | if (offset + length < size()) |
paul@0 | 109 | memcpy((void *) (start + offset), data, length + 1); |
paul@0 | 110 | } |
paul@0 | 111 | |
paul@0 | 112 | // vim: tabstop=4 expandtab shiftwidth=4 |