From aea2c168fc9c12148181dbcc33d7085aad8e6d90 Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Fri, 10 Oct 2014 09:02:09 -0400 Subject: Add gpgme_set_global_malloc_hooks(). * configure.ac: Remove check for vasprintf(). * doc/gpgme.texi: Add documentation. * src/gpgme.h.in (gpgme_malloc_hooks,gpgme_malloc_hooks_t): New. * src/gpgme.h.in (gpgme_set_global_malloc_hooks): New prototype. * src/mem.c, src/mem.h: New. * src/gpgme.c (gpgme_set_global_malloc_hooks): New. * src/gpgme.def, src/libgpgme.vers: Add gpgme_set_global_malloc_hooks. * src/vasprintf.c (vasprintf): Rename to _gpgme_vasprintf(). * src/vasprintf.c (asprintf): Rename to _gpgme_asprintf(). * src/Makefile.am: Add mem.c, mem.h and vasprintf.c to main_sources. * src/assuan-support.c, src/ath-pthread.c, src/conversion.c, src/data-compat.c, src/data-identify.c, src/data-mem.c, src/data.c, src/debug.c, src/decrypt.c, src/dirinfo.c, src/encrypt.c, src/engine-assuan.c, src/engine-g13.c, src/engine-gpg.c, src/engine-gpgconf.c, src/engine-gpgsm.c, src/engine-spawn.c, src/engine-uiserver.c, src/engine.c, src/export.c, src/genkey.c, src/get-env.c, src/gpgme-w32spawn.c, src/gpgme.c, src/import.c, src/key.c, src/keylist.c, src/op-support.c, src/passphrase.c, src/posix-io.c, src/posix-util.c, src/progress.c, src/setenv.c, src/sig-notation.c, src/sign.c, src/signers.c, src/trust-item.c, src/trustlist.c, src/util.h, src/verify.c, src/version.c, src/vfs-create.c, src/vfs-mount.c, src/w32-ce.c, src/w32-ce.h, src/w32-glib-io.c, src/w32-io.c, src/w32-sema.c, src/w32-util.c, src/wait-global.c, src/wait.c: Change allocation functions: free() to _gpgme_free(), malloc() to _gpgme_malloc(), calloc() to _gpgme_calloc(), realloc() to _gpgme_realloc() and strdup() to _gpgme_strdup(). -- Mostly borrowed from libassuan for custom memory handling. This changes vasprintf.c to implement _gpgme_asprintf() and _gpgme_vasprintf() and will always use the built-in vasprintf() even when provided by the OS. --- src/mem.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/mem.c (limited to 'src/mem.c') diff --git a/src/mem.c b/src/mem.c new file mode 100644 index 00000000..99ae41a5 --- /dev/null +++ b/src/mem.c @@ -0,0 +1,69 @@ +/* gpgme.c - GnuPG Made Easy. + Copyright (C) 2014 g10 Code GmbH + + This file is part of GPGME. + + GPGME is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of + the License, or (at your option) any later version. + + GPGME is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this program; if not, see . + */ +#if HAVE_CONFIG_H +#include +#endif +#include +#include +#include "gpgme.h" +#include "util.h" +#include "mem.h" + +/* The default memory management functions. */ +static struct gpgme_malloc_hooks _gpgme_default_malloc_hooks = + { malloc, calloc, realloc, free }; + +void * +_gpgme_malloc (size_t size) +{ + return _gpgme_default_malloc_hooks.malloc (size); +} + +void * +_gpgme_calloc (size_t nmemb, size_t size) +{ + return _gpgme_default_malloc_hooks.calloc (nmemb, size); +} + +void * +_gpgme_realloc (void *p, size_t size) +{ + return _gpgme_default_malloc_hooks.realloc (p, size); +} + +void +_gpgme_free (void *p) +{ + _gpgme_default_malloc_hooks.free (p); +} + +char * +_gpgme_strdup (const char *s) +{ + char *r = _gpgme_malloc (strlen(s)+1); + + stpcpy (r, s); + return r; +} + +void +_gpgme_set_global_malloc_hooks (gpgme_malloc_hooks_t malloc_hooks) +{ + _gpgme_default_malloc_hooks = *malloc_hooks; +} -- cgit v1.2.3