introduce allocation wrappers to improve libgcrypt performance
authorChristian Grothoff <christian@grothoff.org>
Sat, 11 Aug 2018 04:20:40 +0000 (06:20 +0200)
committerChristian Grothoff <christian@grothoff.org>
Sat, 11 Aug 2018 04:20:40 +0000 (06:20 +0200)
src/util/common_logging.c
src/util/crypto_random.c

index f875d165d8870edaf8d7a260633533b822721872..be2e084b5a8827a362dbe2069db9e828d3767f68 100644 (file)
@@ -11,7 +11,7 @@
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      Affero General Public License for more details.
-    
+
      You should have received a copy of the GNU Affero General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
@@ -817,32 +817,39 @@ output_message (enum GNUNET_ErrorType kind,
   if ( (NULL != GNUNET_stderr) &&
        (NULL == loggers) )
   {
-    if (kind == GNUNET_ERROR_TYPE_MESSAGE) {
-       /* The idea here is to produce "normal" output messages
-        * for end users while still having the power of the
-        * logging engine for developer needs. So ideally this
-        * is what it should look like when CLI tools are used
-        * interactively, yet the same message shouldn't look
-        * this way if the output is going to logfiles or robots
-        * instead. Is this the right place to do this? --lynX
-        */
-       FPRINTF (GNUNET_stderr,
-             "* %s",
-             msg);
-    } else {
-       FPRINTF (GNUNET_stderr,
-             "%s %s %s %s",
-             datestr,
-             comp,
-             GNUNET_error_type_to_string (kind),
-             msg);
+    if (kind == GNUNET_ERROR_TYPE_MESSAGE)
+    {
+      /* The idea here is to produce "normal" output messages
+       * for end users while still having the power of the
+       * logging engine for developer needs. So ideally this
+       * is what it should look like when CLI tools are used
+       * interactively, yet the same message shouldn't look
+       * this way if the output is going to logfiles or robots
+       * instead.
+       */
+      FPRINTF (GNUNET_stderr,
+               "* %s",
+               msg);
+    }
+    else
+    {
+      FPRINTF (GNUNET_stderr,
+               "%s %s %s %s",
+               datestr,
+               comp,
+               GNUNET_error_type_to_string (kind),
+               msg);
     }
     fflush (GNUNET_stderr);
   }
   pos = loggers;
-  while (pos != NULL)
+  while (NULL != pos)
   {
-    pos->logger (pos->logger_cls, kind, comp, datestr, msg);
+    pos->logger (pos->logger_cls,
+                 kind,
+                 comp,
+                 datestr,
+                 msg);
     pos = pos->next;
   }
 #if WINDOWS
index 2163a9731a8da912cdc26e5dafd4023cd6d38cb2..df6d3fb9bc0585f1a63b5f8a834e270f036572f0 100644 (file)
@@ -11,7 +11,7 @@
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      Affero General Public License for more details.
-    
+
      You should have received a copy of the GNU Affero General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
@@ -268,6 +268,28 @@ GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
 }
 
 
+/**
+ * Allocation wrapper for libgcrypt, used to avoid bad locking
+ * strategy of libgcrypt implementation.
+ */
+static void *
+w_malloc (size_t n)
+{
+  return calloc (n, 1);
+}
+
+
+/**
+ * Allocation wrapper for libgcrypt, used to avoid bad locking
+ * strategy of libgcrypt implementation.
+ */
+static int
+w_check (const void *p)
+{
+  return 0; /* not secure memory */
+}
+
+
 /**
  * Initialize libgcrypt.
  */
@@ -283,6 +305,13 @@ GNUNET_CRYPTO_random_init ()
              NEED_LIBGCRYPT_VERSION);
     GNUNET_assert (0);
   }
+  /* set custom allocators */
+  gcry_set_allocation_handler (&w_malloc,
+                               &w_malloc,
+                               &w_check,
+                               &realloc,
+                               &free);
+  /* Disable use of secure memory */
   if ((rc = gcry_control (GCRYCTL_DISABLE_SECMEM, 0)))
     FPRINTF (stderr,
              "Failed to set libgcrypt option %s: %s\n",