range.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * QEMU 64-bit address ranges
  3. *
  4. * Copyright (c) 2015-2016 Red Hat, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/range.h"
  21. int range_compare(Range *a, Range *b)
  22. {
  23. assert(!range_is_empty(a) && !range_is_empty(b));
  24. /* Careful, avoid wraparound */
  25. if (b->lob && b->lob - 1 > a->upb) {
  26. return -1;
  27. }
  28. if (a->lob && a->lob - 1 > b->upb) {
  29. return 1;
  30. }
  31. return 0;
  32. }
  33. /* Insert @data into @list of ranges; caller no longer owns @data */
  34. GList *range_list_insert(GList *list, Range *data)
  35. {
  36. GList *l;
  37. assert(!range_is_empty(data));
  38. /* Skip all list elements strictly less than data */
  39. for (l = list; l && range_compare(l->data, data) < 0; l = l->next) {
  40. }
  41. if (!l || range_compare(l->data, data) > 0) {
  42. /* Rest of the list (if any) is strictly greater than @data */
  43. return g_list_insert_before(list, l, data);
  44. }
  45. /* Current list element overlaps @data, merge the two */
  46. range_extend(l->data, data);
  47. g_free(data);
  48. /* Merge any subsequent list elements that now also overlap */
  49. while (l->next && range_compare(l->data, l->next->data) == 0) {
  50. GList *new_l;
  51. range_extend(l->data, l->next->data);
  52. g_free(l->next->data);
  53. new_l = g_list_delete_link(list, l->next);
  54. assert(new_l == list);
  55. }
  56. return list;
  57. }
  58. static inline
  59. GList *append_new_range(GList *list, uint64_t lob, uint64_t upb)
  60. {
  61. Range *new = g_new0(Range, 1);
  62. range_set_bounds(new, lob, upb);
  63. return g_list_append(list, new);
  64. }
  65. void range_inverse_array(GList *in, GList **rev,
  66. uint64_t low, uint64_t high)
  67. {
  68. Range *r, *rn;
  69. GList *l = in, *out = *rev;
  70. for (l = in; l && range_upb(l->data) < low; l = l->next) {
  71. continue;
  72. }
  73. if (!l) {
  74. out = append_new_range(out, low, high);
  75. goto exit;
  76. }
  77. r = (Range *)l->data;
  78. /* first range lob is greater than min, insert a first range */
  79. if (range_lob(r) > low) {
  80. out = append_new_range(out, low, MIN(range_lob(r) - 1, high));
  81. }
  82. /* insert a range in between each original range until we reach high */
  83. for (; l->next; l = l->next) {
  84. r = (Range *)l->data;
  85. rn = (Range *)l->next->data;
  86. if (range_lob(r) >= high) {
  87. goto exit;
  88. }
  89. if (range_compare(r, rn)) {
  90. out = append_new_range(out, range_upb(r) + 1,
  91. MIN(range_lob(rn) - 1, high));
  92. }
  93. }
  94. /* last range */
  95. r = (Range *)l->data;
  96. /* last range upb is less than max, insert a last range */
  97. if (range_upb(r) < high) {
  98. out = append_new_range(out, range_upb(r) + 1, high);
  99. }
  100. exit:
  101. *rev = out;
  102. }