new file mode 100644
@@ -0,0 +1,53 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef XALLOC_H
+#define XALLOC_H
+
+#include <stdlib.h>
+#include <string.h>
+
+static inline void *xmalloc(size_t size)
+{
+ void *p = malloc(size);
+
+ if (!p)
+ exit(1);
+ return p;
+}
+
+static inline void *xcalloc(size_t nmemb, size_t size)
+{
+ void *p = calloc(nmemb, size);
+
+ if (!p)
+ exit(1);
+ return p;
+}
+
+static inline void *xrealloc(void *p, size_t size)
+{
+ p = realloc(p, size);
+ if (!p)
+ exit(1);
+ return p;
+}
+
+static inline char *xstrdup(const char *s)
+{
+ char *p = strdup(s);
+
+ if (!p)
+ exit(1);
+ return p;
+}
+
+static inline char *xstrndup(const char *s, size_t n)
+{
+ char *p = strndup(s, n);
+
+ if (!p)
+ exit(1);
+ return p;
+}
+
+#endif /* XALLOC_H */
@@ -18,6 +18,7 @@
#include <time.h>
#include <unistd.h>
+#include <xalloc.h>
#include "internal.h"
#include "lkc.h"
@@ -9,6 +9,7 @@
#include <stdlib.h>
#include <string.h>
+#include <xalloc.h>
#include "lkc.h"
#define DEBUG_EXPR 0
@@ -13,6 +13,7 @@
#include <stdlib.h>
#include <string.h>
+#include <xalloc.h>
#include "lkc.h"
#include "preprocess.h"
@@ -53,11 +53,6 @@ static inline void xfwrite(const void *str, size_t len, size_t count, FILE *out)
/* util.c */
unsigned int strhash(const char *s);
const char *file_lookup(const char *name);
-void *xmalloc(size_t size);
-void *xcalloc(size_t nmemb, size_t size);
-void *xrealloc(void *p, size_t size);
-char *xstrdup(const char *s);
-char *xstrndup(const char *s, size_t n);
/* lexer.l */
int yylex(void);
@@ -20,6 +20,7 @@
#include <unistd.h>
#include <list.h>
+#include <xalloc.h>
#include "lkc.h"
#include "lxdialog/dialog.h"
#include "mnconf-common.h"
@@ -9,6 +9,7 @@
#include <string.h>
#include <list.h>
+#include <xalloc.h>
#include "lkc.h"
#include "internal.h"
@@ -12,6 +12,7 @@
#include <stdlib.h>
#include <list.h>
+#include <xalloc.h>
#include "lkc.h"
#include "mnconf-common.h"
#include "nconf.h"
@@ -4,6 +4,7 @@
*
* Derived from menuconfig.
*/
+#include <xalloc.h>
#include "nconf.h"
#include "lkc.h"
@@ -11,6 +11,7 @@
#include <string.h>
#include <stdbool.h>
+#include <xalloc.h>
#include "lkc.h"
#include "internal.h"
#include "preprocess.h"
@@ -11,6 +11,7 @@
#include <array_size.h>
#include <list.h>
+#include <xalloc.h>
#include "internal.h"
#include "lkc.h"
#include "preprocess.h"
@@ -22,6 +22,7 @@
#include <stdlib.h>
+#include <xalloc.h>
#include "lkc.h"
#include "qconf.h"
@@ -9,6 +9,7 @@
#include <string.h>
#include <regex.h>
+#include <xalloc.h>
#include "internal.h"
#include "lkc.h"
@@ -9,6 +9,7 @@
#include <string.h>
#include <hashtable.h>
+#include <xalloc.h>
#include "lkc.h"
unsigned int strhash(const char *s)
@@ -102,52 +103,3 @@ char *str_get(const struct gstr *gs)
{
return gs->s;
}
-
-void *xmalloc(size_t size)
-{
- void *p = malloc(size);
- if (p)
- return p;
- fprintf(stderr, "Out of memory.\n");
- exit(1);
-}
-
-void *xcalloc(size_t nmemb, size_t size)
-{
- void *p = calloc(nmemb, size);
- if (p)
- return p;
- fprintf(stderr, "Out of memory.\n");
- exit(1);
-}
-
-void *xrealloc(void *p, size_t size)
-{
- p = realloc(p, size);
- if (p)
- return p;
- fprintf(stderr, "Out of memory.\n");
- exit(1);
-}
-
-char *xstrdup(const char *s)
-{
- char *p;
-
- p = strdup(s);
- if (p)
- return p;
- fprintf(stderr, "Out of memory.\n");
- exit(1);
-}
-
-char *xstrndup(const char *s, size_t n)
-{
- char *p;
-
- p = strndup(s, n);
- if (p)
- return p;
- fprintf(stderr, "Out of memory.\n");
- exit(1);
-}
These functions will be useful for other host programs. Signed-off-by: Masahiro Yamada <masahiroy@kernel.org> --- scripts/include/xalloc.h | 53 ++++++++++++++++++++++++++++++++++++ scripts/kconfig/confdata.c | 1 + scripts/kconfig/expr.c | 1 + scripts/kconfig/lexer.l | 1 + scripts/kconfig/lkc.h | 5 ---- scripts/kconfig/mconf.c | 1 + scripts/kconfig/menu.c | 1 + scripts/kconfig/nconf.c | 1 + scripts/kconfig/nconf.gui.c | 1 + scripts/kconfig/parser.y | 1 + scripts/kconfig/preprocess.c | 1 + scripts/kconfig/qconf.cc | 1 + scripts/kconfig/symbol.c | 1 + scripts/kconfig/util.c | 50 +--------------------------------- 14 files changed, 65 insertions(+), 54 deletions(-) create mode 100644 scripts/include/xalloc.h