From 80c18e1b212cc91946864db7a53da50e9f91b861 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Fri, 17 Nov 2017 11:00:39 +0100 Subject: core: New API functions gpgrt_malloc, gpgrt_calloc, and gpgrt_realloc. * src/visibility.c (gpgrt_realloc): New API function. (gpgrt_malloc): New API function. (gpgrt_calloc): New API function. * src/visibility.h: Add corresponding macros. * src/gpg-error.def.in: Add them. * src/gpg-error.vers: Add them. * src/init.c (_gpgrt_calloc): New. * src/gpg-error.h.in: Add prototypes. Signed-off-by: Werner Koch --- src/init.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/init.c') diff --git a/src/init.c b/src/init.c index b01e089..0154091 100644 --- a/src/init.c +++ b/src/init.c @@ -194,6 +194,27 @@ _gpgrt_malloc (size_t n) } +void * +_gpgrt_calloc (size_t n, size_t m) +{ + size_t bytes; + void *p; + + bytes = n * m; /* size_t is unsigned so the behavior on overflow is + defined. */ + if (m && bytes / m != n) + { + _gpg_err_set_errno (ENOMEM); + return NULL; + } + + p = _gpgrt_realloc (NULL, bytes); + if (p) + memset (p, 0, bytes); + return p; +} + + /* The free to be used for data returned by the public API. */ void _gpgrt_free (void *a) -- cgit v1.2.3