diff --git a/lib/Makefile.am b/lib/Makefile.am index 5b77b36..8f70be8 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,5 +1,5 @@ noinst_LIBRARIES = libwait4.a -libwait4_a_SOURCES = err.compat.h compat.h dummy.c +libwait4_a_SOURCES = err.compat.h compat.h xmem.c xmem.h libwait4_a_LIBADD = $(LIBOBJS) diff --git a/lib/dummy.c b/lib/dummy.c deleted file mode 100644 index e69de29..0000000 diff --git a/lib/xmem.c b/lib/xmem.c new file mode 100644 index 0000000..70a4ac7 --- /dev/null +++ b/lib/xmem.c @@ -0,0 +1,79 @@ +/* + * xmem - Incenp.org Notch Library: die-on-error memory functions + * Copyright (C) 2011 Damien Goutte-Gattat + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +void (*xmem_error)(const char *, unsigned int) = NULL; + +#define mem_error(f, l) \ + do { \ + if ( xmem_error ) \ + (*xmem_error)((f), (l)); \ + err(EXIT_FAILURE, "%s, %d", (f), (l)); \ + } while ( 0 ) + +void * +do_malloc(size_t s, const char *file, unsigned int line) +{ + void *p; + + if ( ! (p = malloc(s)) && s ) + mem_error(file, line); + + return p; +} + +void * +do_calloc(size_t n, size_t s, const char *file, unsigned int line) +{ + void *p; + + if ( ! (p = calloc(n, s)) && n && s ) + mem_error(file, line); + + return p; +} + +void * +do_realloc(void *p, size_t s, const char *file, unsigned int line) +{ + void *np; + + if ( ! (np = realloc(p, s)) && s ) + mem_error(file, line); + + return np; +} + +char * +do_strdup(const char *s, const char *file, unsigned int line) +{ + char *dup; + + if ( ! (dup = strdup(s)) && s ) + mem_error(file, line); + + return dup; +} diff --git a/lib/xmem.h b/lib/xmem.h new file mode 100644 index 0000000..e84ae11 --- /dev/null +++ b/lib/xmem.h @@ -0,0 +1,52 @@ +/* + * xmem - Incenp.org Notch Library: die-on-error memory functions + * Copyright (C) 2011 Damien Goutte-Gattat + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef ICP20110203_XMEM_H +#define ICP20110203_XMEM_H + +#include +#include + +#ifdef __cpluscplus +extern "C" { +#endif + +extern void (*xmem_error)(const char *, unsigned int); + +void * +do_malloc(size_t, const char *, unsigned int); + +void * +do_calloc(size_t, size_t, const char *, unsigned int); + +void * +do_realloc(void *, size_t, const char *, unsigned int); + +char * +do_strdup(const char *, const char *, unsigned int); + +#define xmalloc(s) do_malloc(s, __FILE__, __LINE__) +#define xcalloc(n,s) do_calloc(n, s, __FILE__, __LINE__) +#define xrealloc(p,s) do_realloc(p, s, __FILE__, __LINE__) +#define xstrdup(s) do_strdup(s, __FILE__, __LINE__) + +#ifdef __cplusplus +} +#endif + +#endif /* !ICP20110203_XMEM_H */ diff --git a/src/wait4.c b/src/wait4.c index 783aef9..cd151aa 100644 --- a/src/wait4.c +++ b/src/wait4.c @@ -25,6 +25,7 @@ #include #include #include +#include /* Help and informations about the program. */ @@ -112,7 +113,7 @@ main(int argc, char **argv) if ( (len = argc - optind) < 1 ) usage(EXIT_FAILURE); - pids = malloc(len * sizeof(int)); + pids = xmalloc(len * sizeof(int)); for ( i = 0; i < len; i++ ) pids[i] = get_integer_or_die(argv[optind + i]);