1 /* 2 * Memory region abstractions. 3 * 4 * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <string.h> 23 #include <stdlib.h> 24 25 #include "region.h" 26 27 28 29 /* Initialise region state, indicating the size, file and position. */ 30 31 RegionState::RegionState(unsigned long size, fileid_t fileid, offset_t filepos) 32 : size(size), fileid(fileid), filepos(filepos) 33 { 34 } 35 36 void RegionState::fill(fileid_t fileid, offset_t filepos) 37 { 38 this->fileid = fileid; 39 this->filepos = filepos; 40 } 41 42 43 44 /* Initialise a region having the given 'start' and 'end' addresses, with the 45 'end' being one location beyond the last address in the region. */ 46 47 Region::Region(offset_t start, offset_t end) 48 : start(start), end(end), state(end - start) 49 { 50 /* Content state. */ 51 52 memset((void *) start, 0, end - start); 53 } 54 55 Region::~Region() 56 { 57 free((void *) start); 58 } 59 60 offset_t Region::size() 61 { 62 return end - start; 63 } 64 65 /* Debugging methods. */ 66 67 int Region::compare(Region *other) 68 { 69 if (start < other->start) 70 return -1; 71 else if (start > other->start) 72 return 1; 73 else 74 return 0; 75 } 76 77 void Region::fill(fileid_t fileid, offset_t filepos) 78 { 79 state.fill(fileid, filepos); 80 } 81 82 void Region::flush() 83 { 84 } 85 86 /* Simulation methods. */ 87 88 char *Region::read(offset_t offset) 89 { 90 if (offset < size()) 91 return (char *) start + offset; 92 else 93 return NULL; 94 } 95 96 void Region::write(const char *data, offset_t offset) 97 { 98 size_t length = strlen(data); 99 100 if (offset + length < size()) 101 memcpy((void *) (start + offset), data, length + 1); 102 } 103 104 // vim: tabstop=4 expandtab shiftwidth=4