]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/memrange.c
2011-02-14 Pedro Alves <pedro@codesourcery.com>
[thirdparty/binutils-gdb.git] / gdb / memrange.c
CommitLineData
2a7498d8
PA
1/* Memory ranges
2
3 Copyright (C) 2010, 2011 Free Software Foundation, Inc.
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"
22
23int
24mem_ranges_overlap (CORE_ADDR start1, int len1,
25 CORE_ADDR start2, int len2)
26{
27 ULONGEST h, l;
28
29 l = max (start1, start2);
30 h = min (start1 + len1, start2 + len2);
31 return (l < h);
32}
33
34/* qsort comparison function, that compares mem_ranges. */
35
36static int
37compare_mem_ranges (const void *ap, const void *bp)
38{
39 const struct mem_range *r1 = ap;
40 const struct mem_range *r2 = bp;
41
42 if (r1->start > r2->start)
43 return 1;
44 else if (r1->start < r2->start)
45 return -1;
46 else
47 return 0;
48}
49
50void
51normalize_mem_ranges (VEC(mem_range_s) *ranges)
52{
53 if (!VEC_empty (mem_range_s, ranges))
54 {
55 struct mem_range *ra, *rb;
56 int a, b;
57
58 qsort (VEC_address (mem_range_s, ranges),
59 VEC_length (mem_range_s, ranges),
60 sizeof (mem_range_s),
61 compare_mem_ranges);
62
63 a = 0;
64 ra = VEC_index (mem_range_s, ranges, a);
65 for (b = 1; VEC_iterate (mem_range_s, ranges, b, rb); b++)
66 {
67 /* If mem_range B overlaps or is adjacent to mem_range A,
68 merge them. */
69 if (rb->start <= ra->start + ra->length)
70 {
71 ra->length = (rb->start + rb->length) - ra->start;
72 continue; /* next b, same a */
73 }
74 a++; /* next a */
75 ra = VEC_index (mem_range_s, ranges, a);
76
77 if (a != b)
78 *ra = *rb;
79 }
80 VEC_truncate (mem_range_s, ranges, a + 1);
81 }
82}