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