diff --git a/src/mem.c b/src/mem.c index 49429cc..f17fa61 100644 --- a/src/mem.c +++ b/src/mem.c @@ -53,3 +53,26 @@ void *ntru_malloc(size_t size) return ptr; } + +/** + * Allocate memory of size and return + * a void pointer. The memory is zeroed. + * + * @param nmemb amount of blocks to allocate + * @param size of the memory blocks to allocate in bytes + * @return void pointer to the beginning of the allocated memory block + */ +void *ntru_calloc(size_t nmemb, size_t size) +{ + void *ptr; + + ptr = calloc(nmemb, size); + + if (size) + if (!ptr) { + fprintf(stderr, "failed to allocate memory, aborting!"); + abort(); + } + + return ptr; +} diff --git a/src/mem.h b/src/mem.h index 8c0ca3c..201d1fc 100644 --- a/src/mem.h +++ b/src/mem.h @@ -31,5 +31,7 @@ #include void *ntru_malloc(size_t size); +void *ntru_calloc(size_t nmemb, size_t size); + #endif /* NTRU_MEM_H */