[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 08/22] range: Introduce range_inverse_array()
|
From: |
Cédric Le Goater |
|
Subject: |
[PULL 08/22] range: Introduce range_inverse_array() |
|
Date: |
Mon, 6 Nov 2023 15:36:39 +0100 |
From: Eric Auger <eric.auger@redhat.com>
This helper reverses a list of regions within a [low, high]
span, turning original regions into holes and original
holes into actual regions, covering the whole UINT64_MAX span.
Signed-off-by: Eric Auger <eric.auger@redhat.com>
Tested-by: Yanghang Liu <yanghliu@redhat.com>
Reviewed-by: "Michael S. Tsirkin" <mst@redhat.com>
Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
include/qemu/range.h | 8 +++++++
util/range.c | 55 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+)
diff --git a/include/qemu/range.h b/include/qemu/range.h
index
aa671da143cf82470658311599ac473c837b8102..205e1da76dc5b29327f590b8293a826ced63c25d
100644
--- a/include/qemu/range.h
+++ b/include/qemu/range.h
@@ -225,4 +225,12 @@ int range_compare(Range *a, Range *b);
GList *range_list_insert(GList *list, Range *data);
+/*
+ * Inverse an array of sorted ranges over the [low, high] span, ie.
+ * original ranges becomes holes in the newly allocated inv_ranges
+ */
+void range_inverse_array(GList *in_ranges,
+ GList **out_ranges,
+ uint64_t low, uint64_t high);
+
#endif
diff --git a/util/range.c b/util/range.c
index
782cb8b21c77867864a0da1b31a3638f465d1ae6..9605ccfcbed9749dc8c2a665a037300c97981d28
100644
--- a/util/range.c
+++ b/util/range.c
@@ -66,3 +66,58 @@ GList *range_list_insert(GList *list, Range *data)
return list;
}
+
+static inline
+GList *append_new_range(GList *list, uint64_t lob, uint64_t upb)
+{
+ Range *new = g_new0(Range, 1);
+
+ range_set_bounds(new, lob, upb);
+ return g_list_append(list, new);
+}
+
+
+void range_inverse_array(GList *in, GList **rev,
+ uint64_t low, uint64_t high)
+{
+ Range *r, *rn;
+ GList *l = in, *out = *rev;
+
+ for (l = in; l && range_upb(l->data) < low; l = l->next) {
+ continue;
+ }
+
+ if (!l) {
+ out = append_new_range(out, low, high);
+ goto exit;
+ }
+ r = (Range *)l->data;
+
+ /* first range lob is greater than min, insert a first range */
+ if (range_lob(r) > low) {
+ out = append_new_range(out, low, MIN(range_lob(r) - 1, high));
+ }
+
+ /* insert a range inbetween each original range until we reach high */
+ for (; l->next; l = l->next) {
+ r = (Range *)l->data;
+ rn = (Range *)l->next->data;
+ if (range_lob(r) >= high) {
+ goto exit;
+ }
+ if (range_compare(r, rn)) {
+ out = append_new_range(out, range_upb(r) + 1,
+ MIN(range_lob(rn) - 1, high));
+ }
+ }
+
+ /* last range */
+ r = (Range *)l->data;
+
+ /* last range upb is less than max, insert a last range */
+ if (range_upb(r) < high) {
+ out = append_new_range(out, range_upb(r) + 1, high);
+ }
+exit:
+ *rev = out;
+}
--
2.41.0
- [PULL 00/22] vfio queue, Cédric Le Goater, 2023/11/06
- [PULL 02/22] memory: Introduce memory_region_iommu_set_iova_ranges, Cédric Le Goater, 2023/11/06
- [PULL 01/22] memory: Let ReservedRegion use Range, Cédric Le Goater, 2023/11/06
- [PULL 04/22] virtio-iommu: Rename reserved_regions into prop_resv_regions, Cédric Le Goater, 2023/11/06
- [PULL 05/22] range: Make range_compare() public, Cédric Le Goater, 2023/11/06
- [PULL 06/22] util/reserved-region: Add new ReservedRegion helpers, Cédric Le Goater, 2023/11/06
- [PULL 07/22] virtio-iommu: Introduce per IOMMUDevice reserved regions, Cédric Le Goater, 2023/11/06
- [PULL 03/22] vfio: Collect container iova range info, Cédric Le Goater, 2023/11/06
- [PULL 08/22] range: Introduce range_inverse_array(),
Cédric Le Goater <=
- [PULL 09/22] virtio-iommu: Record whether a probe request has been issued, Cédric Le Goater, 2023/11/06
- [PULL 10/22] virtio-iommu: Implement set_iova_ranges() callback, Cédric Le Goater, 2023/11/06
- [PULL 12/22] test: Add some tests for range and resv-mem helpers, Cédric Le Goater, 2023/11/06
- [PULL 11/22] virtio-iommu: Consolidate host reserved regions and property set ones, Cédric Le Goater, 2023/11/06
- [PULL 13/22] hw/pci: modify pci_setup_iommu() to set PCIIOMMUOps, Cédric Le Goater, 2023/11/06
- [PULL 14/22] util/uuid: Add UUID_STR_LEN definition, Cédric Le Goater, 2023/11/06
- [PULL 15/22] vfio/pci: Fix buffer overrun when writing the VF token, Cédric Le Goater, 2023/11/06
- [PULL 16/22] util/uuid: Remove UUID_FMT_LEN, Cédric Le Goater, 2023/11/06