[virt-tools-list] [PATCH 27/47] Convert OS hypervisor sections from GTree to GHashTable

Daniel P. Berrange berrange at redhat.com
Wed Aug 25 19:37:22 UTC 2010


Remove another GTree data structure in favour of GHashTable
for simpler, more efficient code

* osinfo/osinfo_common.h: Change OsinfoOsPrivate struct
  to use a GHashTable
* osinfo/osinfo_common.c, osinfo/osinfo_dataread.c,
  osinfo/osinfo_device.c, osinfo/osinfo_os.c: Update to
  use hash table APIs
---
 osinfo/osinfo_common.c   |   10 ----------
 osinfo/osinfo_common.h   |    5 ++---
 osinfo/osinfo_dataread.c |    9 ++++-----
 osinfo/osinfo_device.c   |    2 +-
 osinfo/osinfo_os.c       |   33 +++++++++++++++++++++------------
 5 files changed, 28 insertions(+), 31 deletions(-)

diff --git a/osinfo/osinfo_common.c b/osinfo/osinfo_common.c
index b9751b5..3db1073 100644
--- a/osinfo/osinfo_common.c
+++ b/osinfo/osinfo_common.c
@@ -150,13 +150,3 @@ void __osinfoFreeOsLink(gpointer ptr)
     struct __osinfoOsLink *osLink = (struct __osinfoOsLink *) ptr;
     g_free(osLink);
 }
-
-void __osinfoFreeHvSection(gpointer ptr)
-{
-    if (!ptr)
-        return;
-    struct __osinfoHvSection * hvSection = (struct __osinfoHvSection *) ptr;
-    g_tree_destroy(hvSection->sections);
-    g_tree_destroy(hvSection->sectionsAsList);
-    g_free(hvSection);
-}
diff --git a/osinfo/osinfo_common.h b/osinfo/osinfo_common.h
index c414544..779ec89 100644
--- a/osinfo/osinfo_common.h
+++ b/osinfo/osinfo_common.h
@@ -85,7 +85,6 @@ void __osinfoFreePtrArray(gpointer ptrarray);
 void __osinfoFreeRelationship(gpointer ptrarray);
 void __osinfoFreeParamVals(gpointer ptrarray);
 void __osinfoFreeDeviceSection(gpointer tree);
-void __osinfoFreeHvSection(gpointer hvSection);
 void __osinfoFreeDeviceLink(gpointer ptr);
 void __osinfoFreeOsLink(gpointer ptr);
 
