gdb/memrange.c - gdb

Functions defined

Source code

  1. /* Memory ranges

  2.    Copyright (C) 2010-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "memrange.h"

  16. int
  17. mem_ranges_overlap (CORE_ADDR start1, int len1,
  18.                     CORE_ADDR start2, int len2)
  19. {
  20.   ULONGEST h, l;

  21.   l = max (start1, start2);
  22.   h = min (start1 + len1, start2 + len2);
  23.   return (l < h);
  24. }

  25. /* See memrange.h.  */

  26. int
  27. address_in_mem_range (CORE_ADDR address, const struct mem_range *r)
  28. {
  29.   return (r->start <= address
  30.           && (address - r->start) < r->length);
  31. }

  32. /* qsort comparison function, that compares mem_ranges.  Ranges are
  33.    sorted in ascending START order.  */

  34. static int
  35. compare_mem_ranges (const void *ap, const void *bp)
  36. {
  37.   const struct mem_range *r1 = ap;
  38.   const struct mem_range *r2 = bp;

  39.   if (r1->start > r2->start)
  40.     return 1;
  41.   else if (r1->start < r2->start)
  42.     return -1;
  43.   else
  44.     return 0;
  45. }

  46. void
  47. normalize_mem_ranges (VEC(mem_range_s) *ranges)
  48. {
  49.   /* This function must not use any VEC operation on RANGES that
  50.      reallocates the memory block as that invalidates the RANGES
  51.      pointer, which callers expect to remain valid.  */

  52.   if (!VEC_empty (mem_range_s, ranges))
  53.     {
  54.       struct mem_range *ra, *rb;
  55.       int a, b;

  56.       qsort (VEC_address (mem_range_s, ranges),
  57.              VEC_length (mem_range_s, ranges),
  58.              sizeof (mem_range_s),
  59.              compare_mem_ranges);

  60.       a = 0;
  61.       ra = VEC_index (mem_range_s, ranges, a);
  62.       for (b = 1; VEC_iterate (mem_range_s, ranges, b, rb); b++)
  63.         {
  64.           /* If mem_range B overlaps or is adjacent to mem_range A,
  65.              merge them.  */
  66.           if (rb->start <= ra->start + ra->length)
  67.             {
  68.               ra->length = max (ra->length,
  69.                                 (rb->start - ra->start) + rb->length);
  70.               continue;                /* next b, same a */
  71.             }
  72.           a++;                        /* next a */
  73.           ra = VEC_index (mem_range_s, ranges, a);

  74.           if (a != b)
  75.             *ra = *rb;
  76.         }
  77.       VEC_truncate (mem_range_s, ranges, a + 1);
  78.     }
  79. }