utils: inline xmalloc(), xzalloc() and xfree()
Pablo Neira Ayuso
maintainerPATCH 1/1 utils: inline xmalloc(), xzalloc() and xfree()
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 <[email protected]>
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- include/utils.h | 29 ++++++++++++++++++++++++++--- src/main.c | 8 +------- src/utils.c | 24 ------------------------ 3 files changed, 27 insertions(+), 34 deletions(-)
include/utils.h +26 -3
@@ -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);
src/main.c +1 -7
@@ -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)
src/utils.c +0 -24
@@ -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;
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerQuoted 3 lines · Pablo Neira Ayuso
So these functions can be used from
main.candcli.c.This patch reverts
7c0407c5ca53("nft: don't usexzalloc()").
Scratch this. Headers are missing, and then __memory_allocation_error needs to be inlined too.
I let you fix this, Phil, will not follow up on this.
Thanks.