[Spice-devel] [PATCH] Prefer malloc() over calloc()

Zeeshan Ali (Khattak) zeeshanak at gnome.org
Wed May 18 13:38:33 PDT 2011


When only 1 element needs to be allocated, calloc() only makes the code
*slightly* less readable.
---
 src/console-kit.c         |    2 +-
 src/udscs.c               |    6 +++---
 src/vdagent-virtio-port.c |    2 +-
 src/vdagent-x11.c         |    2 +-
 src/vdagentd-uinput.c     |    2 +-
 src/vdagentd.c            |    4 ++--
 6 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/console-kit.c b/src/console-kit.c
index eb15939..5ec3643 100644
--- a/src/console-kit.c
+++ b/src/console-kit.c
@@ -43,7 +43,7 @@ struct console_kit *console_kit_create(FILE *errfile)
     DBusError error;
     char match[1024];
 
-    ck = calloc(1, sizeof(*ck));
+    ck = malloc(sizeof(*ck));
     if (!ck)
         return NULL;
 
diff --git a/src/udscs.c b/src/udscs.c
index c451855..52ef17c 100644
--- a/src/udscs.c
+++ b/src/udscs.c
@@ -94,7 +94,7 @@ struct udscs_server *udscs_create_server(const char *socketname,
     struct sockaddr_un address;
     struct udscs_server *server;
 
-    server = calloc(1, sizeof(*server));
+    server = malloc(sizeof(*server));
     if (!server)
         return NULL;
 
@@ -161,7 +161,7 @@ struct udscs_connection *udscs_connect(const char *socketname,
     struct sockaddr_un address;
     struct udscs_connection *conn;
 
-    conn = calloc(1, sizeof(*conn));
+    conn = malloc(sizeof(*conn));
     if (!conn)
         return NULL;
 
@@ -287,7 +287,7 @@ static void udscs_server_accept(struct udscs_server *server) {
         return;
     }
 
-    new_conn = calloc(1, sizeof(*conn));
+    new_conn = malloc(sizeof(*conn));
     if (!new_conn) {
         fprintf(server->errfile, "out of memory, disconnecting new client\n");
         close(fd);
diff --git a/src/vdagent-virtio-port.c b/src/vdagent-virtio-port.c
index 84e2e67..5fc5f0a 100644
--- a/src/vdagent-virtio-port.c
+++ b/src/vdagent-virtio-port.c
@@ -79,7 +79,7 @@ struct vdagent_virtio_port *vdagent_virtio_port_create(const char *portname,
 {
     struct vdagent_virtio_port *vport;
 
-    vport = calloc(1, sizeof(*vport));
+    vport = malloc(sizeof(*vport));
     if (!vport)
         return 0;
 
diff --git a/src/vdagent-x11.c b/src/vdagent-x11.c
index 795ed41..bee9676 100644
--- a/src/vdagent-x11.c
+++ b/src/vdagent-x11.c
@@ -173,7 +173,7 @@ struct vdagent_x11 *vdagent_x11_create(struct udscs_connection *vdagentd,
     XWindowAttributes attrib;
     int i, j, major, minor;
 
-    x11 = calloc(1, sizeof(*x11));
+    x11 = malloc(sizeof(*x11));
     if (!x11) {
         fprintf(errfile, "out of memory allocating vdagent_x11 struct\n");
         return NULL;
diff --git a/src/vdagentd-uinput.c b/src/vdagentd-uinput.c
index 54cccb0..e675bb8 100644
--- a/src/vdagentd-uinput.c
+++ b/src/vdagentd-uinput.c
@@ -45,7 +45,7 @@ struct vdagentd_uinput *vdagentd_uinput_create(const char *devname,
 {
     struct vdagentd_uinput *uinput;
 
-    uinput = calloc(1, sizeof(*uinput));
+    uinput = malloc(sizeof(*uinput));
     if (!uinput)
         return NULL;
 
diff --git a/src/vdagentd.c b/src/vdagentd.c
index be6ef1f..297d5f4 100644
--- a/src/vdagentd.c
+++ b/src/vdagentd.c
@@ -82,7 +82,7 @@ static void send_capabilities(struct vdagent_virtio_port *vport,
     uint32_t size;
 
     size = sizeof(*caps) + VD_AGENT_CAPS_BYTES;
-    caps = calloc(1, size);
+    caps = malloc(size);
     if (!caps) {
         fprintf(logfile,
                 "out of memory allocating capabilities array (write)\n");
@@ -495,7 +495,7 @@ void agent_connect(struct udscs_connection *conn)
     uint32_t pid;
     struct agent_data *agent_data;
     
-    agent_data = calloc(1, sizeof(*agent_data));
+    agent_data = malloc(sizeof(*agent_data));
     if (!agent_data) {
         fprintf(logfile, "Out of memory allocating agent data, disconnecting\n");
         udscs_destroy_connection(&conn);
-- 
1.7.5.1



More information about the Spice-devel mailing list