GDB (API)
/home/stan/gdb/src/gdb/memrange.c
Go to the documentation of this file.
00001 /* Memory ranges
00002 
00003    Copyright (C) 2010-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "memrange.h"
00022 
00023 int
00024 mem_ranges_overlap (CORE_ADDR start1, int len1,
00025                     CORE_ADDR start2, int len2)
00026 {
00027   ULONGEST h, l;
00028 
00029   l = max (start1, start2);
00030   h = min (start1 + len1, start2 + len2);
00031   return (l < h);
00032 }
00033 
00034 /* qsort comparison function, that compares mem_ranges.  Ranges are
00035    sorted in ascending START order.  */
00036 
00037 static int
00038 compare_mem_ranges (const void *ap, const void *bp)
00039 {
00040   const struct mem_range *r1 = ap;
00041   const struct mem_range *r2 = bp;
00042 
00043   if (r1->start > r2->start)
00044     return 1;
00045   else if (r1->start < r2->start)
00046     return -1;
00047   else
00048     return 0;
00049 }
00050 
00051 void
00052 normalize_mem_ranges (VEC(mem_range_s) *ranges)
00053 {
00054   /* This function must not use any VEC operation on RANGES that
00055      reallocates the memory block as that invalidates the RANGES
00056      pointer, which callers expect to remain valid.  */
00057 
00058   if (!VEC_empty (mem_range_s, ranges))
00059     {
00060       struct mem_range *ra, *rb;
00061       int a, b;
00062 
00063       qsort (VEC_address (mem_range_s, ranges),
00064              VEC_length (mem_range_s, ranges),
00065              sizeof (mem_range_s),
00066              compare_mem_ranges);
00067 
00068       a = 0;
00069       ra = VEC_index (mem_range_s, ranges, a);
00070       for (b = 1; VEC_iterate (mem_range_s, ranges, b, rb); b++)
00071         {
00072           /* If mem_range B overlaps or is adjacent to mem_range A,
00073              merge them.  */
00074           if (rb->start <= ra->start + ra->length)
00075             {
00076               ra->length = max (ra->length,
00077                                 (rb->start - ra->start) + rb->length);
00078               continue;         /* next b, same a */
00079             }
00080           a++;                  /* next a */
00081           ra = VEC_index (mem_range_s, ranges, a);
00082 
00083           if (a != b)
00084             *ra = *rb;
00085         }
00086       VEC_truncate (mem_range_s, ranges, a + 1);
00087     }
00088 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines