[PATCH wayland 3/6] Use zalloc instead of malloc + memset

Jonas Ådahl jadahl at gmail.com
Mon Jan 11 20:19:12 PST 2016


On Mon, Jan 11, 2016 at 10:50:36AM +0100, Marek Chalupa wrote:
> Hi,
> 
> On 12/29/2015 03:10 AM, Jonas Ådahl wrote:
> >Signed-off-by: Jonas Ådahl <jadahl at gmail.com>
> >---
> >  src/connection.c      |  4 ++--
> >  src/wayland-client.c  | 12 +++---------
> >  src/wayland-private.h |  6 ++++++
> >  src/wayland-server.c  |  6 ++----
> >  src/wayland-shm.c     |  4 +---
> >  5 files changed, 14 insertions(+), 18 deletions(-)
> >
> >diff --git a/src/connection.c b/src/connection.c
> >index 6742f19..45a2e78 100644
> >--- a/src/connection.c
> >+++ b/src/connection.c
> >@@ -163,10 +163,10 @@ wl_connection_create(int fd)
> >  {
> >  	struct wl_connection *connection;
> >
> >-	connection = malloc(sizeof *connection);
> >+	connection = zalloc(sizeof *connection);
> >  	if (connection == NULL)
> >  		return NULL;
> >-	memset(connection, 0, sizeof *connection);
> >+
> >  	connection->fd = fd;
> >
> >  	return connection;
> >diff --git a/src/wayland-client.c b/src/wayland-client.c
> >index 509be08..613767e 100644
> >--- a/src/wayland-client.c
> >+++ b/src/wayland-client.c
> >@@ -331,12 +331,10 @@ proxy_create(struct wl_proxy *factory, const struct wl_interface *interface)
> >  	struct wl_proxy *proxy;
> >  	struct wl_display *display = factory->display;
> >
> >-	proxy = malloc(sizeof *proxy);
> >+	proxy = zalloc(sizeof *proxy);
> >  	if (proxy == NULL)
> >  		return NULL;
> >
> >-	memset(proxy, 0, sizeof *proxy);
> >-
> >  	proxy->object.interface = interface;
> >  	proxy->display = display;
> >  	proxy->queue = factory->queue;
> >@@ -387,12 +385,10 @@ wl_proxy_create_for_id(struct wl_proxy *factory,
> >  	struct wl_proxy *proxy;
> >  	struct wl_display *display = factory->display;
> >
> >-	proxy = malloc(sizeof *proxy);
> >+	proxy = zalloc(sizeof *proxy);
> >  	if (proxy == NULL)
> >  		return NULL;
> >
> >-	memset(proxy, 0, sizeof *proxy);
> >-
> >  	proxy->object.interface = interface;
> >  	proxy->object.id = id;
> >  	proxy->display = display;
> >@@ -817,14 +813,12 @@ wl_display_connect_to_fd(int fd)
> >  	if (debug && (strstr(debug, "client") || strstr(debug, "1")))
> >  		debug_client = 1;
> >
> >-	display = malloc(sizeof *display);
> >+	display = zalloc(sizeof *display);
> >  	if (display == NULL) {
> >  		close(fd);
> >  		return NULL;
> >  	}
> >
> >-	memset(display, 0, sizeof *display);
> >-
> >  	display->fd = fd;
> >  	wl_map_init(&display->objects, WL_MAP_CLIENT_SIDE);
> >  	wl_event_queue_init(&display->default_queue, display);
> >diff --git a/src/wayland-private.h b/src/wayland-private.h
> >index 58ac952..786b3a0 100644
> >--- a/src/wayland-private.h
> >+++ b/src/wayland-private.h
> >@@ -216,4 +216,10 @@ struct wl_display;
> >  struct wl_array *
> >  wl_display_get_additional_shm_formats(struct wl_display *display);
> >
> >+static inline void *
> >+zalloc(size_t s)
> >+{
> >+	return calloc(s, 1);
> >+}
> 
> Shouldn't it be calloc(1,s)? Man says:
> 
> void *calloc(size_t nmemb, size_t size);
> 
> I see this is the same definition as in scanner.c ...
> Also compiler complains about implicit declaration of calloc()
> 
> Maybe we could use zalloc even in tests/test-compositor.c?

Thanks for spotting the issues. I fixed zalloc definition in
wayland-private.h (it was copied from the scanner actually). I suppose
it would be good to have a single definition that can be used also by
the tests, but we don't have such a header file yet.


Jonas

> 
> otherwise
> Reviewed-by: Marek Chalupa <mchqwerty at gmail.com>
> 
> Regards,
> Marek
> 
> >+
> >  #endif
> >diff --git a/src/wayland-server.c b/src/wayland-server.c
> >index 55c0cf9..9e26b18 100644
> >--- a/src/wayland-server.c
> >+++ b/src/wayland-server.c
> >@@ -415,11 +415,10 @@ wl_client_create(struct wl_display *display, int fd)
> >  	struct wl_client *client;
> >  	socklen_t len;
> >
> >-	client = malloc(sizeof *client);
> >+	client = zalloc(sizeof *client);
> >  	if (client == NULL)
> >  		return NULL;
> >
> >-	memset(client, 0, sizeof *client);
> >  	client->display = display;
> >  	client->source = wl_event_loop_add_fd(display->loop, fd,
> >  					      WL_EVENT_READABLE,
> >@@ -854,11 +853,10 @@ wl_socket_alloc(void)
> >  {
> >  	struct wl_socket *s;
> >
> >-	s = malloc(sizeof *s);
> >+	s = zalloc(sizeof *s);
> >  	if (!s)
> >  		return NULL;
> >
> >-	memset(s, 0, sizeof *s);
> >  	s->fd = -1;
> >  	s->fd_lock = -1;
> >
> >diff --git a/src/wayland-shm.c b/src/wayland-shm.c
> >index 0cd8c11..359c3bd 100644
> >--- a/src/wayland-shm.c
> >+++ b/src/wayland-shm.c
> >@@ -536,12 +536,10 @@ wl_shm_buffer_begin_access(struct wl_shm_buffer *buffer)
> >
> >  	sigbus_data = pthread_getspecific(wl_shm_sigbus_data_key);
> >  	if (sigbus_data == NULL) {
> >-		sigbus_data = malloc(sizeof *sigbus_data);
> >+		sigbus_data = zalloc(sizeof *sigbus_data);
> >  		if (sigbus_data == NULL)
> >  			return;
> >
> >-		memset(sigbus_data, 0, sizeof *sigbus_data);
> >-
> >  		pthread_setspecific(wl_shm_sigbus_data_key, sigbus_data);
> >  	}
> >
> >


More information about the wayland-devel mailing list