memrange.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Memory ranges
  2. Copyright (C) 2010-2022 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. #include <algorithm>
  17. int
  18. mem_ranges_overlap (CORE_ADDR start1, int len1,
  19. CORE_ADDR start2, int len2)
  20. {
  21. ULONGEST h, l;
  22. l = std::max (start1, start2);
  23. h = std::min (start1 + len1, start2 + len2);
  24. return (l < h);
  25. }
  26. /* See memrange.h. */
  27. int
  28. address_in_mem_range (CORE_ADDR address, const struct mem_range *r)
  29. {
  30. return (r->start <= address
  31. && (address - r->start) < r->length);
  32. }
  33. void
  34. normalize_mem_ranges (std::vector<mem_range> *memory)
  35. {
  36. if (!memory->empty ())
  37. {
  38. std::vector<mem_range> &m = *memory;
  39. std::sort (m.begin (), m.end ());
  40. int a = 0;
  41. for (int b = 1; b < m.size (); b++)
  42. {
  43. /* If mem_range B overlaps or is adjacent to mem_range A,
  44. merge them. */
  45. if (m[b].start <= m[a].start + m[a].length)
  46. {
  47. m[a].length = std::max ((CORE_ADDR) m[a].length,
  48. (m[b].start - m[a].start) + m[b].length);
  49. continue; /* next b, same a */
  50. }
  51. a++; /* next a */
  52. if (a != b)
  53. m[a] = m[b];
  54. }
  55. m.resize (a + 1);
  56. }
  57. }