aboutsummaryrefslogtreecommitdiffstats
path: root/src/init.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2014-08-26 15:56:44 +0000
committerWerner Koch <[email protected]>2014-08-26 15:56:44 +0000
commit7fdca61bcf60e730177889fbbd2f935ba33ae0c3 (patch)
tree80e9fcb444debc32dd1569bf033dad9dbaf28c7a /src/init.c
parentExport missing init functions. (diff)
downloadlibgpg-error-7fdca61bcf60e730177889fbbd2f935ba33ae0c3.tar.gz
libgpg-error-7fdca61bcf60e730177889fbbd2f935ba33ae0c3.zip
Add gpgrt_set_alloc_func.
* src/visibility.c (gpgrt_set_alloc_func): New. * configure.ac (_ESTREAM_PRINTF_REALLOC): Define. (_ESTREAM_PRINTF_EXTRA_INCLUDE): Define. * src/estream.c (mem_alloc, mem_realloc, mem_free): Simplify. (_gpgrt_free): Remove. * src/init.c (custom_realloc): New var. (_gpgrt_set_alloc_func): New. (_gpgrt_realloc, _gpgrt_malloc, _gpgrt_free): New. * src/visibility.h (gpg_err_deinit): Mark as visible.
Diffstat (limited to 'src/init.c')
-rw-r--r--src/init.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/init.c b/src/init.c
index 16cdfed..6305fe3 100644
--- a/src/init.c
+++ b/src/init.c
@@ -60,6 +60,11 @@ static void drop_locale_dir (char *locale_dir);
#endif /*!HAVE_W32_SYSTEM*/
+
+/* The realloc function as set by gpgrt_set_alloc_func. */
+static void *(*custom_realloc)(void *a, size_t n);
+
+
static void
real_init (void)
@@ -146,6 +151,58 @@ _gpg_err_deinit (int mode)
}
+
+
+/* Register F as allocation function. This function is used for all
+ APIs which return an allocated buffer. F needs to have standard
+ realloc semantics. It should be called as early as possible and
+ not changed later. */
+void
+_gpgrt_set_alloc_func (void *(*f)(void *a, size_t n))
+{
+ custom_realloc = f;
+}
+
+
+/* The realloc to be used for data returned by the public API. */
+void *
+_gpgrt_realloc (void *a, size_t n)
+{
+ if (custom_realloc)
+ return custom_realloc (a, n);
+
+ if (!a)
+ return malloc (n);
+
+ if (!n)
+ {
+ free (a);
+ return NULL;
+ }
+
+ return realloc (a, n);
+}
+
+
+/* The malloc to be used for data returned by the public API. */
+void *
+_gpgrt_malloc (size_t n)
+{
+ if (!n)
+ n++;
+ return _gpgrt_realloc (NULL, n);
+}
+
+
+/* The free to be used for data returned by the public API. */
+void
+_gpgrt_free (void *a)
+{
+ _gpgrt_realloc (a, 0);
+}
+
+
+
#ifdef HAVE_W32_SYSTEM