mirror of
git://git.gnupg.org/gnupg.git
synced 2025-07-02 22:46:30 +02:00
Add code to allow for late memory cleanup.
* common/init.c (mem_cleanup_item_t): New. (run_mem_cleanup): New. (_init_common_subsystems): Add an atexit for it. (register_mem_cleanup_func): New. * g10/kbnode.c (cleanup_registered): New. (release_unused_nodes): New. (alloc_node): Call register_mem_cleanup_func. -- It is often time consuming to figure out whether still allocated memory at process termination is fine (e.g. a cache) or a problem. To help for that register_mem_cleanup_func may now be used to cleanup such memory. The run time of the program will be longer; if that turns out to be a problem we can change the code to only run in debugging mode.
This commit is contained in:
parent
44159b681f
commit
2739834206
3 changed files with 90 additions and 18 deletions
59
g10/kbnode.c
59
g10/kbnode.c
|
@ -31,35 +31,58 @@
|
|||
|
||||
#define USE_UNUSED_NODES 1
|
||||
|
||||
static int cleanup_registered;
|
||||
static KBNODE unused_nodes;
|
||||
|
||||
static KBNODE
|
||||
alloc_node(void)
|
||||
#if USE_UNUSED_NODES
|
||||
static void
|
||||
release_unused_nodes (void)
|
||||
{
|
||||
KBNODE n;
|
||||
while (unused_nodes)
|
||||
{
|
||||
kbnode_t next = unused_nodes->next;
|
||||
xfree (unused_nodes);
|
||||
unused_nodes = next;
|
||||
}
|
||||
}
|
||||
#endif /*USE_UNUSED_NODES*/
|
||||
|
||||
n = unused_nodes;
|
||||
if( n )
|
||||
unused_nodes = n->next;
|
||||
else
|
||||
n = xmalloc( sizeof *n );
|
||||
n->next = NULL;
|
||||
n->pkt = NULL;
|
||||
n->flag = 0;
|
||||
n->private_flag=0;
|
||||
n->recno = 0;
|
||||
return n;
|
||||
|
||||
static kbnode_t
|
||||
alloc_node (void)
|
||||
{
|
||||
kbnode_t n;
|
||||
|
||||
n = unused_nodes;
|
||||
if (n)
|
||||
unused_nodes = n->next;
|
||||
else
|
||||
{
|
||||
if (!cleanup_registered)
|
||||
{
|
||||
cleanup_registered = 1;
|
||||
register_mem_cleanup_func (release_unused_nodes);
|
||||
}
|
||||
n = xmalloc (sizeof *n);
|
||||
}
|
||||
n->next = NULL;
|
||||
n->pkt = NULL;
|
||||
n->flag = 0;
|
||||
n->private_flag=0;
|
||||
n->recno = 0;
|
||||
return n;
|
||||
}
|
||||
|
||||
static void
|
||||
free_node( KBNODE n )
|
||||
{
|
||||
if( n ) {
|
||||
if (n)
|
||||
{
|
||||
#if USE_UNUSED_NODES
|
||||
n->next = unused_nodes;
|
||||
unused_nodes = n;
|
||||
n->next = unused_nodes;
|
||||
unused_nodes = n;
|
||||
#else
|
||||
xfree( n );
|
||||
xfree (n);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue