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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
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))
/***====================================================================***/