chiark / gitweb /
NEW etc.: Provide size-safe wrappers for safe_malloc et al.
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 7 Oct 2014 19:28:33 +0000 (20:28 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 21 Oct 2014 00:07:12 +0000 (01:07 +0100)
These automatically compute the size from the type to be assigned to,
and automatically generate a context string.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
secnet.h

index 96717ae..8e5dad0 100644 (file)
--- a/secnet.h
+++ b/secnet.h
@@ -180,6 +180,16 @@ extern void *safe_malloc_ary(size_t size, size_t count, const char *message);
 extern void *safe_realloc_ary(void *p, size_t size, size_t count,
                              const char *message);
 
+#define NEW(p)                                 \
+    ((p)=safe_malloc(sizeof(*(p)),             \
+                    __FILE__ ":" #p))
+#define NEW_ARY(p,count)                                       \
+    ((p)=safe_malloc_ary(sizeof(*(p)),(count),                 \
+                        __FILE__ ":" #p "[" #count "]"))
+#define REALLOC_ARY(p,count)                                   \
+    ((p)=safe_realloc_ary((p),sizeof(*(p)),(count),            \
+                         __FILE__ ":" #p "[" #count "]"))
+
 void setcloexec(int fd); /* cannot fail */
 void setnonblock(int fd); /* cannot fail */
 void pipe_cloexec(int fd[2]); /* pipe(), setcloexec() twice; cannot fail */