[PATCH 1/2] clients & tests: Unify multiple definitions of x*alloc and related functions

Bryce Harrington bryce at osg.samsung.com
Tue Mar 15 22:23:30 UTC 2016


Direct fail_on_null calls now produce output like:

    [weston-info] clients/weston-info.c:714: out of memory

xmalloc, et al produce output on failure like:

    [weston-info] out of memory (-1)

Signed-off-by: Bryce Harrington <bryce at osg.samsung.com>
---
 Makefile.am                        |  6 +++-
 clients/clickdot.c                 |  1 +
 clients/cliptest.c                 |  1 +
 clients/desktop-shell.c            |  1 +
 clients/dnd.c                      |  1 +
 clients/editor.c                   |  1 +
 clients/ivi-shell-user-interface.c | 13 +------
 clients/keyboard.c                 |  1 +
 clients/multi-resource.c           | 15 +-------
 clients/resizor.c                  |  1 +
 clients/screenshot.c               | 16 +--------
 clients/subsurfaces.c              |  1 +
 clients/terminal.c                 |  1 +
 clients/weston-info.c              | 30 +---------------
 clients/window.c                   | 44 +++---------------------
 clients/window.h                   | 11 ------
 ivi-shell/hmi-controller.c         | 12 +------
 shared/xalloc.c                    | 54 +++++++++++++++++++++++++++++
 shared/xalloc.h                    | 70 ++++++++++++++++++++++++++++++++++++++
 tests/ivi_layout-test.c            |  1 +
 tests/presentation-test.c          |  1 +
 tests/weston-test-client-helper.c  | 14 ++------
 tests/weston-test-client-helper.h  | 15 --------
 23 files changed, 152 insertions(+), 159 deletions(-)
 create mode 100644 shared/xalloc.c
 create mode 100644 shared/xalloc.h

diff --git a/Makefile.am b/Makefile.am
index fe08d94..5a98b87 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -93,6 +93,7 @@ weston_SOURCES =					\
 	shared/matrix.c					\
 	shared/matrix.h					\
 	shared/timespec-util.h				\
+	shared/xalloc.h					\
 	shared/zalloc.h					\
 	shared/platform.h				\
 	src/weston-egl-ext.h
@@ -210,6 +211,7 @@ westoninclude_HEADERS =				\
 	src/timeline-object.h			\
 	shared/matrix.h				\
 	shared/config-parser.h			\
+	shared/xalloc.h				\
 	shared/zalloc.h				\
 	shared/platform.h
 
@@ -998,7 +1000,9 @@ libshared_la_SOURCES =				\
 	shared/file-util.h			\
 	shared/helpers.h			\
 	shared/os-compatibility.c		\
-	shared/os-compatibility.h
+	shared/os-compatibility.h		\
+	shared/xalloc.c			\
+	shared/xalloc.h
 
 libshared_cairo_la_CFLAGS =			\
 	-DDATADIR='"$(datadir)"'		\
diff --git a/clients/clickdot.c b/clients/clickdot.c
index 776f8da..f52fbf0 100644
--- a/clients/clickdot.c
+++ b/clients/clickdot.c
@@ -41,6 +41,7 @@
 
 #include "window.h"
 #include "shared/helpers.h"
