Skip to contentSkip to conversation

ui: Use g_new() & friends where that makes obvious sense

PATCH In review Markus Armbruster 2 participants 3 messages Sep 14, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ui: Use g_new() & friends where that makes obvious sense

g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer, for two reasons. One, it catches multiplication overflowing size_t. Two, it returns T * rather than void *, which lets the compiler catch more type errors.

This commit only touches allocations with size arguments of the form sizeof(T). Same Coccinelle semantic patch as in commit b45c03f.

---
 ui/console.c      | 2 +-
 ui/curses.c       | 2 +-
 ui/input-legacy.c | 4 ++--
 ui/keymaps.c      | 2 +-
 ui/sdl.c          | 2 +-
 ui/vnc-jobs.c     | 6 +++---
 ui/vnc.c          | 6 +++---
 7 files changed, 12 insertions(+), 12 deletions(-)
ui/console.c +1 -1
index 75fc492..6edda1e 100644
@@ -449,7 +449,7 @@ static void text_console_resize(QemuConsole *s)
     if (s->width < w1)
         w1 = s->width;
 
-    cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
+    cells = g_new(TextCell, s->width * s->total_height);
     for(y = 0; y < s->total_height; y++) {
         c = &cells[y * s->width];
         if (w1 > 0) {
ui/curses.c +1 -1
index 8edb038..db83188 100644
@@ -382,7 +382,7 @@ void curses_display_init(DisplayState *ds, int full_screen)
 
     curses_winch_init();
 
-    dcl = (DisplayChangeListener *) g_malloc0(sizeof(DisplayChangeListener));
+    dcl = g_new0(DisplayChangeListener, 1);
     dcl->ops = &dcl_ops;
     register_displaychangelistener(dcl);
 
ui/input-legacy.c +2 -2
index e50f296..c5f173d 100644
@@ -205,7 +205,7 @@ QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
 {
     QEMUPutMouseEntry *s;
 
-    s = g_malloc0(sizeof(QEMUPutMouseEntry));
+    s = g_new0(QEMUPutMouseEntry, 1);
 
     s->qemu_put_mouse_event = func;
     s->qemu_put_mouse_event_opaque = opaque;
@@ -239,7 +239,7 @@ QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
 {
     QEMUPutLEDEntry *s;
 
-    s = g_malloc0(sizeof(QEMUPutLEDEntry));
+    s = g_new0(QEMUPutLEDEntry, 1);
 
     s->put_led = func;
     s->opaque = opaque;
ui/keymaps.c +1 -1
index 49410ae..1b9ba3f 100644
@@ -109,7 +109,7 @@ static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
     }
 
     if (!k) {
-        k = g_malloc0(sizeof(kbd_layout_t));
+        k = g_new0(kbd_layout_t, 1);
     }
 
     for(;;) {
ui/sdl.c +1 -1
index 3be2910..570cb99 100644
@@ -985,7 +985,7 @@ void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
         sdl_grab_start();
     }
 
-    dcl = g_malloc0(sizeof(DisplayChangeListener));
+    dcl = g_new0(DisplayChangeListener, 1);
     dcl->ops = &dcl_ops;
     register_displaychangelistener(dcl);
 
ui/vnc-jobs.c +3 -3
index 22c9abc..9512b87 100644
@@ -79,7 +79,7 @@ static void vnc_unlock_queue(VncJobQueue *queue)
 
 VncJob *vnc_job_new(VncState *vs)
 {
-    VncJob *job = g_malloc0(sizeof(VncJob));
+    VncJob *job = g_new0(VncJob, 1);
 
     job->vs = vs;
     vnc_lock_queue(queue);
@@ -90,7 +90,7 @@ VncJob *vnc_job_new(VncState *vs)
 
 int vnc_job_add_rect(VncJob *job, int x, int y, int w, int h)
 {
-    VncRectEntry *entry = g_malloc0(sizeof(VncRectEntry));
+    VncRectEntry *entry = g_new0(VncRectEntry, 1);
 
     entry->rect.x = x;
     entry->rect.y = y;
@@ -298,7 +298,7 @@ disconnected:
 
 static VncJobQueue *vnc_queue_init(void)
 {
-    VncJobQueue *queue = g_malloc0(sizeof(VncJobQueue));
+    VncJobQueue *queue = g_new0(VncJobQueue, 1);
 
     qemu_cond_init(&queue->cond);
     qemu_mutex_init(&queue->mutex);
ui/vnc.c +3 -3
index caf82f5..94df777 100644
@@ -170,7 +170,7 @@ static VncBasicInfo *vnc_basic_info_get(struct sockaddr_storage *sa,
         return NULL;
     }
 
-    info = g_malloc0(sizeof(VncBasicInfo));
+    info = g_new0(VncBasicInfo, 1);
     info->host = g_strdup(host);
     info->service = g_strdup(serv);
     info->family = inet_netfamily(sa->ss_family);
@@ -3002,7 +3002,7 @@ static void vnc_refresh(DisplayChangeListener *dcl)
 static void vnc_connect(VncDisplay *vd, int csock,
                         bool skipauth, bool websocket)
 {
-    VncState *vs = g_malloc0(sizeof(VncState));
+    VncState *vs = g_new0(VncState, 1);
     int i;
 
     vs->csock = csock;
@@ -3025,7 +3025,7 @@ static void vnc_connect(VncDisplay *vd, int csock,
 
     vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect));
     for (i = 0; i < VNC_STAT_ROWS; ++i) {
-        vs->lossy_rect[i] = g_malloc0(VNC_STAT_COLS * sizeof (uint8_t));
+        vs->lossy_rect[i] = g_new0(uint8_t, VNC_STAT_COLS);
     }
 
     VNC_DEBUG("New client on socket %d\n", csock);
Signature · 1 line
2.4.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Markus Armbruster

g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer, for two reasons. One, it catches multiplication overflowing size_t. Two, it returns T * rather than void *, which lets the compiler catch more type errors.

This commit only touches allocations with size arguments of the form sizeof(T). Same Coccinelle semantic patch as in commit b45c03f.

Quoted 9 lines
---
 ui/console.c      | 2 +-
 ui/curses.c       | 2 +-
 ui/input-legacy.c | 4 ++--
 ui/keymaps.c      | 2 +-
 ui/sdl.c          | 2 +-
 ui/vnc-jobs.c     | 6 +++---
 ui/vnc.c          | 6 +++---
 7 files changed, 12 insertions(+), 12 deletions(-)

ui/console.c static void text_console_resize(QemuConsole *s) · line 452

Quoted 11 lines
ui/console.c +1 -1
index 75fc492..6edda1e 100644
@@ -449,7 +449,7 @@ static void text_console_resize(QemuConsole *s)
     if (s->width < w1)
         w1 = s->width;
 
-    cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
+    cells = g_new(TextCell, s->width * s->total_height);

Hopefully s->width * s->total_height can't overflow.

ui/console.c static void vnc_connect(VncDisplay *vd, int csock, · line 3028

Quoted 6 lines
ui/console.c +1 -1
@@ -3025,7 +3025,7 @@ static void vnc_connect(VncDisplay *vd, int csock,
 
     vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect));
     for (i = 0; i < VNC_STAT_ROWS; ++i) {
-        vs->lossy_rect[i] = g_malloc0(VNC_STAT_COLS * sizeof (uint8_t));
+        vs->lossy_rect[i] = g_new0(uint8_t, VNC_STAT_COLS);

sizeof(uint8_t) == 1, according to POSIX. This could be further simplified to g_malloc0(VNC_STAT_COLS). But if someone wants to do that simplification, it should be a separate patch.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
Quoted 31 lines · Eric Blake
Quoted 9 lines · Markus Armbruster

g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer, for two reasons. One, it catches multiplication overflowing size_t. Two, it returns T * rather than void *, which lets the compiler catch more type errors.

This commit only touches allocations with size arguments of the form sizeof(T). Same Coccinelle semantic patch as in commit b45c03f.

Quoted 9 lines
---
 ui/console.c      | 2 +-
 ui/curses.c       | 2 +-
 ui/input-legacy.c | 4 ++--
 ui/keymaps.c      | 2 +-
 ui/sdl.c          | 2 +-
 ui/vnc-jobs.c     | 6 +++---
 ui/vnc.c          | 6 +++---
 7 files changed, 12 insertions(+), 12 deletions(-)
Quoted 9 lines
ui/console.c +0 -0
index 75fc492..6edda1e 100644
@@ -449,7 +449,7 @@ static void text_console_resize(QemuConsole *s)
     if (s->width < w1)
         w1 = s->width;
 
Quoted 4 lines

ui/console.c static void text_console_resize(QemuConsole *s) · line 452

Quoted 2 lines
ui/console.c +1 -1
-    cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
+    cells = g_new(TextCell, s->width * s->total_height);

Hopefully s->width * s->total_height can't overflow.

Two billion is a helluva lot of character cells :)

Quoted 10 lines

ui/console.c static void vnc_connect(VncDisplay *vd, int csock, · line 3028

Quoted 6 lines
ui/console.c +1 -1
@@ -3025,7 +3025,7 @@ static void vnc_connect(VncDisplay *vd, int csock,
 
     vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect));
     for (i = 0; i < VNC_STAT_ROWS; ++i) {
-        vs->lossy_rect[i] = g_malloc0(VNC_STAT_COLS * sizeof (uint8_t));
+        vs->lossy_rect[i] = g_new0(uint8_t, VNC_STAT_COLS);

sizeof(uint8_t) == 1, according to POSIX. This could be further simplified to g_malloc0(VNC_STAT_COLS). But if someone wants to do that simplification, it should be a separate patch.

g_new0(uint8_t, VNC_STAT_COLS) returns uint8_t *.

g_malloc0(VNC_STAT_COLS) returns void *.

Transforming the former to the latter loses a bit of type checking.