clients: Introduce xmalloc() and use it a few places

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.
This commit is contained in:
Kristian Høgsberg 2013-07-25 15:20:20 -07:00
parent c0bf8173dd
commit ce27841be5
3 changed files with 34 additions and 16 deletions

View 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);

View 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));
}

View 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[]);