clients: Introduce xmalloc() and use it a few places
authorKristian Høgsberg <krh@bitplanet.net>
Thu, 25 Jul 2013 22:20:20 +0000 (15:20 -0700)
committerKristian Høgsberg <krh@bitplanet.net>
Thu, 25 Jul 2013 22:54:20 +0000 (15:54 -0700)
For the sample clients we introduce xmalloc() to simplify OOM-handling.
This patch only converts a few callsites, but this will be our strategy
going forward.

clients/clickdot.c
clients/window.c
clients/window.h

index 714b4b9..c5fbc64 100644 (file)
@@ -247,9 +247,7 @@ clickdot_create(struct display *display)
 {
        struct clickdot *clickdot;
 
-       clickdot = malloc(sizeof *clickdot);
-       if (clickdot == NULL)
-               return clickdot;
+       clickdot = xmalloc(sizeof *clickdot);
        memset(clickdot, 0, sizeof *clickdot);
 
        clickdot->window = window_create(display);
index cbfe12f..dd9ea7a 100644 (file)
@@ -4190,10 +4190,7 @@ window_create_internal(struct display *display,
        struct window *window;
        struct surface *surface;
 
-       window = malloc(sizeof *window);
-       if (window == NULL)
-               return NULL;
-
+       window = xmalloc(sizeof *window);
        memset(window, 0, sizeof *window);
        wl_list_init(&window->subsurface_list);
        window->display = display;
@@ -4206,6 +4203,7 @@ window_create_internal(struct display *display,
                window->shell_surface =
                        wl_shell_get_shell_surface(display->shell,
                                                   surface->surface);
+               fail_on_null(window->shell_surface);
        }
 
        window->type = type;
@@ -4251,13 +4249,7 @@ window_create(struct display *display)
 struct window *
 window_create_custom(struct display *display)
 {
-       struct window *window;
-
-       window = window_create_internal(display, NULL, TYPE_CUSTOM);
-       if (!window)
-               return NULL;
-
-       return window;
+       return window_create_internal(display, NULL, TYPE_CUSTOM);
 }
 
 struct window *
@@ -4268,8 +4260,6 @@ window_create_transient(struct display *display, struct window *parent,
 
        window = window_create_internal(parent->display,
                                        parent, TYPE_TRANSIENT);
-       if (!window)
-               return NULL;
 
        window->x = x;
        window->y = y;
@@ -5309,3 +5299,26 @@ keysym_modifiers_get_mask(struct wl_array *modifiers_map,
 
        return 1 << index;
 }
+
+void *
+fail_on_null(void *p)
+{
+       if (p == NULL) {
+               fprintf(stderr, "wayland-scanner: out of memory\n");
+               exit(EXIT_FAILURE);
+       }
+
+       return p;
+}
+
+void *
+xmalloc(size_t s)
+{
+       return fail_on_null(malloc(s));
+}
+
+char *
+xstrdup(const char *s)
+{
+       return fail_on_null(strdup(s));
+}
index a79b020..75be165 100644 (file)
@@ -53,6 +53,13 @@ struct rectangle {
        int32_t height;
 };
 
+void *
+fail_on_null(void *p);
+void *
+xmalloc(size_t s);
+char *
+xstrdup(const char *s);
+
 struct display *
 display_create(int *argc, char *argv[]);