]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/memrange.c
Fix regression on aarch64-linux gdbserver
[thirdparty/binutils-gdb.git] / gdb / memrange.c
CommitLineData
2a7498d8
PA
1/* Memory ranges
2
1d506c26 3 Copyright (C) 2010-2024 Free Software Foundation, Inc.
2a7498d8
PA
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
2a7498d8 20#include "memrange.h"
325fac50 21#include <algorithm>
2a7498d8
PA
22
23int
24mem_ranges_overlap (CORE_ADDR start1, int len1,
25 CORE_ADDR start2, int len2)
26{
27 ULONGEST h, l;
28
325fac50
PA
29 l = std::max (start1, start2);
30 h = std::min (start1 + len1, start2 + len2);
2a7498d8
PA
31 return (l < h);
32}
33
3437254d
PA
34/* See memrange.h. */
35
36int
37address_in_mem_range (CORE_ADDR address, const struct mem_range *r)
38{
39 return (r->start <= address
40 && (address - r->start) < r->length);
41}
42
2a7498d8 43void
a79b1bc6 44normalize_mem_ranges (std::vector<mem_range> *memory)
2a7498d8 45{
a79b1bc6 46 if (!memory->empty ())
2a7498d8 47 {
a79b1bc6 48 std::vector<mem_range> &m = *memory;
2a7498d8 49
a79b1bc6 50 std::sort (m.begin (), m.end ());
2a7498d8 51
a79b1bc6
SM
52 int a = 0;
53 for (int b = 1; b < m.size (); b++)
2a7498d8
PA
54 {
55 /* If mem_range B overlaps or is adjacent to mem_range A,
56 merge them. */
a79b1bc6 57 if (m[b].start <= m[a].start + m[a].length)
2a7498d8 58 {
a79b1bc6
SM
59 m[a].length = std::max ((CORE_ADDR) m[a].length,
60 (m[b].start - m[a].start) + m[b].length);
2a7498d8
PA
61 continue; /* next b, same a */
62 }
63 a++; /* next a */
2a7498d8
PA
64
65 if (a != b)
a79b1bc6 66 m[a] = m[b];
2a7498d8 67 }
a79b1bc6
SM
68
69 m.resize (a + 1);
2a7498d8
PA
70 }
71}