mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
lib: Replace qsort() with g_qsort_with_data()
While glibc provides qsort(), which usually is just a mergesort, until sorting arrays so huge that temporary array used by mergesort would not fit into physical memory (which in our case is never), we are not guaranteed it'll use mergesort. The advantage of mergesort is clear - it's stable. IOW, if we have an array of values parsed from XML, qsort() it and produce some output based on those values, we can then compare the output with some expected output, line by line. But with newer glibc this is all history. After [1], qsort() is no longer mergesort but introsort instead, which is not stable. This is suboptimal, because in some cases we want to preserve order of equal items. For instance, in ebiptablesApplyNewRules(), nwfilter rules are sorted by their priority. But if two rules have the same priority, we want to keep them in the order they appear in the XML. Since it's hard/needless work to identify places where stable or unstable sorting is needed, let's just play it safe and use stable sorting everywhere. Fortunately, glib provides g_qsort_with_data() which indeed implement mergesort and it's a drop in replacement for qsort(), almost. It accepts fifth argument (pointer to opaque data), that is passed to comparator function, which then accepts three arguments. We have to keep one occurance of qsort() though - in NSS module which deliberately does not link with glib. 1: https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=03bf8357e8291857a435afcc3048e0b697b6cc04 Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
@@ -526,7 +526,8 @@ virshCheckpointListFree(struct virshCheckpointList *checkpointlist)
|
||||
|
||||
static int
|
||||
virshChkSorter(const void *a,
|
||||
const void *b)
|
||||
const void *b,
|
||||
void *opaque G_GNUC_UNUSED)
|
||||
{
|
||||
const struct virshChk *sa = a;
|
||||
const struct virshChk *sb = b;
|
||||
@@ -594,9 +595,10 @@ virshCheckpointListCollect(vshControl *ctl,
|
||||
}
|
||||
|
||||
if (!(orig_flags & VIR_DOMAIN_CHECKPOINT_LIST_TOPOLOGICAL) &&
|
||||
checkpointlist->chks)
|
||||
qsort(checkpointlist->chks, checkpointlist->nchks,
|
||||
sizeof(*checkpointlist->chks), virshChkSorter);
|
||||
checkpointlist->chks) {
|
||||
g_qsort_with_data(checkpointlist->chks, checkpointlist->nchks,
|
||||
sizeof(*checkpointlist->chks), virshChkSorter, NULL);
|
||||
}
|
||||
|
||||
ret = g_steal_pointer(&checkpointlist);
|
||||
|
||||
|
||||
@@ -1483,7 +1483,9 @@ static const vshCmdInfo info_list[] = {
|
||||
|
||||
/* compare domains, pack NULLed ones at the end */
|
||||
static int
|
||||
virshDomainSorter(const void *a, const void *b)
|
||||
virshDomainSorter(const void *a,
|
||||
const void *b,
|
||||
void *opaque G_GNUC_UNUSED)
|
||||
{
|
||||
virDomainPtr *da = (virDomainPtr *) a;
|
||||
virDomainPtr *db = (virDomainPtr *) b;
|
||||
@@ -1741,9 +1743,10 @@ virshDomainListCollect(vshControl *ctl, unsigned int flags)
|
||||
|
||||
finished:
|
||||
/* sort the list */
|
||||
if (list->domains && list->ndomains)
|
||||
qsort(list->domains, list->ndomains, sizeof(*list->domains),
|
||||
virshDomainSorter);
|
||||
if (list->domains && list->ndomains) {
|
||||
g_qsort_with_data(list->domains, list->ndomains,
|
||||
sizeof(*list->domains), virshDomainSorter, NULL);
|
||||
}
|
||||
|
||||
/* truncate the list if filter simulation deleted entries */
|
||||
if (deleted)
|
||||
|
||||
@@ -300,7 +300,9 @@ static const vshCmdOptDef opts_freepages[] = {
|
||||
};
|
||||
|
||||
static int
|
||||
vshPageSizeSorter(const void *a, const void *b)
|
||||
vshPageSizeSorter(const void *a,
|
||||
const void *b,
|
||||
void *opaque G_GNUC_UNUSED)
|
||||
{
|
||||
unsigned int pa = *(unsigned int *)a;
|
||||
unsigned int pb = *(unsigned int *)b;
|
||||
@@ -377,7 +379,8 @@ cmdFreepages(vshControl *ctl, const vshCmd *cmd)
|
||||
* @pagesize array will contain duplicates. We should
|
||||
* remove them otherwise not very nice output will be
|
||||
* produced. */
|
||||
qsort(pagesize, nodes_cnt, sizeof(*pagesize), vshPageSizeSorter);
|
||||
g_qsort_with_data(pagesize, nodes_cnt,
|
||||
sizeof(*pagesize), vshPageSizeSorter, NULL);
|
||||
|
||||
for (i = 0; i < nodes_cnt - 1;) {
|
||||
if (pagesize[i] == pagesize[i + 1]) {
|
||||
|
||||
@@ -141,7 +141,9 @@ cmdInterfaceEdit(vshControl *ctl, const vshCmd *cmd)
|
||||
}
|
||||
|
||||
static int
|
||||
virshInterfaceSorter(const void *a, const void *b)
|
||||
virshInterfaceSorter(const void *a,
|
||||
const void *b,
|
||||
void *opaque G_GNUC_UNUSED)
|
||||
{
|
||||
virInterfacePtr *ia = (virInterfacePtr *) a;
|
||||
virInterfacePtr *ib = (virInterfacePtr *) b;
|
||||
@@ -281,9 +283,10 @@ virshInterfaceListCollect(vshControl *ctl,
|
||||
|
||||
finished:
|
||||
/* sort the list */
|
||||
if (list->ifaces && list->nifaces)
|
||||
qsort(list->ifaces, list->nifaces,
|
||||
sizeof(*list->ifaces), virshInterfaceSorter);
|
||||
if (list->ifaces && list->nifaces) {
|
||||
g_qsort_with_data(list->ifaces, list->nifaces,
|
||||
sizeof(*list->ifaces), virshInterfaceSorter, NULL);
|
||||
}
|
||||
|
||||
/* truncate the list if filter simulation deleted entries */
|
||||
if (deleted)
|
||||
|
||||
@@ -791,7 +791,9 @@ cmdNetworkInfo(vshControl *ctl, const vshCmd *cmd)
|
||||
}
|
||||
|
||||
static int
|
||||
virshNetworkSorter(const void *a, const void *b)
|
||||
virshNetworkSorter(const void *a,
|
||||
const void *b,
|
||||
void *opaque G_GNUC_UNUSED)
|
||||
{
|
||||
virNetworkPtr *na = (virNetworkPtr *) a;
|
||||
virNetworkPtr *nb = (virNetworkPtr *) b;
|
||||
@@ -982,9 +984,10 @@ virshNetworkListCollect(vshControl *ctl,
|
||||
|
||||
finished:
|
||||
/* sort the list */
|
||||
if (list->nets && list->nnets)
|
||||
qsort(list->nets, list->nnets,
|
||||
sizeof(*list->nets), virshNetworkSorter);
|
||||
if (list->nets && list->nnets) {
|
||||
g_qsort_with_data(list->nets, list->nnets,
|
||||
sizeof(*list->nets), virshNetworkSorter, NULL);
|
||||
}
|
||||
|
||||
/* truncate the list if filter simulation deleted entries */
|
||||
if (deleted)
|
||||
@@ -1801,7 +1804,9 @@ static const vshCmdOptDef opts_network_dhcp_leases[] = {
|
||||
};
|
||||
|
||||
static int
|
||||
virshNetworkDHCPLeaseSorter(const void *a, const void *b)
|
||||
virshNetworkDHCPLeaseSorter(const void *a,
|
||||
const void *b,
|
||||
void *opaque G_GNUC_UNUSED)
|
||||
{
|
||||
virNetworkDHCPLeasePtr *lease1 = (virNetworkDHCPLeasePtr *) a;
|
||||
virNetworkDHCPLeasePtr *lease2 = (virNetworkDHCPLeasePtr *) b;
|
||||
@@ -1840,7 +1845,8 @@ cmdNetworkDHCPLeases(vshControl *ctl, const vshCmd *cmd)
|
||||
}
|
||||
|
||||
/* Sort the list according to MAC Address/IAID */
|
||||
qsort(leases, nleases, sizeof(*leases), virshNetworkDHCPLeaseSorter);
|
||||
g_qsort_with_data(leases, nleases,
|
||||
sizeof(*leases), virshNetworkDHCPLeaseSorter, NULL);
|
||||
|
||||
table = vshTableNew(_("Expiry Time"), _("MAC address"), _("Protocol"),
|
||||
_("IP address"), _("Hostname"), _("Client ID or DUID"),
|
||||
@@ -2068,7 +2074,9 @@ cmdNetworkPortDelete(vshControl *ctl, const vshCmd *cmd)
|
||||
|
||||
|
||||
static int
|
||||
virshNetworkPortSorter(const void *a, const void *b)
|
||||
virshNetworkPortSorter(const void *a,
|
||||
const void *b,
|
||||
void *opaque G_GNUC_UNUSED)
|
||||
{
|
||||
virNetworkPortPtr *na = (virNetworkPortPtr *) a;
|
||||
virNetworkPortPtr *nb = (virNetworkPortPtr *) b;
|
||||
@@ -2129,9 +2137,10 @@ virshNetworkPortListCollect(vshControl *ctl,
|
||||
list->nports = ret;
|
||||
|
||||
/* sort the list */
|
||||
if (list->ports && list->nports)
|
||||
qsort(list->ports, list->nports,
|
||||
sizeof(*list->ports), virshNetworkPortSorter);
|
||||
if (list->ports && list->nports) {
|
||||
g_qsort_with_data(list->ports, list->nports,
|
||||
sizeof(*list->ports), virshNetworkPortSorter, NULL);
|
||||
}
|
||||
|
||||
success = true;
|
||||
|
||||
|
||||
@@ -183,7 +183,9 @@ virshNodeListLookup(int devid, bool parent, void *opaque)
|
||||
}
|
||||
|
||||
static int
|
||||
virshNodeDeviceSorter(const void *a, const void *b)
|
||||
virshNodeDeviceSorter(const void *a,
|
||||
const void *b,
|
||||
void *opaque G_GNUC_UNUSED)
|
||||
{
|
||||
virNodeDevicePtr *na = (virNodeDevicePtr *) a;
|
||||
virNodeDevicePtr *nb = (virNodeDevicePtr *) b;
|
||||
@@ -334,9 +336,10 @@ virshNodeDeviceListCollect(vshControl *ctl,
|
||||
|
||||
finished:
|
||||
/* sort the list */
|
||||
if (list->devices && list->ndevices)
|
||||
qsort(list->devices, list->ndevices,
|
||||
sizeof(*list->devices), virshNodeDeviceSorter);
|
||||
if (list->devices && list->ndevices) {
|
||||
g_qsort_with_data(list->devices, list->ndevices,
|
||||
sizeof(*list->devices), virshNodeDeviceSorter, NULL);
|
||||
}
|
||||
|
||||
/* truncate the list if filter simulation deleted entries */
|
||||
if (deleted)
|
||||
|
||||
@@ -220,7 +220,9 @@ cmdNWFilterDumpXML(vshControl *ctl, const vshCmd *cmd)
|
||||
}
|
||||
|
||||
static int
|
||||
virshNWFilterSorter(const void *a, const void *b)
|
||||
virshNWFilterSorter(const void *a,
|
||||
const void *b,
|
||||
void *opaque G_GNUC_UNUSED)
|
||||
{
|
||||
virNWFilterPtr *fa = (virNWFilterPtr *) a;
|
||||
virNWFilterPtr *fb = (virNWFilterPtr *) b;
|
||||
@@ -323,9 +325,10 @@ virshNWFilterListCollect(vshControl *ctl,
|
||||
|
||||
finished:
|
||||
/* sort the list */
|
||||
if (list->filters && list->nfilters)
|
||||
qsort(list->filters, list->nfilters,
|
||||
sizeof(*list->filters), virshNWFilterSorter);
|
||||
if (list->filters && list->nfilters) {
|
||||
g_qsort_with_data(list->filters, list->nfilters,
|
||||
sizeof(*list->filters), virshNWFilterSorter, NULL);
|
||||
}
|
||||
|
||||
/* truncate the list for not found filter objects */
|
||||
if (deleted)
|
||||
@@ -644,7 +647,9 @@ cmdNWFilterBindingDumpXML(vshControl *ctl, const vshCmd *cmd)
|
||||
|
||||
|
||||
static int
|
||||
virshNWFilterBindingSorter(const void *a, const void *b)
|
||||
virshNWFilterBindingSorter(const void *a,
|
||||
const void *b,
|
||||
void *opaque G_GNUC_UNUSED)
|
||||
{
|
||||
virNWFilterBindingPtr *fa = (virNWFilterBindingPtr *) a;
|
||||
virNWFilterBindingPtr *fb = (virNWFilterBindingPtr *) b;
|
||||
@@ -702,9 +707,10 @@ virshNWFilterBindingListCollect(vshControl *ctl,
|
||||
list->nbindings = ret;
|
||||
|
||||
/* sort the list */
|
||||
if (list->bindings && list->nbindings > 1)
|
||||
qsort(list->bindings, list->nbindings,
|
||||
sizeof(*list->bindings), virshNWFilterBindingSorter);
|
||||
if (list->bindings && list->nbindings > 1) {
|
||||
g_qsort_with_data(list->bindings, list->nbindings,
|
||||
sizeof(*list->bindings), virshNWFilterBindingSorter, NULL);
|
||||
}
|
||||
|
||||
success = true;
|
||||
|
||||
|
||||
@@ -814,7 +814,9 @@ cmdPoolDumpXML(vshControl *ctl, const vshCmd *cmd)
|
||||
}
|
||||
|
||||
static int
|
||||
virshStoragePoolSorter(const void *a, const void *b)
|
||||
virshStoragePoolSorter(const void *a,
|
||||
const void *b,
|
||||
void *opaque G_GNUC_UNUSED)
|
||||
{
|
||||
virStoragePoolPtr *pa = (virStoragePoolPtr *) a;
|
||||
virStoragePoolPtr *pb = (virStoragePoolPtr *) b;
|
||||
@@ -1005,9 +1007,10 @@ virshStoragePoolListCollect(vshControl *ctl,
|
||||
|
||||
finished:
|
||||
/* sort the list */
|
||||
if (list->pools && list->npools)
|
||||
qsort(list->pools, list->npools,
|
||||
sizeof(*list->pools), virshStoragePoolSorter);
|
||||
if (list->pools && list->npools) {
|
||||
g_qsort_with_data(list->pools, list->npools,
|
||||
sizeof(*list->pools), virshStoragePoolSorter, NULL);
|
||||
}
|
||||
|
||||
/* truncate the list if filter simulation deleted entries */
|
||||
if (deleted)
|
||||
|
||||
@@ -399,7 +399,9 @@ cmdSecretUndefine(vshControl *ctl, const vshCmd *cmd)
|
||||
}
|
||||
|
||||
static int
|
||||
virshSecretSorter(const void *a, const void *b)
|
||||
virshSecretSorter(const void *a,
|
||||
const void *b,
|
||||
void *opaque G_GNUC_UNUSED)
|
||||
{
|
||||
virSecretPtr *sa = (virSecretPtr *) a;
|
||||
virSecretPtr *sb = (virSecretPtr *) b;
|
||||
@@ -509,9 +511,10 @@ virshSecretListCollect(vshControl *ctl,
|
||||
|
||||
finished:
|
||||
/* sort the list */
|
||||
if (list->secrets && list->nsecrets)
|
||||
qsort(list->secrets, list->nsecrets,
|
||||
sizeof(*list->secrets), virshSecretSorter);
|
||||
if (list->secrets && list->nsecrets) {
|
||||
g_qsort_with_data(list->secrets, list->nsecrets,
|
||||
sizeof(*list->secrets), virshSecretSorter, NULL);
|
||||
}
|
||||
|
||||
/* truncate the list for not found secret objects */
|
||||
if (deleted)
|
||||
|
||||
@@ -982,7 +982,9 @@ virshSnapshotListFree(struct virshSnapshotList *snaplist)
|
||||
}
|
||||
|
||||
static int
|
||||
virshSnapSorter(const void *a, const void *b)
|
||||
virshSnapSorter(const void *a,
|
||||
const void *b,
|
||||
void *opaque G_GNUC_UNUSED)
|
||||
{
|
||||
const struct virshSnap *sa = a;
|
||||
const struct virshSnap *sb = b;
|
||||
@@ -1232,7 +1234,7 @@ virshSnapshotListCollect(vshControl *ctl, virDomainPtr dom,
|
||||
* still in list. We mark known descendants by clearing
|
||||
* snaps[i].parents. Sorry, this is O(n^3) - hope your
|
||||
* hierarchy isn't huge. XXX Is it worth making O(n^2 log n)
|
||||
* by using qsort and bsearch? */
|
||||
* by using g_qsort_with_data and bsearch? */
|
||||
if (start_index < 0) {
|
||||
vshError(ctl, _("snapshot %1$s disappeared from list"), fromname);
|
||||
goto cleanup;
|
||||
@@ -1312,8 +1314,8 @@ virshSnapshotListCollect(vshControl *ctl, virDomainPtr dom,
|
||||
}
|
||||
if (!(orig_flags & VIR_DOMAIN_SNAPSHOT_LIST_TOPOLOGICAL) &&
|
||||
snaplist->snaps && snaplist->nsnaps) {
|
||||
qsort(snaplist->snaps, snaplist->nsnaps, sizeof(*snaplist->snaps),
|
||||
virshSnapSorter);
|
||||
g_qsort_with_data(snaplist->snaps, snaplist->nsnaps,
|
||||
sizeof(*snaplist->snaps), virshSnapSorter, NULL);
|
||||
}
|
||||
snaplist->nsnaps -= deleted;
|
||||
|
||||
|
||||
@@ -1200,7 +1200,9 @@ cmdVolDumpXML(vshControl *ctl, const vshCmd *cmd)
|
||||
}
|
||||
|
||||
static int
|
||||
virshStorageVolSorter(const void *a, const void *b)
|
||||
virshStorageVolSorter(const void *a,
|
||||
const void *b,
|
||||
void *opaque G_GNUC_UNUSED)
|
||||
{
|
||||
virStorageVolPtr *va = (virStorageVolPtr *) a;
|
||||
virStorageVolPtr *vb = (virStorageVolPtr *) b;
|
||||
@@ -1299,8 +1301,10 @@ virshStorageVolListCollect(vshControl *ctl,
|
||||
|
||||
finished:
|
||||
/* sort the list */
|
||||
if (list->vols && list->nvols)
|
||||
qsort(list->vols, list->nvols, sizeof(*list->vols), virshStorageVolSorter);
|
||||
if (list->vols && list->nvols) {
|
||||
g_qsort_with_data(list->vols, list->nvols,
|
||||
sizeof(*list->vols), virshStorageVolSorter, NULL);
|
||||
}
|
||||
|
||||
if (deleted)
|
||||
VIR_SHRINK_N(list->vols, list->nvols, deleted);
|
||||
|
||||
Reference in New Issue
Block a user