Commit | Line | Data |
---|---|---|
2a7498d8 PA |
1 | /* Memory ranges |
2 | ||
42a4f53d | 3 | Copyright (C) 2010-2019 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 | ||
20 | #include "defs.h" | |
21 | #include "memrange.h" | |
325fac50 | 22 | #include <algorithm> |
2a7498d8 PA |
23 | |
24 | int | |
25 | mem_ranges_overlap (CORE_ADDR start1, int len1, | |
26 | CORE_ADDR start2, int len2) | |
27 | { | |
28 | ULONGEST h, l; | |
29 | ||
325fac50 PA |
30 | l = std::max (start1, start2); |
31 | h = std::min (start1 + len1, start2 + len2); | |
2a7498d8 PA |
32 | return (l < h); |
33 | } | |
34 | ||
3437254d PA |
35 | /* See memrange.h. */ |
36 | ||
37 | int | |
38 | address_in_mem_range (CORE_ADDR address, const struct mem_range *r) | |
39 | { | |
40 | return (r->start <= address | |
41 | && (address - r->start) < r->length); | |
42 | } | |
43 | ||
2a7498d8 | 44 | void |
a79b1bc6 | 45 | normalize_mem_ranges (std::vector<mem_range> *memory) |
2a7498d8 | 46 | { |
c0f61f9c PA |
47 | /* This function must not use any VEC operation on RANGES that |
48 | reallocates the memory block as that invalidates the RANGES | |
49 | pointer, which callers expect to remain valid. */ | |
50 | ||
a79b1bc6 | 51 | if (!memory->empty ()) |
2a7498d8 | 52 | { |
a79b1bc6 | 53 | std::vector<mem_range> &m = *memory; |
2a7498d8 | 54 | |
a79b1bc6 | 55 | std::sort (m.begin (), m.end ()); |
2a7498d8 | 56 | |
a79b1bc6 SM |
57 | int a = 0; |
58 | for (int b = 1; b < m.size (); b++) | |
2a7498d8 PA |
59 | { |
60 | /* If mem_range B overlaps or is adjacent to mem_range A, | |
61 | merge them. */ | |
a79b1bc6 | 62 | if (m[b].start <= m[a].start + m[a].length) |
2a7498d8 | 63 | { |
a79b1bc6 SM |
64 | m[a].length = std::max ((CORE_ADDR) m[a].length, |
65 | (m[b].start - m[a].start) + m[b].length); | |
2a7498d8 PA |
66 | continue; /* next b, same a */ |
67 | } | |
68 | a++; /* next a */ | |
2a7498d8 PA |
69 | |
70 | if (a != b) | |
a79b1bc6 | 71 | m[a] = m[b]; |
2a7498d8 | 72 | } |
a79b1bc6 SM |
73 | |
74 | m.resize (a + 1); | |
2a7498d8 PA |
75 | } |
76 | } |