Skip to contentSkip to conversation

plugins: Freeing allocated values in hash tables.

PATCH Applied Mahmoud Mandour 2 participants 4 messages Apr 22, 2021 – Apr 26, 2021

lore
Share & tools
lore
#0 lore raw

v2 cover plugins: Freeing allocated values in hash tables.

A hash table made using ``g_hash_table_new`` requires manually freeing any dynamically allocated keys/values. The two patches in this series fixes this issue in hotblocks and hotpages plugins.

v1 -> v2: Added a freeing function to hotpages instead of freeing
the sorted list. That's probably better because the sorted list is only made on having ``counts != NULL`` and ``counts`` has a next pointer so essentially at least 2 elements in the list.

Mahmoud Mandour (2):
plugins/hotblocks: Properly freed the hash table values plugins/hotpages: Properly freed the hash table values

 contrib/plugins/hotblocks.c | 3 ++-
 contrib/plugins/hotpages.c  | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/2 plugins/hotblocks: Properly freed the hash table values

Freed the values stored in the hash table ``hotblocks`` returned by ``g_hash_table_get_values()`` by freeing the sorted list and destroyed the hash table afterward.

---
 contrib/plugins/hotblocks.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
contrib/plugins/hotblocks.c +2 -1
index 4b08340143..64692c0670 100644
@@ -68,10 +68,11 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
                                    rec->insns, rec->exec_count);
         }
 
-        g_list_free(it);
+        g_list_free_full(it, g_free);
         g_mutex_unlock(&lock);
     }
 
+    g_hash_table_destroy(hotblocks);
     qemu_plugin_outs(report->str);
 }
 
Signature · 1 line
2.25.1
#2 lore raw

PATCH v2 2/2 plugins/hotpages: Properly freed the hash table values

In reply to Mahmoud Mandour

Allocated ``pages`` hash table through ``g_hash_table_new_full`` to add a freeing function & destroyed the hash table on exit.

---
 contrib/plugins/hotpages.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
contrib/plugins/hotpages.c +2 -1
index bf53267532..9cf7f02c77 100644
@@ -97,13 +97,14 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
         g_list_free(it);
     }
 
+    g_hash_table_destroy(pages);
     qemu_plugin_outs(report->str);
 }
 
 static void plugin_init(void)
 {
     page_mask = (page_size - 1);
-    pages = g_hash_table_new(NULL, g_direct_equal);
+    pages = g_hash_table_new_full(NULL, g_direct_equal, NULL, g_free);
 }
 
 static void vcpu_haddr(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo,
Signature · 1 line
2.25.1
Applied
#3 lore raw
In reply to Mahmoud Mandour
Quoted 8 lines · Mahmoud Mandour

A hash table made using ``g_hash_table_new`` requires manually freeing any dynamically allocated keys/values. The two patches in this series fixes this issue in hotblocks and hotpages plugins.

v1 -> v2: Added a freeing function to hotpages instead of freeing
the sorted list. That's probably better because the sorted list is only made on having ``counts != NULL`` and ``counts`` has a next pointer so essentially at least 2 elements in the list.

Queued to plugins/next, thanks.

Signature · 1 line
Alex Bennée