diff options
author | Mike Frysinger <vapier@gentoo.org> | 2010-09-30 01:27:38 -0400 |
---|---|---|
committer | Artem Bityutskiy <Artem.Bityutskiy@nokia.com> | 2010-09-30 08:54:05 +0300 |
commit | b2dc086600f64d5a2db179fb6bed79a0a78228e4 (patch) | |
tree | 9a49989ca2ec2b2ee74f1399be1d9d8469ef8af7 /include | |
parent | 94a20240f9d6776619d7652013bb10131ddad1fc (diff) |
mtd-utils: new memory wrappers
The mkfs.jffs2 program has local wrappers for memory related functions
that are useful beyond mkfs.jffs2, so break them out into a common header.
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Diffstat (limited to 'include')
-rw-r--r-- | include/common.h | 8 | ||||
-rw-r--r-- | include/xalloc.h | 80 |
2 files changed, 88 insertions, 0 deletions
diff --git a/include/common.h b/include/common.h index 472315e..9f49f55 100644 --- a/include/common.h +++ b/include/common.h @@ -58,6 +58,9 @@ extern "C" { fprintf(stderr, "%s: error!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \ -1; \ }) +#define errmsg_die(fmt, ...) do { \ + exit(errmsg(fmt, ##__VA_ARGS__)); \ +} while(0) /* System error messages */ #define sys_errmsg(fmt, ...) ({ \ @@ -69,6 +72,9 @@ extern "C" { fprintf(stderr, "error %d (%s)\n", _err, strerror(_err)); \ -1; \ }) +#define sys_errmsg_die(fmt, ...) do { \ + exit(sys_errmsg(fmt, ##__VA_ARGS__)); \ +} while(0) /* Warnings */ #define warnmsg(fmt, ...) do { \ @@ -103,6 +109,8 @@ simple_strtoX(strtoll, long int) simple_strtoX(strtoul, unsigned long int) simple_strtoX(strtoull, unsigned long int) +#include "xalloc.h" + #ifdef __cplusplus } #endif diff --git a/include/xalloc.h b/include/xalloc.h new file mode 100644 index 0000000..5d145d9 --- /dev/null +++ b/include/xalloc.h @@ -0,0 +1,80 @@ +/* + * memory wrappers + * + * Copyright (c) Artem Bityutskiy, 2007, 2008 + * Copyright 2001, 2002 Red Hat, Inc. + * 2001 David A. Schleef <ds@lineo.com> + * 2002 Axis Communications AB + * 2001, 2002 Erik Andersen <andersen@codepoet.org> + * 2004 University of Szeged, Hungary + * 2006 KaiGai Kohei <kaigai@ak.jp.nec.com> + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef __MTD_UTILS_XALLOC_H__ +#define __MTD_UTILS_XALLOC_H__ + +#include <stdlib.h> +#include <string.h> + +/* + * Mark these functions as unused so that gcc does not emit warnings + * when people include this header but don't use every function. + */ + +__attribute__((unused)) +static void *xmalloc(size_t size) +{ + void *ptr = malloc(size); + + if (ptr == NULL && size != 0) + sys_errmsg_die("malloc(%zu) failed", size); + return ptr; +} + +__attribute__((unused)) +static void *xcalloc(size_t nmemb, size_t size) +{ + void *ptr = calloc(nmemb, size); + + if (ptr == NULL && nmemb != 0 && size != 0) + sys_errmsg_die("calloc(%zu, %zu) failed", nmemb, size); + return ptr; +} + +__attribute__((unused)) +static void *xrealloc(void *ptr, size_t size) +{ + ptr = realloc(ptr, size); + if (ptr == NULL && size != 0) + sys_errmsg_die("realloc(%p, %zu) failed", ptr, size); + return ptr; +} + +__attribute__((unused)) +static char *xstrdup(const char *s) +{ + char *t; + + if (s == NULL) + return NULL; + t = strdup(s); + if (t == NULL) + sys_errmsg_die("strdup(%p) failed", s); + return t; +} + +#endif /* !__MTD_UTILS_XALLOC_H__ */ |