common,gpg,sm: Restrict the use of algorithms according to CO_DE_VS.
[gnupg.git] / g10 / kbnode.c
index 1a8b91e..c2aaacd 100644 (file)
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
  */
 
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <assert.h>
 
 #include "gpg.h"
-#include "util.h"
+#include "../common/util.h"
+#include "../common/init.h"
 #include "packet.h"
 #include "keydb.h"
 
 #define USE_UNUSED_NODES 1
 
+static int cleanup_registered;
 static KBNODE unused_nodes;
 
-static KBNODE
-alloc_node(void)
+static void
+release_unused_nodes (void)
 {
-    KBNODE n;
+#if USE_UNUSED_NODES
+  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
     }
 }
@@ -94,8 +117,8 @@ release_kbnode( KBNODE n )
     while( n ) {
        n2 = n->next;
        if( !is_cloned_kbnode(n) ) {
-           free_packet( n->pkt );
-           xfree( n->pkt );
+            free_packet (n->pkt, NULL);
+            xfree( n->pkt );
        }
        free_node( n );
        n = n2;
@@ -265,7 +288,7 @@ commit_kbnode( KBNODE *root )
            else
                nl->next = n->next;
            if( !is_cloned_kbnode(n) ) {
-               free_packet( n->pkt );
+                free_packet (n->pkt, NULL);
                xfree( n->pkt );
            }
            free_node( n );
@@ -289,7 +312,7 @@ remove_kbnode( KBNODE *root, KBNODE node )
            else
                nl->next = n->next;
            if( !is_cloned_kbnode(n) ) {
-               free_packet( n->pkt );
+                free_packet (n->pkt, NULL);
                xfree( n->pkt );
            }
            free_node( n );
@@ -369,10 +392,10 @@ dump_kbnode (KBNODE node)
           es_write_sanitized (log_get_stream (), uid->name, uid->len,
                               NULL, NULL);
           log_printf ("\" %c%c%c%c\n",
-                      uid->is_expired? 'e':'.',
-                      uid->is_revoked? 'r':'.',
+                      uid->flags.expired? 'e':'.',
+                      uid->flags.revoked? 'r':'.',
                       uid->created?    'v':'.',
-                      uid->is_primary? 'p':'.' );
+                      uid->flags.primary? 'p':'.' );
         }
       else if (node->pkt->pkttype == PKT_SIGNATURE)
         {
@@ -400,6 +423,8 @@ dump_kbnode (KBNODE node)
                       pk->flags.valid?    'v':'.',
                       pk->flags.mdc?   'm':'.');
         }
+      else
+        log_printf ("\n");
 
       log_flush ();
     }