[PATCH] utils: inline xmalloc(), xzalloc() and xfree()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



So these functions can be used from main.c and cli.c.

This patch reverts 7c0407c5ca53 ("nft: don't use xzalloc()").

Suggested-by: Phil Sutter <phil@xxxxxx>
Signed-off-by: Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx>
---
 include/utils.h | 29 ++++++++++++++++++++++++++---
 src/main.c      |  8 +-------
 src/utils.c     | 24 ------------------------
 3 files changed, 27 insertions(+), 34 deletions(-)

diff --git a/include/utils.h b/include/utils.h
index 647e8bbe0030..5f3f49d2e87a 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -128,11 +128,34 @@ extern void __memory_allocation_error(const char *filename, uint32_t line) __nor
 #define memory_allocation_error()		\
 	__memory_allocation_error(__FILE__, __LINE__);
 
-extern void xfree(const void *ptr);
-extern void *xmalloc(size_t size);
+static inline void *xmalloc(size_t size)
+{
+	void *ptr;
+
+	ptr = malloc(size);
+	if (ptr == NULL)
+		memory_allocation_error();
+
+	return ptr;
+}
+
+static inline void *xzalloc(size_t size)
+{
+	void *ptr;
+
+	ptr = xmalloc(size);
+	memset(ptr, 0, size);
+
+	return ptr;
+}
+
+static inline void xfree(const void *ptr)
+{
+	free((void *)ptr);
+}
+
 extern void *xmalloc_array(size_t nmemb, size_t size);
 extern void *xrealloc(void *ptr, size_t size);
-extern void *xzalloc(size_t size);
 extern char *xstrdup(const char *s);
 extern void xstrunescape(const char *in, char *out);
 
diff --git a/src/main.c b/src/main.c
index 8e6c897cdd36..cbfd69a42d04 100644
--- a/src/main.c
+++ b/src/main.c
@@ -19,7 +19,6 @@
 #include <sys/types.h>
 
 #include <nftables/libnftables.h>
-#include <nftables.h>
 #include <utils.h>
 #include <cli.h>
 
@@ -303,12 +302,7 @@ int main(int argc, char * const *argv)
 		for (len = 0, i = optind; i < argc; i++)
 			len += strlen(argv[i]) + strlen(" ");
 
-		buf = calloc(1, len);
-		if (buf == NULL) {
-			fprintf(stderr, "%s:%u: Memory allocation failure\n",
-				__FILE__, __LINE__);
-			exit(NFT_EXIT_NOMEM);
-		}
+		buf = xzalloc(len);
 		for (i = optind; i < argc; i++) {
 			strcat(buf, argv[i]);
 			if (i + 1 < argc)
diff --git a/src/utils.c b/src/utils.c
index 47f5b791547b..da7547d8b989 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -24,21 +24,6 @@ void __noreturn __memory_allocation_error(const char *filename, uint32_t line)
 	exit(NFT_EXIT_NOMEM);
 }
 
-void xfree(const void *ptr)
-{
-	free((void *)ptr);
-}
-
-void *xmalloc(size_t size)
-{
-	void *ptr;
-
-	ptr = malloc(size);
-	if (ptr == NULL)
-		memory_allocation_error();
-	return ptr;
-}
-
 void *xmalloc_array(size_t nmemb, size_t size)
 {
 	assert(size != 0);
@@ -58,15 +43,6 @@ void *xrealloc(void *ptr, size_t size)
 	return ptr;
 }
 
-void *xzalloc(size_t size)
-{
-	void *ptr;
-
-	ptr = xmalloc(size);
-	memset(ptr, 0, size);
-	return ptr;
-}
-
 char *xstrdup(const char *s)
 {
 	char *res;
-- 
2.11.0




[Index of Archives]     [Netfitler Users]     [Berkeley Packet Filter]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux