1 /* 2 * Memory area support. 3 * 4 * Copyright (C) 2022, 2023 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 #pragma once 23 24 #include <map> 25 #include <set> 26 #include <stack> 27 28 #include <l4/sys/types.h> 29 30 #include <systypes/base.h> 31 32 33 34 /* A memory area abstraction. */ 35 36 class MemoryArea 37 { 38 protected: 39 address_t _start, _end; 40 41 public: 42 explicit MemoryArea() 43 : _start(0), _end(0) 44 { 45 } 46 47 explicit MemoryArea(address_t start, address_t end) 48 : _start(start), _end(end) 49 { 50 } 51 52 virtual ~MemoryArea() 53 { 54 } 55 56 virtual MemoryArea *copy(); 57 58 /* Access to area properties. */ 59 60 virtual address_t area_start() 61 { return _start; } 62 63 virtual address_t area_end() 64 { return _end; } 65 66 virtual l4_umword_t flags() 67 { return 0; } 68 69 virtual l4_cap_idx_t dataspace() 70 { return L4_INVALID_CAP; } 71 72 virtual address_t dataspace_start() 73 { return 0; } 74 75 /* Return whether the area supports the given address. */ 76 77 virtual bool supports(address_t addr) 78 { return (_start <= addr) && (addr < _end); } 79 80 /* Return whether an area is reserved and therefore cannot be mapped. */ 81 82 virtual bool is_reserved() 83 { return false; } 84 85 /* Return whether the area provides a mapped region. */ 86 87 virtual bool is_mapped() 88 { return false; } 89 90 /* Support for populating areas. */ 91 92 virtual long add(MemoryArea &area); 93 94 virtual long remove(MemoryArea &area); 95 96 /* Support for finding regions. */ 97 98 virtual long find(address_t addr, MemoryArea **area, MemoryArea **parent); 99 100 /* Support for finding areas. */ 101 102 virtual long find(address_t *start, address_t *size, map_flags_t flags, 103 unsigned char align, MemoryArea **area); 104 }; 105 106 107 108 /* A reserved area abstraction. */ 109 110 class ReservedMemoryArea : public MemoryArea 111 { 112 public: 113 explicit ReservedMemoryArea(address_t start, address_t end) 114 : MemoryArea(start, end) 115 { 116 } 117 118 virtual MemoryArea *copy(); 119 120 /* Return whether an area is reserved and therefore cannot be mapped. */ 121 122 virtual bool is_reserved() 123 { return true; } 124 125 /* Support for finding areas. */ 126 127 virtual long find(address_t addr, MemoryArea **area, MemoryArea **parent); 128 }; 129 130 131 132 /* Collection types. */ 133 134 typedef std::map<address_t, MemoryArea *> MemoryAreaMap; 135 typedef std::set<MemoryArea *> MemoryAreas; 136 137 138 139 /* A memory area containing other areas. */ 140 141 class AvailableMemoryArea : public MemoryArea 142 { 143 protected: 144 MemoryAreaMap _areas; 145 MemoryAreas _allocated; 146 147 public: 148 explicit AvailableMemoryArea(address_t start, address_t end) 149 : MemoryArea(start, end) 150 { 151 } 152 153 virtual ~AvailableMemoryArea(); 154 155 virtual MemoryArea *copy(); 156 157 /* Support for populating areas. */ 158 159 virtual long add(MemoryArea &area); 160 161 virtual long remove(MemoryArea &area); 162 163 /* Support for finding areas. */ 164 165 virtual long find(address_t addr, MemoryArea **area, MemoryArea **parent); 166 167 virtual long find(address_t *start, address_t *size, map_flags_t flags, 168 unsigned char align, MemoryArea **area); 169 170 /* A recursive iterator over a memory area. */ 171 172 class iterator 173 { 174 protected: 175 std::stack<MemoryAreaMap::iterator> _iterators, _ends; 176 177 void ascend(); 178 179 void descend(); 180 181 void descend_all(); 182 183 public: 184 explicit iterator(); 185 186 explicit iterator(MemoryAreaMap::iterator it, 187 MemoryAreaMap::iterator end); 188 189 MemoryArea *operator *(); 190 191 iterator &operator ++(); 192 193 iterator &operator ++(int); 194 195 bool operator ==(iterator other); 196 197 bool operator !=(iterator other); 198 199 /* Access to the underlying iterators. */ 200 201 MemoryAreaMap::iterator &area_iterator(); 202 203 MemoryAreaMap::iterator &area_end(); 204 }; 205 206 /* Iteration methods. */ 207 208 virtual iterator begin(); 209 210 virtual iterator end(); 211 212 virtual MemoryAreaMap::iterator areas_begin(); 213 214 virtual MemoryAreaMap::iterator areas_end(); 215 }; 216 217 /* vim: tabstop=2 expandtab shiftwidth=2 218 */