diff options
author | Nick Mathewson <nickm@torproject.org> | 2006-09-19 22:36:48 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2006-09-19 22:36:48 +0000 |
commit | 2d4950c8373676b9b5ae0a3c8013e780b5c18e19 (patch) | |
tree | 88a609b1bd0590b32168a6d503aea7c5e4c8a838 | |
parent | 7b0ec744bc8afa691873f97cfb6a3b7822376d7b (diff) | |
download | tor-2d4950c8373676b9b5ae0a3c8013e780b5c18e19.tar.gz tor-2d4950c8373676b9b5ae0a3c8013e780b5c18e19.zip |
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.
svn:r8431
-rw-r--r-- | ChangeLog | 2 | ||||
-rw-r--r-- | configure.in | 27 | ||||
-rw-r--r-- | src/common/util.c | 8 |
3 files changed, 34 insertions, 3 deletions
@@ -3,6 +3,8 @@ Changes in version 0.1.2.2-alpha - 2006-??-?? 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: diff --git a/configure.in b/configure.in index acedafc7af..7527bed5cc 100644 --- a/configure.in +++ b/configure.in @@ -558,6 +558,33 @@ if test $tor_cv_null_is_zero = yes; then [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, diff --git a/src/common/util.c b/src/common/util.c index 264bd0b74f..d4a939b14f 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -106,13 +106,15 @@ _tor_malloc(size_t size DMALLOC_PARAMS) { 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 @@ _tor_realloc(void *ptr, size_t size DMALLOC_PARAMS) 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_strdup(const char *s DMALLOC_PARAMS) 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); } |