gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r1169 - GNUnet/src/util


From: grothoff
Subject: [GNUnet-SVN] r1169 - GNUnet/src/util
Date: Thu, 30 Jun 2005 10:51:11 -0700 (PDT)

Author: grothoff
Date: 2005-06-30 10:51:07 -0700 (Thu, 30 Jun 2005)
New Revision: 1169

Modified:
   GNUnet/src/util/initialize.c
   GNUnet/src/util/kblockkey.c
Log:
caching kblocks

Modified: GNUnet/src/util/initialize.c
===================================================================
--- GNUnet/src/util/initialize.c        2005-06-30 17:50:53 UTC (rev 1168)
+++ GNUnet/src/util/initialize.c        2005-06-30 17:51:07 UTC (rev 1169)
@@ -33,6 +33,10 @@
 
 void doneXmalloc();
 
+void initKBlockKey();
+
+void doneKBlockKey();
+
 /**
  * Initialize Random number generator.
  */
@@ -116,6 +120,7 @@
   initLockingGcrypt();
   initRAND();
   initXmalloc();
+  initKBlockKey();
   initConfiguration();
   if (argc > 0)
     setConfigurationString("MAIN",
@@ -149,6 +154,7 @@
 #ifdef MINGW
   ShutdownWinEnv();
 #endif
+  doneKBlockKey();
   doneLockingGcrypt();
   doneXmalloc();
   gnunet_util_doneIO();

Modified: GNUnet/src/util/kblockkey.c
===================================================================
--- GNUnet/src/util/kblockkey.c 2005-06-30 17:50:53 UTC (rev 1168)
+++ GNUnet/src/util/kblockkey.c 2005-06-30 17:51:07 UTC (rev 1169)
@@ -1,7 +1,7 @@
 /*
      This file is part of GNUnet.
      Copyright (C) 1994, 1996, 1998, 2001, 2002, 2003 Free Software 
Foundation, Inc.
-     Copyright (C) 2004 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2004, 2005 Christian Grothoff (and other contributing 
authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -428,7 +428,8 @@
  * Deterministically (!) create a hostkey using only the
  * given HashCode as input to the PRNG.
  */
-struct PrivateKey * makeKblockKey(const HashCode512 * hc) {
+static PrivateKeyEncoded * 
+makeKblockKeyInternal(const HashCode512 * hc) {
   KBlock_secret_key sk;
   HashCode512 hx;
   void * pbu[6];
@@ -436,7 +437,6 @@
   size_t sizes[6];
   PrivateKeyEncoded * retval;
   int i;
-  struct PrivateKey * ret;
   size_t size;
 
   hx = *hc;
@@ -507,11 +507,65 @@
     mpz_clear(*pkv[i]);
     free(pbu[i]);
   }
+  return retval;
+}
 
-  ret = decodePrivateKey(retval);
-  FREE(retval);
-  return ret;
+typedef struct {
+  HashCode512 hc;
+  PrivateKeyEncoded * pke;
+} KBlockKeyCacheLine;
+
+static KBlockKeyCacheLine ** cache;
+static unsigned int cacheSize;
+static Mutex lock;
+
+/**
+ * Deterministically (!) create a hostkey using only the
+ * given HashCode as input to the PRNG.
+ */
+struct PrivateKey * makeKblockKey(const HashCode512 * hc) {
+  struct PrivateKey * ret;
+  KBlockKeyCacheLine * line;
+  int i;
+
+  MUTEX_LOCK(&lock);
+  for (i=0;i<cacheSize;i++) {
+    if (equalsHashCode512(hc,
+                         &cache[i]->hc)) {
+      ret = decodePrivateKey(cache[i]->pke);
+      MUTEX_UNLOCK(&lock);
+      return ret;
+    }
+  }
+
+  line
+    = MALLOC(sizeof(KBlockKeyCacheLine));
+  line->hc = *hc;
+  line->pke
+    = makeKblockKeyInternal(hc);
+  GROW(cache,
+       cacheSize,
+       cacheSize+1);
+  cache[cacheSize-1]
+    = line;
+  MUTEX_UNLOCK(&lock);
+  return decodePrivateKey(line->pke);
 }
 
+void initKBlockKey() {
+  MUTEX_CREATE(&lock);
+}
 
+void doneKBlockKey() {
+  int i;
+  for (i=0;i<cacheSize;i++) {
+    FREE(cache[i]->pke);
+    FREE(cache[i]);
+  }
+  GROW(cache,
+       cacheSize,
+       0);
+  MUTEX_DESTROY(&lock);
+}
+
 /* end of kblockkey.c */





reply via email to

[Prev in Thread] Current Thread [Next in Thread]