Skip to content

Commit 4f8c353

Browse files
authored
Merge pull request #209 from libtom/reintroduce-realloc
Reintroduce calloc
2 parents ec4149d + a3651cb commit 4f8c353

File tree

3 files changed

+5
-17
lines changed

3 files changed

+5
-17
lines changed

bn_mp_init.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,12 @@
1515
/* init a new mp_int */
1616
int mp_init(mp_int *a)
1717
{
18-
int i;
19-
2018
/* allocate memory required and clear it */
21-
a->dp = (mp_digit *) XMALLOC(MP_PREC * sizeof(mp_digit));
19+
a->dp = (mp_digit *) XCALLOC((size_t)MP_PREC, sizeof(mp_digit));
2220
if (a->dp == NULL) {
2321
return MP_MEM;
2422
}
2523

26-
/* set the digits to zero */
27-
for (i = 0; i < MP_PREC; i++) {
28-
a->dp[i] = 0;
29-
}
30-
3124
/* set the used to zero, allocated digits to the default precision
3225
* and sign to positive */
3326
a->used = 0;

bn_mp_init_size.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
/* init an mp_init for a given size */
1616
int mp_init_size(mp_int *a, int size)
1717
{
18-
int x;
19-
2018
/* pad size so there are always extra digits */
2119
size += (MP_PREC * 2) - (size % MP_PREC);
2220

2321
/* alloc mem */
24-
a->dp = (mp_digit *) XMALLOC((size_t)size * sizeof(mp_digit));
22+
a->dp = (mp_digit *) XCALLOC((size_t)size, sizeof(mp_digit));
2523
if (a->dp == NULL) {
2624
return MP_MEM;
2725
}
@@ -31,11 +29,6 @@ int mp_init_size(mp_int *a, int size)
3129
a->alloc = size;
3230
a->sign = MP_ZPOS;
3331

34-
/* zero the digits */
35-
for (x = 0; x < size; x++) {
36-
a->dp[x] = 0;
37-
}
38-
3932
return MP_OKAY;
4033
}
4134
#endif

tommath_private.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ extern "C" {
3030
#ifndef XMALLOC
3131
/* default to libc stuff */
3232
# define XMALLOC(size) malloc(size)
33-
# define XFREE(mem, size) free(mem)
3433
# define XREALLOC(mem, oldsize, newsize) realloc(mem, newsize)
34+
# define XCALLOC(nmemb, size) calloc(nmemb, size)
35+
# define XFREE(mem, size) free(mem)
3536
#else
3637
/* prototypes for our heap functions */
3738
extern void *XMALLOC(size_t size);
3839
extern void *XREALLOC(void *mem, size_t oldsize, size_t newsize);
40+
extern void *XCALLOC(size_t nmemb, size_t size);
3941
extern void XFREE(void *mem, size_t size);
4042
#endif
4143

0 commit comments

Comments
 (0)