clients: Simplify memory allocation with xzalloc()

It is redundant to check x*alloc's return value for null pointers, since
they are guaranteed to either return non-NULL or terminate the program.

In cases where we memset the malloc'd memory to 0, we can more
efficiently use the xzalloc() routine.  zalloc looks for opportunities
to return memory chunks that have already been zero'd out, so it can
provide better performance.

This patch addresses this warning, reported by Denis Denisov:

  [clients/window.c:1164] -> [clients/window.c:1166]: (warning) Possible
  null pointer dereference: surface - otherwise it is redundant to check
  it against null.

  [clients/window.c:4513] -> [clients/window.c:4514]: (warning) Possible
  null pointer dereference: surface - otherwise it is redundant to check
  it against null.

Signed-off-by: Bryce Harrington <bryce@osg.samsung.com>
Reviewed-by: Marek Chalupa <mchqwerty@gmail.com>
dev
Bryce Harrington 9 years ago
parent 430aee1c23
commit da9d8fa3e4
  1. 13
      clients/window.c

@ -1162,12 +1162,7 @@ shm_surface_create(struct display *display, struct wl_surface *wl_surface,
struct shm_surface *surface; struct shm_surface *surface;
DBG_OBJ(wl_surface, "\n"); DBG_OBJ(wl_surface, "\n");
surface = xmalloc(sizeof *surface); surface = xzalloc(sizeof *surface);
memset(surface, 0, sizeof *surface);
if (!surface)
return NULL;
surface->base.prepare = shm_surface_prepare; surface->base.prepare = shm_surface_prepare;
surface->base.swap = shm_surface_swap; surface->base.swap = shm_surface_swap;
surface->base.acquire = shm_surface_acquire; surface->base.acquire = shm_surface_acquire;
@ -4511,11 +4506,7 @@ surface_create(struct window *window)
struct display *display = window->display; struct display *display = window->display;
struct surface *surface; struct surface *surface;
surface = xmalloc(sizeof *surface); surface = xzalloc(sizeof *surface);
memset(surface, 0, sizeof *surface);
if (!surface)
return NULL;
surface->window = window; surface->window = window;
surface->surface = wl_compositor_create_surface(display->compositor); surface->surface = wl_compositor_create_surface(display->compositor);
surface->buffer_scale = 1; surface->buffer_scale = 1;

Loading…
Cancel
Save