Commit 861b0c79c40786c5384adba208f353003b291849

Ran Benita 2012-03-23T23:47:26

Rewrite recalloc to the correct type The recalloc function should be expressed in terms of bytes to match its name. However uTypedRecalloc retains its type so nothing is changed. Signed-off-by: Ran Benita <ran234@gmail.com>

diff --git a/src/utils.c b/src/utils.c
index a9d59c2..031162e 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -34,20 +34,17 @@
 #include <string.h>
 
 void *
-recalloc(void * old, unsigned nOld, unsigned nNew, unsigned itemSize)
+recalloc(void *ptr, size_t old_size, size_t new_size)
 {
     char *rtrn;
 
-    if (old == NULL)
-        rtrn = calloc(nNew, itemSize);
-    else
-    {
-        rtrn = realloc(old, nNew * itemSize);
-        if ((rtrn) && (nNew > nOld))
-        {
-            memset(&rtrn[nOld * itemSize], 0, (nNew - nOld) * itemSize);
-        }
-    }
+    if (ptr == NULL)
+        return calloc(1, new_size);
+
+    rtrn = realloc(ptr, new_size);
+    if (rtrn && new_size > old_size)
+        memset(&rtrn[old_size], 0, new_size - old_size);
+
     return rtrn;
 }
 
diff --git a/src/utils.h b/src/utils.h
index 1cb7611..2fbf87a 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -38,12 +38,12 @@
 #endif
 
 extern void *
-recalloc(void * old, unsigned nOld, unsigned nNew, unsigned newSize);
+recalloc(void *ptr, size_t old_size, size_t new_size);
 
 #define	uTypedAlloc(t)		((t *)malloc((unsigned)sizeof(t)))
 #define	uTypedCalloc(n,t)	((t *)calloc((unsigned)n,(unsigned)sizeof(t)))
 #define	uTypedRealloc(pO,n,t)	((t *)realloc(pO,((unsigned)n)*sizeof(t)))
-#define	uTypedRecalloc(pO,o,n,t) ((t *)recalloc(pO,((unsigned)o),((unsigned)n),sizeof(t)))
+#define uTypedRecalloc(pO, o, n, t) recalloc(pO, (o) * sizeof(t), (n) * sizeof(t))
 
 /***====================================================================***/