[or-cvs] r8431: Malloc and friends are critical-path: Thus, add an it-wont-h (in tor/trunk: . src/common)

nickm at seul.org nickm at seul.org
Tue Sep 19 22:36:49 UTC 2006


Author: nickm
Date: 2006-09-19 18:36:48 -0400 (Tue, 19 Sep 2006)
New Revision: 8431

Modified:
   tor/trunk/ChangeLog
   tor/trunk/configure.in
   tor/trunk/src/common/util.c
Log:
Malloc and friends are critical-path: Thus, add an it-wont-happen branch prediction for NULL returns, and skip the malloc(0) check on platforms where malloc(0) returns a pointer.

Modified: tor/trunk/ChangeLog
===================================================================
--- tor/trunk/ChangeLog	2006-09-19 22:20:09 UTC (rev 8430)
+++ tor/trunk/ChangeLog	2006-09-19 22:36:48 UTC (rev 8431)
@@ -3,6 +3,8 @@
   o Minor Bugfixes
     - Small performance improvements on parsing and inserting
       descriptors.
+    - Make the common memory allocation path faster on machines where
+      malloc(0) returns a pointer.
 
 Changes in version 0.1.2.1-alpha - 2006-08-27
   o Major features:

Modified: tor/trunk/configure.in
===================================================================
--- tor/trunk/configure.in	2006-09-19 22:20:09 UTC (rev 8430)
+++ tor/trunk/configure.in	2006-09-19 22:36:48 UTC (rev 8431)
@@ -558,6 +558,33 @@
             [Define to 1 iff memset(0) sets pointers to NULL])
 fi
 
+# And what happens when we malloc zero?
+
+if test -z "$CROSS_COMPILE"; then
+AC_CACHE_CHECK([whether we can malloc(0) safely.], tor_cv_malloc_zero_works,
+[AC_RUN_IFELSE([AC_LANG_SOURCE(
+[[#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#ifdef HAVE_STDDEF_H
+#include <stddef.h>
+#endif
+int main () { return malloc(0)?0:1; }]])],
+       [tor_cv_malloc_zero_works=yes],
+       [tor_cv_malloc_zero_works=no],
+       [tor_cv_malloc_zero_works=cross])])
+
+else
+  # Cross-compiling; let's hope that the target isn't raving mad.
+  AC_MSG_NOTICE([Cross-compiling: we'll assume that we need to check malloc() arguments for 0.])
+  tor_cv_malloc_zero_works=no
+fi
+
+if test $tor_cv_malloc_zero_works = yes; then
+  AC_DEFINE([MALLOC_ZERO_WORKS], 1,
+            [Define to 1 iff malloc(0) returns a pointer])
+fi
+
 # Whether we should use the dmalloc memory allocation debugging library.
 AC_MSG_CHECKING(whether to use dmalloc (debug memory allocation library))
 AC_ARG_WITH(dmalloc,

Modified: tor/trunk/src/common/util.c
===================================================================
--- tor/trunk/src/common/util.c	2006-09-19 22:20:09 UTC (rev 8430)
+++ tor/trunk/src/common/util.c	2006-09-19 22:36:48 UTC (rev 8431)
@@ -106,13 +106,15 @@
 {
   void *result;
 
+#ifndef MALLOC_ZERO_WORKS
   /* Some libcs don't do the right thing on size==0. Override them. */
   if (size==0) {
     size=1;
   }
+#endif
   result = dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0);
 
-  if (!result) {
+  if (PREDICT(result == NULL, 0)) {
     log_err(LD_MM,"Out of memory. Dying.");
     /* If these functions die within a worker process, they won't call
      * spawn_exit, but that's ok, since the parent will run out of memory soon
@@ -144,7 +146,7 @@
   void *result;
 
   result = dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0);
-  if (!result) {
+  if (PREDICT(result == NULL, 0)) {
     log_err(LD_MM,"Out of memory. Dying.");
     exit(1);
   }
@@ -162,7 +164,7 @@
   tor_assert(s);
 
   dup = dmalloc_strdup(file, line, s, 0);
-  if (!dup) {
+  if (PREDICT(dup == NULL, 0)) {
     log_err(LD_MM,"Out of memory. Dying.");
     exit(1);
   }



More information about the tor-commits mailing list