chg: add utility functions mallocx, reallocx
authorJun Wu <quark@fb.com>
Wed, 17 Feb 2016 14:51:38 +0000
changeset 28165 c6705c6303dd
parent 28164 ad11edefa7c4
child 28166 c6e0c2533e5f
chg: add utility functions mallocx, reallocx They are like malloc and realloc but will abort the program on error. A lot of places use {m,re}alloc and check their results. This patch can simplify them.
contrib/chg/util.c
contrib/chg/util.h
--- a/contrib/chg/util.c	Mon Feb 15 13:20:20 2016 -0800
+++ b/contrib/chg/util.c	Wed Feb 17 14:51:38 2016 +0000
@@ -50,6 +50,22 @@
 	va_end(args);
 }
 
+void *mallocx(size_t size)
+{
+	void *result = malloc(size);
+	if (!result)
+		abortmsg("failed to malloc");
+	return result;
+}
+
+void *reallocx(void *ptr, size_t size)
+{
+	void *result = realloc(ptr, size);
+	if (!result)
+		abortmsg("failed to realloc");
+	return result;
+}
+
 /*
  * Execute a shell command in mostly the same manner as system(), with the
  * give environment variables, after chdir to the given cwd. Returns a status
--- a/contrib/chg/util.h	Mon Feb 15 13:20:20 2016 -0800
+++ b/contrib/chg/util.h	Wed Feb 17 14:51:38 2016 +0000
@@ -19,6 +19,9 @@
 void enabledebugmsg(void);
 void debugmsg(const char *fmt, ...) PRINTF_FORMAT_;
 
+void *mallocx(size_t size);
+void *reallocx(void *ptr, size_t size);
+
 int runshellcmd(const char *cmd, const char *envp[], const char *cwd);
 
 #endif  /* UTIL_H_ */