move sstrdup, scalloc, smalloc, srealloc to libi3, improve error messages
This commit is contained in:
47
libi3/safewrappers.c
Normal file
47
libi3/safewrappers.c
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* vim:ts=4:sw=4:expandtab
|
||||
*
|
||||
* i3 - an improved dynamic tiling window manager
|
||||
*
|
||||
* © 2009-2011 Michael Stapelberg and contributors
|
||||
*
|
||||
* See file LICENSE for license information.
|
||||
*
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <err.h>
|
||||
|
||||
|
||||
/*
|
||||
* The s* functions (safe) are wrappers around malloc, strdup, …, which exits if one of
|
||||
* the called functions returns NULL, meaning that there is no more memory available
|
||||
*
|
||||
*/
|
||||
void *smalloc(size_t size) {
|
||||
void *result = malloc(size);
|
||||
if (result == NULL)
|
||||
err(EXIT_FAILURE, "malloc(%zd)", size);
|
||||
return result;
|
||||
}
|
||||
|
||||
void *scalloc(size_t size) {
|
||||
void *result = calloc(size, 1);
|
||||
if (result == NULL)
|
||||
err(EXIT_FAILURE, "calloc(%zd)", size);
|
||||
return result;
|
||||
}
|
||||
|
||||
void *srealloc(void *ptr, size_t size) {
|
||||
void *result = realloc(ptr, size);
|
||||
if (result == NULL && size > 0)
|
||||
err(EXIT_FAILURE, "realloc(%zd)", size);
|
||||
return result;
|
||||
}
|
||||
|
||||
char *sstrdup(const char *str) {
|
||||
char *result = strdup(str);
|
||||
if (result == NULL)
|
||||
err(EXIT_FAILURE, "strdup()");
|
||||
return result;
|
||||
}
|
Reference in New Issue
Block a user