summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2014-08-26 17:56:44 +0200
committerWerner Koch <wk@gnupg.org>2014-08-26 17:56:44 +0200
commit7fdca61bcf60e730177889fbbd2f935ba33ae0c3 (patch)
tree80e9fcb444debc32dd1569bf033dad9dbaf28c7a
parentd9d5b61a9f70556e8fc0775f1501380f65ce9502 (diff)
downloadlibgpg-error-7fdca61bcf60e730177889fbbd2f935ba33ae0c3.tar.gz
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.
-rw-r--r--NEWS1
-rw-r--r--configure.ac5
-rw-r--r--src/estream-printf.c2
-rw-r--r--src/estream.c35
-rw-r--r--src/gpg-error.def.in2
-rw-r--r--src/gpg-error.h.in10
-rw-r--r--src/gpg-error.vers1
-rw-r--r--src/gpgrt-int.h7
-rw-r--r--src/init.c57
-rw-r--r--src/visibility.c6
-rw-r--r--src/visibility.h8
11 files changed, 108 insertions, 26 deletions
diff --git a/NEWS b/NEWS
index 35532e5..6099d5b 100644
--- a/NEWS
+++ b/NEWS
@@ -13,6 +13,7 @@ Noteworthy changes in version 1.14 (unreleased)
gpgrt_check_version NEW.
gpgrt_lock_trylock NEW.
gpgrt_set_syscall_clamp NEW.
+ gpgrt_set_alloc_func NEW.
gpgrt_stream_t NEW.
gpgrt_cookie_io_functions_t NEW.
gpgrt_syshd_t NEW.
diff --git a/configure.ac b/configure.ac
index cf0d37c..b155437 100644
--- a/configure.ac
+++ b/configure.ac
@@ -136,6 +136,11 @@ AH_BOTTOM([
#if defined(HAVE_W32_SYSTEM) && !defined(ENABLE_NLS)
#define ENABLE_NLS 1
#endif
+
+/* Connect the generic estream-printf.c to our framework. */
+#define _ESTREAM_PRINTF_REALLOC _gpgrt_realloc
+#define _ESTREAM_PRINTF_EXTRA_INCLUDE "gpgrt-int.h"
+
/* For building we need to define these macro. */
#define GPG_ERR_ENABLE_GETTEXT_MACROS 1
#define GPG_ERR_ENABLE_ERRNO_MACROS 1
diff --git a/src/estream-printf.c b/src/estream-printf.c
index b1eb828..39a813f 100644
--- a/src/estream-printf.c
+++ b/src/estream-printf.c
@@ -446,7 +446,7 @@ compute_type (argspec_t arg)
at the address ARGSPECS_ADDR. The caller has provided enough space
to store up to MAX_ARGSPECS in that buffer. The function may
however ignore the provided buffer and malloc a larger one. On
- success the addrrss of that larger buffer will be stored at
+ success the address of that larger buffer will be stored at
ARGSPECS_ADDR. The actual number of specifications will be
returned at R_ARGSPECS_COUNT. */
static int
diff --git a/src/estream.c b/src/estream.c
index cf3067b..7a5646e 100644
--- a/src/estream.c
+++ b/src/estream.c
@@ -242,28 +242,23 @@ static void fname_set_internal (estream_t stream, const char *fname, int quote);
-/* Malloc wrappers to overcome problems on some older OSes. */
static void *
mem_alloc (size_t n)
{
- if (!n)
- n++;
- return malloc (n);
+ return _gpgrt_malloc (n);
}
static void *
mem_realloc (void *p, size_t n)
{
- if (!p)
- return mem_alloc (n);
- return realloc (p, n);
+ return _gpgrt_realloc (p, n);
}
static void
mem_free (void *p)
{
if (p)
- free (p);
+ _gpgrt_free (p);
}
#ifdef HAVE_W32_SYSTEM
@@ -518,7 +513,7 @@ _gpgrt_es_init (void)
should be used before any I/O happens. The function is commonly
used with the nPth library:
- gpgrt_set_syscall_clamp (npth_protect, npth_unprotect);
+ gpgrt_set_syscall_clamp (npth_unprotect, npth_protect);
These functions may not modify ERRNO.
*/
@@ -3210,7 +3205,7 @@ _gpgrt_fclose (estream_t stream)
the buffer. On error NULL is stored at R_BUFFER. Note that if no
write operation has happened, NULL may also be stored at BUFFER on
success. The caller needs to release the returned memory using
- es_free. */
+ gpgrt_free. */
int
_gpgrt_fclose_snatch (estream_t stream, void **r_buffer, size_t *r_buflen)
{
@@ -3835,7 +3830,7 @@ _gpgrt_getline (char *_GPGRT__RESTRICT *_GPGRT__RESTRICT lineptr,
Note: The returned buffer is allocated with enough extra space to
allow the caller to append a CR,LF,Nul. The buffer should be
- released using es_free.
+ released using gpgrt_free.
*/
ssize_t
_gpgrt_read_line (estream_t stream,
@@ -3922,14 +3917,16 @@ _gpgrt_read_line (estream_t stream,
return nbytes;
}
-/* Wrapper around free() to match the memory allocation system used
- by estream. Should be used for all buffers returned to the caller
- by libestream. */
-void
-_gpgrt_free (void *a)
-{
- mem_free (a);
-}
+/* Wrapper around free() to match the memory allocation system used by
+ estream. Should be used for all buffers returned to the caller by
+ libestream. If a custom allocation handler has been set with
+ gpgrt_set_alloc_func that register function may be used
+ instead. This function has been moved to init.c. */
+/* void */
+/* _gpgrt_free (void *a) */
+/* { */
+/* mem_free (a); */
+/* } */
int
diff --git a/src/gpg-error.def.in b/src/gpg-error.def.in
index b318d61..ac20a69 100644
--- a/src/gpg-error.def.in
+++ b/src/gpg-error.def.in
@@ -135,6 +135,6 @@ EXPORTS
gpgrt_check_version @100
gpg_err_init @101
gpg_err_deinit @102
-
+ gpgrt_set_alloc_func @103
;; end of file with public symbols for Windows.
diff --git a/src/gpg-error.h.in b/src/gpg-error.h.in
index 8c008ac..4b1c0a0 100644
--- a/src/gpg-error.h.in
+++ b/src/gpg-error.h.in
@@ -181,13 +181,17 @@ gpg_error_t gpg_err_init (void) _GPG_ERR_CONSTRUCTOR;
# define gpgrt_init() do { ; } while (0)
#endif
-/* Register blocking system I/O clamping functions. */
-void gpgrt_set_syscall_clamp (void (*pre)(void), void (*post)(void));
-
/* See the source on how to use the deinit function; it is usually not
required. */
void gpg_err_deinit (int mode);
+/* Register blocking system I/O clamping functions. */
+void gpgrt_set_syscall_clamp (void (*pre)(void), void (*post)(void));
+
+/* Register a custom malloc/realloc/free function. */
+void gpgrt_set_alloc_func (void *(*f)(void *a, size_t n));
+
+
/* Constructor and accessor functions. */
diff --git a/src/gpg-error.vers b/src/gpg-error.vers
index 0847cfe..43becea 100644
--- a/src/gpg-error.vers
+++ b/src/gpg-error.vers
@@ -118,6 +118,7 @@ GPG_ERROR_1.0 {
gpgrt_check_version;
gpg_err_init;
gpg_err_deinit;
+ gpgrt_set_alloc_func;
local:
*;
diff --git a/src/gpgrt-int.h b/src/gpgrt-int.h
index a029ac0..df7c606 100644
--- a/src/gpgrt-int.h
+++ b/src/gpgrt-int.h
@@ -34,6 +34,12 @@ void _gpg_err_set_errno (int err);
gpg_error_t _gpg_err_init (void);
void _gpg_err_deinit (int mode);
+void _gpgrt_set_alloc_func (void *(*f)(void *a, size_t n));
+
+void *_gpgrt_realloc (void *a, size_t n);
+void *_gpgrt_malloc (size_t n);
+void _gpgrt_free (void *a);
+
const char *_gpg_error_check_version (const char *req_version);
gpg_err_code_t _gpgrt_lock_init (gpgrt_lock_t *lockhd);
@@ -162,7 +168,6 @@ ssize_t _gpgrt_getline (char *_GPGRT__RESTRICT *_GPGRT__RESTRICT lineptr,
ssize_t _gpgrt_read_line (gpgrt_stream_t stream,
char **addr_of_buffer, size_t *length_of_buffer,
size_t *max_length);
-void _gpgrt_free (void *a);
int _gpgrt_fprintf (gpgrt_stream_t _GPGRT__RESTRICT stream,
const char *_GPGRT__RESTRICT format, ...)
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
diff --git a/src/visibility.c b/src/visibility.c
index 829c720..f1bbca6 100644
--- a/src/visibility.c
+++ b/src/visibility.c
@@ -96,6 +96,12 @@ gpgrt_set_syscall_clamp (void (*pre)(void), void (*post)(void))
_gpgrt_set_syscall_clamp (pre, post);
}
+void
+gpgrt_set_alloc_func (void *(*f)(void *a, size_t n))
+{
+ _gpgrt_set_alloc_func (f);
+}
+
gpg_err_code_t
gpgrt_lock_init (gpgrt_lock_t *lockhd)
diff --git a/src/visibility.h b/src/visibility.h
index d0ef0bf..feeb8d1 100644
--- a/src/visibility.h
+++ b/src/visibility.h
@@ -52,6 +52,7 @@ MARK_VISIBLE (gpg_err_code_from_syserror)
MARK_VISIBLE (gpg_err_set_errno)
MARK_VISIBLE (gpg_err_init)
+MARK_VISIBLE (gpg_err_deinit)
MARK_VISIBLE (gpg_error_check_version)
MARK_VISIBLE (gpgrt_check_version)
@@ -136,6 +137,7 @@ MARK_VISIBLE (gpgrt_vbsprintf)
MARK_VISIBLE (gpgrt_snprintf)
MARK_VISIBLE (gpgrt_vsnprintf)
MARK_VISIBLE (gpgrt_set_syscall_clamp)
+MARK_VISIBLE (gpgrt_set_alloc_func)
#undef MARK_VISIBLE
@@ -153,9 +155,9 @@ MARK_VISIBLE (gpgrt_set_syscall_clamp)
#define gpg_err_set_errno _gpgrt_USE_UNDERSCORED_FUNCTION
#define gpg_err_init _gpgrt_USE_UNDERSCORED_FUNCTION
+#define gpg_err_deinit _gpgrt_USE_UNDERSCORED_FUNCTION
#define gpg_error_check_version _gpgrt_USE_UNDERSCORED_FUNCTION
#define gpgrt_check_version _gpgrt_USE_OTHER_FUNCTION
-#define gpgrt_set_syscall_clamp _gpgrt_USE_UNDERSCORED_FUNCTION
#define gpgrt_lock_init _gpgrt_USE_UNDERSCORED_FUNCTION
#define gpgrt_lock_lock _gpgrt_USE_UNDERSCORED_FUNCTION
@@ -238,6 +240,10 @@ MARK_VISIBLE (gpgrt_set_syscall_clamp)
#define gpgrt_snprintf _gpgrt_USE_UNDERSCORED_FUNCTION
#define gpgrt_vsnprintf _gpgrt_USE_UNDERSCORED_FUNCTION
+#define gpgrt_set_syscall_clamp _gpgrt_USE_UNDERSCORED_FUNCTION
+#define gpgrt_set_alloc_func _gpgrt_USE_UNDERSCORED_FUNCTION
+
+
#endif /*!_GPGRT_INCL_BY_VISIBILITY_C*/
#endif /*_GPGRT_VISIBILITY_H*/