Skip to content

Reintroduce calloc #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions bn_mp_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 1 addition & 8 deletions bn_mp_init_size.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tommath_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down