Commit 8f4b6028a8f19d188b9d56a2690f8f7818b24b4f

Con Kolivas 2014-04-05T10:01:26

Provide helper function for recallocing memory

diff --git a/util.c b/util.c
index 05f635c..17f4e61 100644
--- a/util.c
+++ b/util.c
@@ -1524,6 +1524,18 @@ static void clear_sock(struct pool *pool)
 	clear_sockbuf(pool);
 }
 
+/* Realloc memory to new size and zero any extra memory added */
+void _recalloc(void *ptr, size_t old, size_t new, const char *file, const char *func, const int line)
+{
+	if (new == old)
+		return;
+	ptr = realloc(ptr, new);
+	if (unlikely(!ptr))
+		quitfrom(1, file, func, line, "Failed to realloc");
+	if (new > old)
+		memset(ptr + old, 0, new - old);
+}
+
 /* Make sure the pool sockbuf is large enough to cope with any coinbase size
  * by reallocing it to a large enough size rounded up to a multiple of RBUFSIZE
  * and zeroing the new memory */
diff --git a/util.h b/util.h
index f78b015..f122983 100644
--- a/util.h
+++ b/util.h
@@ -137,6 +137,8 @@ int ms_tdiff(struct timeval *end, struct timeval *start);
 double tdiff(struct timeval *end, struct timeval *start);
 bool stratum_send(struct pool *pool, char *s, ssize_t len);
 bool sock_full(struct pool *pool);
+void _recalloc(void *ptr, size_t old, size_t new, const char *file, const char *func, const int line);
+#define recalloc(ptr, old, new) _recalloc(ptr, old, new, __FILE__, __func__, __LINE__)
 char *recv_line(struct pool *pool);
 bool parse_method(struct pool *pool, char *s);
 bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port);