Merge pull request #209 from libtom/reintroduce-realloc Reintroduce calloc
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
diff --git a/bn_mp_init.c b/bn_mp_init.c
index 3c0c489..29af64b 100644
--- a/bn_mp_init.c
+++ b/bn_mp_init.c
@@ -15,19 +15,12 @@
/* init a new mp_int */
int mp_init(mp_int *a)
{
- int i;
-
/* allocate memory required and clear it */
- a->dp = (mp_digit *) XMALLOC(MP_PREC * sizeof(mp_digit));
+ a->dp = (mp_digit *) XCALLOC((size_t)MP_PREC, sizeof(mp_digit));
if (a->dp == NULL) {
return MP_MEM;
}
- /* set the digits to zero */
- for (i = 0; i < MP_PREC; i++) {
- a->dp[i] = 0;
- }
-
/* set the used to zero, allocated digits to the default precision
* and sign to positive */
a->used = 0;
diff --git a/bn_mp_init_size.c b/bn_mp_init_size.c
index 1becb23..1545842 100644
--- a/bn_mp_init_size.c
+++ b/bn_mp_init_size.c
@@ -15,13 +15,11 @@
/* init an mp_init for a given size */
int mp_init_size(mp_int *a, int size)
{
- int x;
-
/* pad size so there are always extra digits */
size += (MP_PREC * 2) - (size % MP_PREC);
/* alloc mem */
- a->dp = (mp_digit *) XMALLOC((size_t)size * sizeof(mp_digit));
+ a->dp = (mp_digit *) XCALLOC((size_t)size, sizeof(mp_digit));
if (a->dp == NULL) {
return MP_MEM;
}
@@ -31,11 +29,6 @@ int mp_init_size(mp_int *a, int size)
a->alloc = size;
a->sign = MP_ZPOS;
- /* zero the digits */
- for (x = 0; x < size; x++) {
- a->dp[x] = 0;
- }
-
return MP_OKAY;
}
#endif
diff --git a/tommath_private.h b/tommath_private.h
index 057f878..64a8794 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -30,12 +30,14 @@ extern "C" {
#ifndef XMALLOC
/* default to libc stuff */
# define XMALLOC(size) malloc(size)
-# define XFREE(mem, size) free(mem)
# define XREALLOC(mem, oldsize, newsize) realloc(mem, newsize)
+# define XCALLOC(nmemb, size) calloc(nmemb, size)
+# define XFREE(mem, size) free(mem)
#else
/* prototypes for our heap functions */
extern void *XMALLOC(size_t size);
extern void *XREALLOC(void *mem, size_t oldsize, size_t newsize);
+extern void *XCALLOC(size_t nmemb, size_t size);
extern void XFREE(void *mem, size_t size);
#endif