diff options
Diffstat (limited to '')
-rw-r--r-- | common/init.c | 47 | ||||
-rw-r--r-- | common/init.h | 2 | ||||
-rw-r--r-- | common/iobuf.c | 5 | ||||
-rw-r--r-- | common/logging.c | 27 | ||||
-rw-r--r-- | common/logging.h | 2 |
5 files changed, 81 insertions, 2 deletions
diff --git a/common/init.c b/common/init.c index e00b9b308..8a0b6a8e7 100644 --- a/common/init.c +++ b/common/init.c @@ -46,6 +46,21 @@ #include "util.h" +/* This object is used to register memory cleanup functions. + Technically they are not needed but they can avoid frequent + questions about un-released memory. Note that we use the system + malloc and not any wrappers. */ +struct mem_cleanup_item_s; +typedef struct mem_cleanup_item_s *mem_cleanup_item_t; + +struct mem_cleanup_item_s +{ + mem_cleanup_item_t next; + void (*func) (void); +}; + +static mem_cleanup_item_t mem_cleanup_list; + /* The default error source of the application. This is different from GPG_ERR_SOURCE_DEFAULT in that it does not depend on the @@ -65,6 +80,36 @@ sleep_on_exit (void) #endif /*HAVE_W32CE_SYSTEM*/ +static void +run_mem_cleanup (void) +{ + mem_cleanup_item_t next; + + while (mem_cleanup_list) + { + next = mem_cleanup_list->next; + mem_cleanup_list->func (); + free (mem_cleanup_list); + mem_cleanup_list = next; + } +} + + +void +register_mem_cleanup_func (void (*func)(void)) +{ + mem_cleanup_item_t item; + + item = malloc (sizeof *item); + if (item) + { + item->func = func; + item->next = mem_cleanup_list; + mem_cleanup_list = item; + } +} + + /* If STRING is not NULL write string to es_stdout or es_stderr. MODE must be 1 or 2. If STRING is NULL flush the respective stream. */ static int @@ -100,6 +145,8 @@ _init_common_subsystems (gpg_err_source_t errsource, int *argcp, char ***argvp) /* Store the error source in a gloabl variable. */ default_errsource = errsource; + atexit (run_mem_cleanup); + /* Try to auto set the character set. */ set_native_charset (NULL); diff --git a/common/init.h b/common/init.h index 633ffac52..eea2eb167 100644 --- a/common/init.h +++ b/common/init.h @@ -36,6 +36,8 @@ # error GPG_ERR_SOURCE_DEFAULT has default value #endif +void register_mem_cleanup_func (void (*func)(void)); + void _init_common_subsystems (gpg_err_source_t errsource, int *argcp, char ***argvp); #define init_common_subsystems(a,b) \ diff --git a/common/iobuf.c b/common/iobuf.c index 3ba3582d0..a3058303d 100644 --- a/common/iobuf.c +++ b/common/iobuf.c @@ -2311,7 +2311,7 @@ iobuf_seek (iobuf_t a, off_t newpos) } clearerr (fp); } - else + else if (a->use != 3) /* Not a temp stream. */ { for (; a; a = a->chain) { @@ -2338,7 +2338,8 @@ iobuf_seek (iobuf_t a, off_t newpos) } #endif } - a->d.len = 0; /* discard buffer */ + if (a->use != 3) + a->d.len = 0; /* Discard the buffer unless it is a temp stream. */ a->d.start = 0; a->nbytes = 0; a->nlimit = 0; diff --git a/common/logging.c b/common/logging.c index f91671e79..cdfd6597c 100644 --- a/common/logging.c +++ b/common/logging.c @@ -857,6 +857,33 @@ log_printhex (const char *text, const void *buffer, size_t length) } +void +log_clock (const char *string) +{ +#if 0 + static unsigned long long initial; + struct timespec tv; + unsigned long long now; + + if (clock_gettime (CLOCK_REALTIME, &tv)) + { + log_debug ("error getting the realtime clock value\n"); + return; + } + now = tv.tv_sec * 1000000000ull; + now += tv.tv_nsec; + + if (!initial) + initial = now; + + log_debug ("[%6llu] %s", (now - initial)/1000, string); +#else + /* You need to link with -ltr to enable the above code. */ + log_debug ("[not enabled in the source] %s", string); +#endif +} + + #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) void bug_at( const char *file, int line, const char *func ) diff --git a/common/logging.h b/common/logging.h index b0d662b71..89913e6e5 100644 --- a/common/logging.h +++ b/common/logging.h @@ -96,5 +96,7 @@ void log_flush (void); by the hexdump and a final LF. */ void log_printhex (const char *text, const void *buffer, size_t length); +void log_clock (const char *string); + #endif /*LIBJNLIB_LOGGING_H*/ |