* ppc-opc.c (powerpc_opcodes) <"lswx">: Use RAX for the second and
[binutils-gdb.git] / gdb / memrange.c
blobed41b9ac01ddf8c078d7cdc937a8b0b1ddfd068a
1 /* Memory ranges
3 Copyright (C) 2010-2012 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "defs.h"
21 #include "memrange.h"
23 int
24 mem_ranges_overlap (CORE_ADDR start1, int len1,
25 CORE_ADDR start2, int len2)
27 ULONGEST h, l;
29 l = max (start1, start2);
30 h = min (start1 + len1, start2 + len2);
31 return (l < h);
34 /* qsort comparison function, that compares mem_ranges. Ranges are
35 sorted in ascending START order. */
37 static int
38 compare_mem_ranges (const void *ap, const void *bp)
40 const struct mem_range *r1 = ap;
41 const struct mem_range *r2 = bp;
43 if (r1->start > r2->start)
44 return 1;
45 else if (r1->start < r2->start)
46 return -1;
47 else
48 return 0;
51 void
52 normalize_mem_ranges (VEC(mem_range_s) *ranges)
54 /* This function must not use any VEC operation on RANGES that
55 reallocates the memory block as that invalidates the RANGES
56 pointer, which callers expect to remain valid. */
58 if (!VEC_empty (mem_range_s, ranges))
60 struct mem_range *ra, *rb;
61 int a, b;
63 qsort (VEC_address (mem_range_s, ranges),
64 VEC_length (mem_range_s, ranges),
65 sizeof (mem_range_s),
66 compare_mem_ranges);
68 a = 0;
69 ra = VEC_index (mem_range_s, ranges, a);
70 for (b = 1; VEC_iterate (mem_range_s, ranges, b, rb); b++)
72 /* If mem_range B overlaps or is adjacent to mem_range A,
73 merge them. */
74 if (rb->start <= ra->start + ra->length)
76 ra->length = max (ra->length,
77 (rb->start - ra->start) + rb->length);
78 continue; /* next b, same a */
80 a++; /* next a */
81 ra = VEC_index (mem_range_s, ranges, a);
83 if (a != b)
84 *ra = *rb;
86 VEC_truncate (mem_range_s, ranges, a + 1);