@@ -113,8 +112,8 @@ struct _OsinfoOsPrivate
 {
     // OS-Hypervisor specific information
     // Key: gchar* (hypervisor id)
-    // Value: hv_section struct
-    GTree *hypervisors;
+    // Value: __osinfoHvSection struct
+    GHashTable *hypervisors;
 
     // Key: gchar* (device type)
     // Value: Tree of device_link structs (multiple devices per type)
diff --git a/osinfo/osinfo_dataread.c b/osinfo/osinfo_dataread.c
index b212852..b10227d 100644
--- a/osinfo/osinfo_dataread.c
+++ b/osinfo/osinfo_dataread.c
@@ -73,7 +73,7 @@ static gboolean __osinfoResolveSectionDevices(gpointer key, gpointer value, gpoi
     return FALSE;
 }
 
-static gboolean __osinfoResolveHvLink(gpointer key, gpointer value, gpointer data)
+static void __osinfoResolveHvLink(gpointer key, gpointer value, gpointer data)
 {
     gchar *hvId = (gchar *) key;
     struct __osinfoDbRet *dbRet = (struct __osinfoDbRet *) data;
@@ -85,17 +85,16 @@ static gboolean __osinfoResolveHvLink(gpointer key, gpointer value, gpointer dat
 
     g_tree_foreach(hvSection->sections, __osinfoResolveSectionDevices, dbRet);
     if (*ret)
-        return TRUE;
+        return;
 
     hv = OSINFO_HYPERVISOR(osinfo_list_find_by_id(OSINFO_LIST(hypervisors), hvId));
     if (!hv) {
         *ret = -EINVAL;
-        return TRUE;
+        return;
     }
 
     hvSection->hv = hv;
     *ret = 0;
-    return FALSE;
 }
 
 static gboolean __osinfoResolveOsLink(gpointer key, gpointer value, gpointer data)
@@ -137,7 +136,7 @@ static gboolean __osinfoFixOsLinks(OsinfoList *list, OsinfoEntity *entity, gpoin
     if (*ret)
         return TRUE;
 
-    g_tree_foreach(os->priv->hypervisors, __osinfoResolveHvLink, dbRet);
+    g_hash_table_foreach(os->priv->hypervisors, __osinfoResolveHvLink, dbRet);
     if (*ret)
         return TRUE;
 
diff --git a/osinfo/osinfo_device.c b/osinfo/osinfo_device.c
index d9f84bd..b7d4cd5 100644
--- a/osinfo/osinfo_device.c
+++ b/osinfo/osinfo_device.c
@@ -57,7 +57,7 @@ gchar *osinfo_device_get_driver(OsinfoDevice *self,
 
     // For os, get hypervisor specific info. If not present, return NULL.
     struct __osinfoHvSection *hvSection = NULL;
-    hvSection = g_tree_lookup(os->priv->hypervisors, (OSINFO_ENTITY(hv))->priv->id);
+    hvSection = g_hash_table_lookup(os->priv->hypervisors, (OSINFO_ENTITY(hv))->priv->id);
     if (!hvSection)
         return NULL;
 
diff --git a/osinfo/osinfo_os.c b/osinfo/osinfo_os.c
index cedeaac..8cf4615 100644
--- a/osinfo/osinfo_os.c
+++ b/osinfo/osinfo_os.c
@@ -13,7 +13,7 @@ osinfo_os_finalize (GObject *object)
 
     g_tree_destroy (self->priv->sections);
     g_tree_destroy (self->priv->sectionsAsList);
-    g_tree_destroy (self->priv->hypervisors);
+    g_hash_table_unref(self->priv->hypervisors);
     g_tree_destroy (self->priv->relationshipsByOs);
     g_tree_destroy (self->priv->relationshipsByType);
 
@@ -31,6 +31,16 @@ osinfo_os_class_init (OsinfoOsClass *klass)
     g_type_class_add_private (klass, sizeof (OsinfoOsPrivate));
 }
 
+static void osinfo_hv_section_free(gpointer value)
+{
+    struct __osinfoHvSection * hvSection = value;
+    if (!hvSection)
+        return;
+    g_tree_destroy(hvSection->sections);
+    g_tree_destroy(hvSection->sectionsAsList);
+    g_free(hvSection);
+}
+
 static void
 osinfo_os_init (OsinfoOs *self)
 {
@@ -53,10 +63,10 @@ osinfo_os_init (OsinfoOs *self)
                                                 __osinfoFreeRelationship);
     self->priv->relationshipsByType = g_tree_new(__osinfoIntCompareBase);
 
-    self->priv->hypervisors = g_tree_new_full(__osinfoStringCompare,
-                                              NULL,
-                                              g_free,
-                                              __osinfoFreeHvSection);
+    self->priv->hypervisors = g_hash_table_new_full(g_str_hash,
+						    g_str_equal,
+						    g_free,
+						    osinfo_hv_section_free);
 }
 
 OsinfoOs *osinfo_os_new(const gchar *id)
@@ -190,14 +200,12 @@ struct __osinfoHvSection *__osinfoAddHypervisorSectionToOs(OsinfoOs *self, gchar
     gboolean found;
     gpointer origKey, foundValue;
     struct __osinfoHvSection *hvSection = NULL;
-    gchar *hvIdDup = NULL;
     GTree *deviceSections;
     GTree *deviceSectionsAsList;
 
-    found = g_tree_lookup_extended(self->priv->hypervisors, hvId, &origKey, &foundValue);
+    found = g_hash_table_lookup_extended(self->priv->hypervisors, hvId, &origKey, &foundValue);
     if (!found) {
         hvSection = g_malloc(sizeof(*hvSection));
-        hvIdDup = g_strdup(hvId);
         deviceSections = g_tree_new_full(__osinfoStringCompare,
                                         NULL,
                                         g_free,
@@ -214,7 +222,8 @@ struct __osinfoHvSection *__osinfoAddHypervisorSectionToOs(OsinfoOs *self, gchar
         hvSection->sections = deviceSections;
         hvSection->sectionsAsList = deviceSectionsAsList;
 
-        g_tree_insert(self->priv->hypervisors, hvIdDup, hvSection);
+        g_hash_table_insert(self->priv->hypervisors,
+			    g_strdup(hvId), hvSection);
         return hvSection;
     }
     else
@@ -223,7 +232,7 @@ struct __osinfoHvSection *__osinfoAddHypervisorSectionToOs(OsinfoOs *self, gchar
 
 void __osinfoRemoveHvSectionFromOs(OsinfoOs *self, gchar *hvId)
 {
-    g_tree_remove(self->priv->hypervisors, hvId);
+    g_hash_table_remove(self->priv->hypervisors, hvId);
 }
 
 OsinfoDevice *osinfo_os_get_preferred_device(OsinfoOs *self, OsinfoHypervisor *hv, gchar *devType, OsinfoFilter *filter)
@@ -238,7 +247,7 @@ OsinfoDevice *osinfo_os_get_preferred_device(OsinfoOs *self, OsinfoHypervisor *h
     if (hv) {
         // Check if hypervisor specific info present for Os, else return NULL.
         struct __osinfoHvSection *hvSection = NULL;
-        hvSection = g_tree_lookup(self->priv->hypervisors, (OSINFO_ENTITY(hv))->priv->id);
+        hvSection = g_hash_table_lookup(self->priv->hypervisors, (OSINFO_ENTITY(hv))->priv->id);
         if (!hvSection)
             return NULL;
 
@@ -300,7 +309,7 @@ OsinfoDeviceList *osinfo_os_get_devices(OsinfoOs *self, OsinfoHypervisor *hv, gc
 
     if (hv) {
         struct __osinfoHvSection *hvSection = NULL;
-        hvSection = g_tree_lookup(self->priv->hypervisors, (OSINFO_ENTITY(hv))->priv->id);
+        hvSection = g_hash_table_lookup(self->priv->hypervisors, (OSINFO_ENTITY(hv))->priv->id);
         if (!hvSection)
             return newList;
 
-- 
1.7.2.1




More information about the virt-tools-list mailing list