GDB (xrefs)
/tmp/gdb-7.10/gdb/memrange.c
Go to the documentation of this file.
1 /* Memory ranges
2 
3  Copyright (C) 2010-2015 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 
23 int
24 mem_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 /* See memrange.h. */
35 
36 int
37 address_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 
43 /* qsort comparison function, that compares mem_ranges. Ranges are
44  sorted in ascending START order. */
45 
46 static int
47 compare_mem_ranges (const void *ap, const void *bp)
48 {
49  const struct mem_range *r1 = ap;
50  const struct mem_range *r2 = bp;
51 
52  if (r1->start > r2->start)
53  return 1;
54  else if (r1->start < r2->start)
55  return -1;
56  else
57  return 0;
58 }
59 
60 void
62 {
63  /* This function must not use any VEC operation on RANGES that
64  reallocates the memory block as that invalidates the RANGES
65  pointer, which callers expect to remain valid. */
66 
67  if (!VEC_empty (mem_range_s, ranges))
68  {
69  struct mem_range *ra, *rb;
70  int a, b;
71 
72  qsort (VEC_address (mem_range_s, ranges),
73  VEC_length (mem_range_s, ranges),
74  sizeof (mem_range_s),
76 
77  a = 0;
78  ra = VEC_index (mem_range_s, ranges, a);
79  for (b = 1; VEC_iterate (mem_range_s, ranges, b, rb); b++)
80  {
81  /* If mem_range B overlaps or is adjacent to mem_range A,
82  merge them. */
83  if (rb->start <= ra->start + ra->length)
84  {
85  ra->length = max (ra->length,
86  (rb->start - ra->start) + rb->length);
87  continue; /* next b, same a */
88  }
89  a++; /* next a */
90  ra = VEC_index (mem_range_s, ranges, a);
91 
92  if (a != b)
93  *ra = *rb;
94  }
95  VEC_truncate (mem_range_s, ranges, a + 1);
96  }
97 }
bfd_vma CORE_ADDR
Definition: common-types.h:41
#define VEC(T)
Definition: vec.h:398
#define VEC_iterate(T, V, I, P)
Definition: vec.h:165
int mem_ranges_overlap(CORE_ADDR start1, int len1, CORE_ADDR start2, int len2)
Definition: memrange.c:24
static int compare_mem_ranges(const void *ap, const void *bp)
Definition: memrange.c:47
#define VEC_length(T, V)
Definition: vec.h:124
#define VEC_index(T, V, I)
Definition: vec.h:151
#define min(a, b)
Definition: defs.h:106
#define VEC_empty(T, V)
Definition: vec.h:132
#define VEC_truncate(T, V, I)
Definition: vec.h:278
#define max(a, b)
Definition: defs.h:109
#define qsort
Definition: ada-exp.c:2747
#define VEC_address(T, V)
Definition: vec.h:369
void normalize_mem_ranges(VEC(mem_range_s)*ranges)
Definition: memrange.c:61
int address_in_mem_range(CORE_ADDR address, const struct mem_range *r)
Definition: memrange.c:37
unsigned long long ULONGEST
Definition: common-types.h:53
int length
Definition: memrange.h:33
CORE_ADDR start
Definition: memrange.h:30