+#include "shared/xalloc.h"
 
 struct clickdot {
 	struct display *display;
diff --git a/clients/cliptest.c b/clients/cliptest.c
index 07b0e2c..d0f4e3e 100644
--- a/clients/cliptest.c
+++ b/clients/cliptest.c
@@ -50,6 +50,7 @@
 #include <wayland-client.h>
 
 #include "src/vertex-clipping.h"
+#include "shared/xalloc.h"
 #include "window.h"
 
 typedef float GLfloat;
diff --git a/clients/desktop-shell.c b/clients/desktop-shell.c
index b9b9815..f5e0ba2 100644
--- a/clients/desktop-shell.c
+++ b/clients/desktop-shell.c
@@ -46,6 +46,7 @@
 #include "shared/cairo-util.h"
 #include "shared/config-parser.h"
 #include "shared/helpers.h"
+#include "shared/xalloc.h"
 #include "shared/zalloc.h"
 
 #include "weston-desktop-shell-client-protocol.h"
diff --git a/clients/dnd.c b/clients/dnd.c
index e6c3147..41e532e 100644
--- a/clients/dnd.c
+++ b/clients/dnd.c
@@ -42,6 +42,7 @@
 #include "window.h"
 #include "shared/cairo-util.h"
 #include "shared/helpers.h"
+#include "shared/xalloc.h"
 
 struct dnd_drag;
 
diff --git a/clients/editor.c b/clients/editor.c
index 311bdb5..e081a5b 100644
--- a/clients/editor.c
+++ b/clients/editor.c
@@ -37,6 +37,7 @@
 #include <pango/pangocairo.h>
 
 #include "shared/helpers.h"
+#include "shared/xalloc.h"
 #include "window.h"
 #include "text-input-unstable-v1-client-protocol.h"
 
diff --git a/clients/ivi-shell-user-interface.c b/clients/ivi-shell-user-interface.c
index 1349c73..db64f19 100644
--- a/clients/ivi-shell-user-interface.c
+++ b/clients/ivi-shell-user-interface.c
@@ -40,6 +40,7 @@
 #include "shared/config-parser.h"
 #include "shared/helpers.h"
 #include "shared/os-compatibility.h"
+#include "shared/xalloc.h"
 #include "shared/zalloc.h"
 #include "ivi-application-client-protocol.h"
 #include "ivi-hmi-controller-client-protocol.h"
@@ -164,18 +165,6 @@ hmi_homescreen_setting {
 };
 
 static void *
-fail_on_null(void *p, size_t size, char *file, int32_t line)
-{
-	if (size && !p) {
-		fprintf(stderr, "%s(%d) %zd: out of memory\n",
-			file, line, size);
-		exit(EXIT_FAILURE);
-	}
-
-	return p;
-}
-
-static void *
 mem_alloc(size_t size, char *file, int32_t line)
 {
 	return fail_on_null(zalloc(size), size, file, line);
diff --git a/clients/keyboard.c b/clients/keyboard.c
index b843fda..d719764 100644
--- a/clients/keyboard.c
+++ b/clients/keyboard.c
@@ -34,6 +34,7 @@
 #include "window.h"
 #include "input-method-unstable-v1-client-protocol.h"
 #include "text-input-unstable-v1-client-protocol.h"
+#include "shared/xalloc.h"
 
 struct keyboard;
 
diff --git a/clients/multi-resource.c b/clients/multi-resource.c
index 4ed4d50..b061e68 100644
--- a/clients/multi-resource.c
+++ b/clients/multi-resource.c
@@ -40,6 +40,7 @@
 
 #include <wayland-client.h>
 #include "shared/os-compatibility.h"
+#include "shared/xalloc.h"
 #include "shared/zalloc.h"
 
 struct device {
@@ -83,20 +84,6 @@ static const struct wl_buffer_listener buffer_listener = {
 	buffer_release
 };
 
-static inline void *
-xzalloc(size_t s)
-{
-	void *p;
-
-	p = zalloc(s);
-	if (p == NULL) {
-		fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
-		exit(EXIT_FAILURE);
-	}
-
-	return p;
-}
-
 static int
 attach_buffer(struct window *window, int width, int height)
 {
diff --git a/clients/resizor.c b/clients/resizor.c
index d205eb6..ad3b71b 100644
--- a/clients/resizor.c
+++ b/clients/resizor.c
@@ -35,6 +35,7 @@
 #include <wayland-client.h>
 
 #include "window.h"
+#include "shared/xalloc.h"
 
 struct spring {
 	double current;
diff --git a/clients/screenshot.c b/clients/screenshot.c
index 18b5f88..6e43d5c 100644
--- a/clients/screenshot.c
+++ b/clients/screenshot.c
@@ -38,6 +38,7 @@
 #include <wayland-client.h>
 #include "weston-screenshooter-client-protocol.h"
 #include "shared/os-compatibility.h"
+#include "shared/xalloc.h"
 
 /* The screenshooter is a good example of a custom object exposed by
  * the compositor and serves as a test bed for implementing client
@@ -79,21 +80,6 @@ display_handle_geometry(void *data,
 	}
 }
 
-static void *
-xmalloc(size_t size)
-{
-	void *p;
-
-	p = malloc(size);
-	if (p == NULL) {
-		fprintf(stderr, "%s: out of memory\n",
-			program_invocation_short_name);
-		exit(EXIT_FAILURE);
-	}
-
-	return p;
-}
-
 static void
 display_handle_mode(void *data,
 		    struct wl_output *wl_output,
diff --git a/clients/subsurfaces.c b/clients/subsurfaces.c
index bf0b96b..45801a8 100644
--- a/clients/subsurfaces.c
+++ b/clients/subsurfaces.c
@@ -42,6 +42,7 @@
 #include <EGL/eglext.h>
 
 #include "shared/helpers.h"
+#include "shared/xalloc.h"
 #include "shared/zalloc.h"
 #include "window.h"
 
diff --git a/clients/terminal.c b/clients/terminal.c
index 7406f50..a70fef3 100644
--- a/clients/terminal.c
+++ b/clients/terminal.c
@@ -45,6 +45,7 @@
 
 #include "shared/config-parser.h"
 #include "shared/helpers.h"
+#include "shared/xalloc.h"
 #include "window.h"
 
 static int option_fullscreen;
diff --git a/clients/weston-info.c b/clients/weston-info.c
index e277f88..8ba80c0 100644
--- a/clients/weston-info.c
+++ b/clients/weston-info.c
@@ -34,6 +34,7 @@
 
 #include "shared/helpers.h"
 #include "shared/os-compatibility.h"
+#include "shared/xalloc.h"
 #include "shared/zalloc.h"
 #include "presentation-time-client-protocol.h"
 
@@ -119,35 +120,6 @@ struct weston_info {
 	bool roundtrip_needed;
 };
 
-static void *
-fail_on_null(void *p)
-{
-	if (p == NULL) {
-		fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
-		exit(EXIT_FAILURE);
-	}
-
-	return p;
-}
-
-static void *
-xmalloc(size_t s)
-{
-	return fail_on_null(malloc(s));
-}
-
-static void *
-xzalloc(size_t s)
-{
-	return fail_on_null(zalloc(s));
-}
-
-static char *
-xstrdup(const char *s)
-{
-	return fail_on_null(strdup(s));
-}
-
 static void
 print_global_info(void *data)
 {
diff --git a/clients/window.c b/clients/window.c
index ced867e..071b059 100644
--- a/clients/window.c
+++ b/clients/window.c
@@ -68,6 +68,7 @@ typedef void *EGLContext;
 #include <wayland-client.h>
 #include "shared/cairo-util.h"
 #include "shared/helpers.h"
+#include "shared/xalloc.h"
 #include "shared/zalloc.h"
 #include "xdg-shell-unstable-v5-client-protocol.h"
 #include "text-cursor-position-client-protocol.h"
@@ -4746,7 +4747,7 @@ window_create(struct display *display)
 		window->xdg_surface =
 			xdg_shell_get_xdg_surface(window->display->xdg_shell,
 						  window->main_surface->surface);
-		fail_on_null(window->xdg_surface);
+		fail_on_null(window->xdg_surface, 1, __FILE__, __LINE__);
 
 		xdg_surface_set_user_data(window->xdg_surface, window);
 		xdg_surface_add_listener(window->xdg_surface,
@@ -4757,7 +4758,7 @@ window_create(struct display *display)
 		window->ivi_surface =
 			ivi_application_surface_create(display->ivi_application,
 						       id_ivisurf, window->main_surface->surface);
-		fail_on_null(window->ivi_surface);
+		fail_on_null(window->ivi_surface, 1, __FILE__, __LINE__);
 
 		ivi_surface_add_listener(window->ivi_surface,
 					 &ivi_surface_listener, window);
@@ -4947,7 +4948,7 @@ create_menu(struct display *display,
 	menu->widget = window_add_widget(menu->window, menu);
 	menu->frame = frame_create(window->display->theme, 0, 0,
 				   FRAME_BUTTON_NONE, NULL);
-	fail_on_null(menu->frame);
+	fail_on_null(menu->frame, 1, __FILE__, __LINE__);
 	menu->entries = entries;
 	menu->count = count;
 	menu->release_count = 0;
@@ -5024,7 +5025,7 @@ window_show_menu(struct display *display,
 						    display_get_serial(window->display),
 						    window->x - ix,
 						    window->y - iy);
-	fail_on_null(window->xdg_popup);
+	fail_on_null(window->xdg_popup, 1, __FILE__, __LINE__);
 
 	xdg_popup_set_user_data(window->xdg_popup, window);
 	xdg_popup_add_listener(window->xdg_popup,
@@ -6027,38 +6028,3 @@ keysym_modifiers_get_mask(struct wl_array *modifiers_map,
 
 	return 1 << index;
 }
-
-void *
-fail_on_null(void *p)
-{
-	if (p == NULL) {
-		fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
-		exit(EXIT_FAILURE);
-	}
-
-	return p;
-}
-
-void *
-xmalloc(size_t s)
-{
-	return fail_on_null(malloc(s));
-}
-
-void *
-xzalloc(size_t s)
-{
-	return fail_on_null(zalloc(s));
-}
-
-char *
-xstrdup(const char *s)
-{
-	return fail_on_null(strdup(s));
-}
-
-void *
-xrealloc(char *p, size_t s)
-{
-	return fail_on_null(realloc(p, s));
-}
diff --git a/clients/window.h b/clients/window.h
index 349dead..8c8568f 100644
--- a/clients/window.h
+++ b/clients/window.h
@@ -51,17 +51,6 @@ struct rectangle {
 	int32_t height;
 };
 
-void *
-fail_on_null(void *p);
-void *
-xmalloc(size_t s);
-void *
-xzalloc(size_t s);
-char *
-xstrdup(const char *s);
-void *
-xrealloc(char *p, size_t s);
-
 struct display *
 display_create(int *argc, char *argv[]);
 
diff --git a/ivi-shell/hmi-controller.c b/ivi-shell/hmi-controller.c
index 86f7c50..0b932d6 100644
--- a/ivi-shell/hmi-controller.c
+++ b/ivi-shell/hmi-controller.c
@@ -61,6 +61,7 @@
 #include "ivi-layout-export.h"
 #include "ivi-hmi-controller-server-protocol.h"
 #include "shared/helpers.h"
+#include "shared/xalloc.h"
 
 /*****************************************************************************
  *  structure, globals
@@ -151,17 +152,6 @@ controller_module_init(struct weston_compositor *ec,
  *  local functions
  ****************************************************************************/
 static void *
-fail_on_null(void *p, size_t size, char *file, int32_t line)
-{
-	if (size && !p) {
-		weston_log("%s(%d) %zd: out of memory\n", file, line, size);
-		exit(EXIT_FAILURE);
-	}
-
-	return p;
-}
-
-static void *
 mem_alloc(size_t size, char *file, int32_t line)
 {
 	return fail_on_null(calloc(1, size), size, file, line);
diff --git a/shared/xalloc.c b/shared/xalloc.c
new file mode 100644
index 0000000..4bf8a3e
--- /dev/null
+++ b/shared/xalloc.c
@@ -0,0 +1,54 @@
+/*
+ * Copyright © 2008 Kristian Høgsberg
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "xalloc.h"
+
+void *
+fail_on_null(void *p, size_t size, char *file, int32_t line)
+{
+	if (p == NULL) {
+		fprintf(stderr, "[%s] ", program_invocation_short_name);
+		if (file)
+			fprintf(stderr, "%s:%d: ", file, line);
+		fprintf(stderr, "out of memory");
+		if (size)
+			fprintf(stderr, " (%zd)", size);
+		fprintf(stderr, "\n");
+		exit(EXIT_FAILURE);
+	}
+
+	return p;
+}
+
+
+#ifdef  __cplusplus
+}
+#endif
diff --git a/shared/xalloc.h b/shared/xalloc.h
new file mode 100644
index 0000000..74a9b6c
--- /dev/null
+++ b/shared/xalloc.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright © 2008 Kristian Høgsberg
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef WESTON_XALLOC_H
+#define WESTON_XALLOC_H
+
+#ifdef  __cplusplus
+extern "C" {
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "zalloc.h"
+
+void *
+fail_on_null(void *p, size_t size, char *file, int32_t line);
+
+static inline void *
+xmalloc(size_t s)
+{
+        return fail_on_null(malloc(s), s, NULL, 0);
+}
+
+static inline void *
+xzalloc(size_t s)
+{
+	return fail_on_null(zalloc(s), s, NULL, 0);
+}
+
+static inline char *
+xstrdup(const char *s)
+{
+	return fail_on_null(strdup(s), 0, NULL, 0);
+}
+
+static inline void *
+xrealloc(char *p, size_t s)
+{
+        return fail_on_null(realloc(p, s), s, NULL, 0);
+}
+
+
+#ifdef  __cplusplus
+}
+#endif
+
+#endif /* WESTON_XALLOC_H */
diff --git a/tests/ivi_layout-test.c b/tests/ivi_layout-test.c
index e2ee5c4..b775abe 100644
--- a/tests/ivi_layout-test.c
+++ b/tests/ivi_layout-test.c
@@ -29,6 +29,7 @@
 #include <string.h>
 
 #include "shared/helpers.h"
+#include "shared/xalloc.h"
 #include "weston-test-client-helper.h"
 #include "ivi-application-client-protocol.h"
 #include "ivi-test.h"
diff --git a/tests/presentation-test.c b/tests/presentation-test.c
index a985c06..f8c3be5 100644
--- a/tests/presentation-test.c
+++ b/tests/presentation-test.c
@@ -32,6 +32,7 @@
 #include <time.h>
 
 #include "shared/helpers.h"
+#include "shared/xalloc.h"
 #include "weston-test-client-helper.h"
 #include "presentation-time-client-protocol.h"
 
diff --git a/tests/weston-test-client-helper.c b/tests/weston-test-client-helper.c
index 97c4395..3f7ff2b 100644
--- a/tests/weston-test-client-helper.c
+++ b/tests/weston-test-client-helper.c
@@ -33,25 +33,15 @@
 #include <sys/mman.h>
 #include <cairo.h>
 
-#include "zalloc.h"
 #include "shared/os-compatibility.h"
+#include "shared/xalloc.h"
+#include "shared/zalloc.h"
 #include "weston-test-client-helper.h"
 
 #define max(a, b) (((a) > (b)) ? (a) : (b))
 #define min(a, b) (((a) > (b)) ? (b) : (a))
 #define clip(x, a, b)  min(max(x, a), b)
 
-void *
-fail_on_null(void *p)
-{
-	if (p == NULL) {
-		fprintf(stderr, "out of memory\n");
-		exit(EXIT_FAILURE);
-	}
-	return p;
-}
-
-
 int
 surface_contains(struct surface *surface, int x, int y)
 {
diff --git a/tests/weston-test-client-helper.h b/tests/weston-test-client-helper.h
index e3ebd1f..51ec881 100644
--- a/tests/weston-test-client-helper.h
+++ b/tests/weston-test-client-helper.h
@@ -145,21 +145,6 @@ struct rectangle {
 	int height;
 };
 
-void *
-fail_on_null(void *p);
-
-static inline void *
-xzalloc(size_t s)
-{
-	return fail_on_null(calloc(1, s));
-}
-
-static inline void *
-xmalloc(size_t s)
-{
-	return fail_on_null(malloc(s));
-}
-
 struct client *
 create_client(void);
 
-- 
1.9.1



More information about the wayland-devel mailing list