[Spice-devel] [PATCH spice-server v3 4/6] Use g_strdup_printf instead of manually malloc/snprintf

Frediano Ziglio fziglio at redhat.com
Wed Nov 16 15:39:14 UTC 2016


Make the code easier and shorter.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/stat-file.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/server/stat-file.c b/server/stat-file.c
index 7e35db0..e9b4208 100644
--- a/server/stat-file.c
+++ b/server/stat-file.c
@@ -36,14 +36,11 @@
 
 void stat_file_init(RedStatFile *stat_file, unsigned int max_nodes)
 {
-    int shm_name_len;
     int fd;
     size_t shm_size = STAT_SHM_SIZE(max_nodes);
 
     stat_file->max_nodes = max_nodes;
-    shm_name_len = strlen(SPICE_STAT_SHM_NAME) + 20;
-    stat_file->shm_name = (char *)spice_malloc(shm_name_len);
-    snprintf(stat_file->shm_name, shm_name_len, SPICE_STAT_SHM_NAME, getpid());
+    stat_file->shm_name = g_strdup_printf(SPICE_STAT_SHM_NAME, getpid());
     shm_unlink(stat_file->shm_name);
     if ((fd = shm_open(stat_file->shm_name, O_CREAT | O_RDWR, 0444)) == -1) {
         spice_error("statistics shm_open failed, %s", strerror(errno));
@@ -68,7 +65,7 @@ void stat_file_unlink(RedStatFile *stat_file)
 {
     if (stat_file->shm_name) {
         shm_unlink(stat_file->shm_name);
-        free(stat_file->shm_name);
+        g_free(stat_file->shm_name);
         stat_file->shm_name = NULL;
     }
 }
-- 
2.7.4



More information about the Spice-devel mailing list