1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libexec/include/exec/memory_area.h Wed Mar 15 00:33:40 2023 +0100
1.3 @@ -0,0 +1,218 @@
1.4 +/*
1.5 + * Memory area support.
1.6 + *
1.7 + * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#pragma once
1.26 +
1.27 +#include <map>
1.28 +#include <set>
1.29 +#include <stack>
1.30 +
1.31 +#include <l4/sys/types.h>
1.32 +
1.33 +#include <systypes/base.h>
1.34 +
1.35 +
1.36 +
1.37 +/* A memory area abstraction. */
1.38 +
1.39 +class MemoryArea
1.40 +{
1.41 +protected:
1.42 + address_t _start, _end;
1.43 +
1.44 +public:
1.45 + explicit MemoryArea()
1.46 + : _start(0), _end(0)
1.47 + {
1.48 + }
1.49 +
1.50 + explicit MemoryArea(address_t start, address_t end)
1.51 + : _start(start), _end(end)
1.52 + {
1.53 + }
1.54 +
1.55 + virtual ~MemoryArea()
1.56 + {
1.57 + }
1.58 +
1.59 + virtual MemoryArea *copy();
1.60 +
1.61 + /* Access to area properties. */
1.62 +
1.63 + virtual address_t area_start()
1.64 + { return _start; }
1.65 +
1.66 + virtual address_t area_end()
1.67 + { return _end; }
1.68 +
1.69 + virtual l4_umword_t flags()
1.70 + { return 0; }
1.71 +
1.72 + virtual l4_cap_idx_t dataspace()
1.73 + { return L4_INVALID_CAP; }
1.74 +
1.75 + virtual address_t dataspace_start()
1.76 + { return 0; }
1.77 +
1.78 + /* Return whether the area supports the given address. */
1.79 +
1.80 + virtual bool supports(address_t addr)
1.81 + { return (_start <= addr) && (addr < _end); }
1.82 +
1.83 + /* Return whether an area is reserved and therefore cannot be mapped. */
1.84 +
1.85 + virtual bool is_reserved()
1.86 + { return false; }
1.87 +
1.88 + /* Return whether the area provides a mapped region. */
1.89 +
1.90 + virtual bool is_mapped()
1.91 + { return false; }
1.92 +
1.93 + /* Support for populating areas. */
1.94 +
1.95 + virtual long add(MemoryArea &area);
1.96 +
1.97 + virtual long remove(MemoryArea &area);
1.98 +
1.99 + /* Support for finding regions. */
1.100 +
1.101 + virtual long find(address_t addr, MemoryArea **area);
1.102 +
1.103 + /* Support for finding areas. */
1.104 +
1.105 + virtual long find(address_t *start, address_t *size, map_flags_t flags,
1.106 + unsigned char align, MemoryArea **area);
1.107 +};
1.108 +
1.109 +
1.110 +
1.111 +/* A reserved area abstraction. */
1.112 +
1.113 +class ReservedMemoryArea : public MemoryArea
1.114 +{
1.115 +public:
1.116 + explicit ReservedMemoryArea(address_t start, address_t end)
1.117 + : MemoryArea(start, end)
1.118 + {
1.119 + }
1.120 +
1.121 + virtual MemoryArea *copy();
1.122 +
1.123 + /* Return whether an area is reserved and therefore cannot be mapped. */
1.124 +
1.125 + virtual bool is_reserved()
1.126 + { return true; }
1.127 +
1.128 + /* Support for finding areas. */
1.129 +
1.130 + virtual long find(address_t addr, MemoryArea **area);
1.131 +};
1.132 +
1.133 +
1.134 +
1.135 +/* Collection types. */
1.136 +
1.137 +typedef std::map<address_t, MemoryArea *> MemoryAreaMap;
1.138 +typedef std::set<MemoryArea *> MemoryAreas;
1.139 +
1.140 +
1.141 +
1.142 +/* A memory area containing other areas. */
1.143 +
1.144 +class AvailableMemoryArea : public MemoryArea
1.145 +{
1.146 +protected:
1.147 + MemoryAreaMap _areas;
1.148 + MemoryAreas _allocated;
1.149 +
1.150 +public:
1.151 + explicit AvailableMemoryArea(address_t start, address_t end)
1.152 + : MemoryArea(start, end)
1.153 + {
1.154 + }
1.155 +
1.156 + virtual ~AvailableMemoryArea();
1.157 +
1.158 + virtual MemoryArea *copy();
1.159 +
1.160 + /* Support for populating areas. */
1.161 +
1.162 + virtual long add(MemoryArea &area);
1.163 +
1.164 + virtual long remove(MemoryArea &area);
1.165 +
1.166 + /* Support for finding areas. */
1.167 +
1.168 + virtual long find(address_t addr, MemoryArea **area);
1.169 +
1.170 + virtual long find(address_t *start, address_t *size, map_flags_t flags,
1.171 + unsigned char align, MemoryArea **area);
1.172 +
1.173 + /* A recursive iterator over a memory area. */
1.174 +
1.175 + class iterator
1.176 + {
1.177 + protected:
1.178 + std::stack<MemoryAreaMap::iterator> _iterators, _ends;
1.179 +
1.180 + void ascend();
1.181 +
1.182 + void descend();
1.183 +
1.184 + void descend_all();
1.185 +
1.186 + public:
1.187 + explicit iterator();
1.188 +
1.189 + explicit iterator(MemoryAreaMap::iterator it,
1.190 + MemoryAreaMap::iterator end);
1.191 +
1.192 + MemoryArea *operator *();
1.193 +
1.194 + iterator &operator ++();
1.195 +
1.196 + iterator &operator ++(int);
1.197 +
1.198 + bool operator ==(iterator other);
1.199 +
1.200 + bool operator !=(iterator other);
1.201 +
1.202 + /* Access to the underlying iterators. */
1.203 +
1.204 + MemoryAreaMap::iterator &area_iterator();
1.205 +
1.206 + MemoryAreaMap::iterator &area_end();
1.207 + };
1.208 +
1.209 + /* Iteration methods. */
1.210 +
1.211 + virtual iterator begin();
1.212 +
1.213 + virtual iterator end();
1.214 +
1.215 + virtual MemoryAreaMap::iterator areas_begin();
1.216 +
1.217 + virtual MemoryAreaMap::iterator areas_end();
1.218 +};
1.219 +
1.220 +/* vim: tabstop=2 expandtab shiftwidth=2
1.221 +*/