Browse Source

Add --with-dmalloc configure option

svn:r3508
Peter Palfrader 19 years ago
parent
commit
ddd724ef94
4 changed files with 59 additions and 15 deletions
  1. 19 0
      configure.in
  2. 21 10
      src/common/util.c
  3. 11 5
      src/common/util.h
  4. 8 0
      src/or/main.c

+ 19 - 0
configure.in

@@ -214,6 +214,25 @@ if test $tor_cv_null_is_zero = yes; then
             [Define to 1 iff memset(0) sets pointers to NULL])
 fi
 
+# Whether we should use the dmalloc memory allocation debugging library.
+AC_MSG_CHECKING(wether to use dmalloc (debug memory allocation library))
+AC_ARG_WITH(dmalloc,
+[  --with-dmalloc        Use debug memory allocation library. ],
+[if [[ "$withval" = "yes" ]]; then
+  dmalloc=1
+  AC_MSG_RESULT(yes)
+else
+  dmalloc=1
+  AC_MSG_RESULT(no)
+fi], [ dmalloc=0; AC_MSG_RESULT(no) ]
+)
+
+if [[ $dmalloc -eq 1 ]]; then
+  AC_SEARCH_LIBS(dmalloc_malloc, [dmalloc], , AC_MSG_ERROR(Libdmalloc library not found. If you enable it you better have it installed.))
+  AC_DEFINE(USE_DMALLOC, 1, [Debug memory allocation library])
+  AC_DEFINE(DMALLOC_FUNC_CHECK, 1, [Enable dmalloc's malloc function check])
+fi
+
 # $prefix stores the value of the --prefix command line option, or
 # NONE if the option wasn't set.  In the case that it wasn't set, make
 # it be the default, so that we can use it to expand directories now.

+ 21 - 10
src/common/util.c

@@ -88,19 +88,30 @@ const char util_c_id[] = "$Id$";
 /* =====
  * Memory management
  * ===== */
+#ifdef USE_DMALLOC
+ #include <dmalloc.h>
+#else
+ #define dmalloc_strdup(file, line, string, xalloc_b) strdup(string)
+
+ #define dmalloc_malloc(file, line, size, func_id, alignment, xalloc_b) malloc(size)
+ #define DMALLOC_FUNC_MALLOC 0
+
+ #define dmalloc_realloc(file, line, old_pnt, new_size, func_id, xalloc_b) realloc((old_pnt), (new_size))
+ #define DMALLOC_FUNC_REALLOC 0
+#endif
 
 /** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to
  * result.  On error, log and terminate the process.  (Same as malloc(size),
  * but never returns NULL.)
  */
-void *tor_malloc(size_t size) {
+void *_tor_malloc(const char *file, const int line, size_t size) {
   void *result;
 
   /* Some libcs don't do the right thing on size==0. Override them. */
   if (size==0) {
     size=1;
   }
-  result = malloc(size);
+  result = dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0);
 
   if (!result) {
     log_fn(LOG_ERR, "Out of memory. Dying.");
@@ -114,8 +125,8 @@ void *tor_malloc(size_t size) {
  * zero bytes, and return a pointer to the result.  Log and terminate
  * the process on error.  (Same as calloc(size,1), but never returns NULL.)
  */
-void *tor_malloc_zero(size_t size) {
-  void *result = tor_malloc(size);
+void *_tor_malloc_zero(const char *file, const int line, size_t size) {
+  void *result = _tor_malloc(file, line, size);
   memset(result, 0, size);
   return result;
 }
@@ -124,10 +135,10 @@ void *tor_malloc_zero(size_t size) {
  * bytes long; return the new memory block.  On error, log and
  * terminate. (Like realloc(ptr,size), but never returns NULL.)
  */
-void *tor_realloc(void *ptr, size_t size) {
+void *_tor_realloc(const char *file, const int line, void *ptr, size_t size) {
   void *result;
 
-  result = realloc(ptr, size);
+  result = dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0);
   if (!result) {
     log_fn(LOG_ERR, "Out of memory. Dying.");
     exit(1);
@@ -139,11 +150,11 @@ void *tor_realloc(void *ptr, size_t size) {
  * error, log and terminate.  (Like strdup(s), but never returns
  * NULL.)
  */
-char *tor_strdup(const char *s) {
+char *_tor_strdup(const char *file, const int line, const char *s) {
   char *dup;
   tor_assert(s);
 
-  dup = strdup(s);
+  dup = dmalloc_strdup(file, line, s, 0);
   if (!dup) {
     log_fn(LOG_ERR,"Out of memory. Dying.");
     exit(1);
@@ -157,10 +168,10 @@ char *tor_strdup(const char *s) {
  * always NUL-terminated.  (Like strndup(s,n), but never returns
  * NULL.)
  */
-char *tor_strndup(const char *s, size_t n) {
+char *_tor_strndup(const char *file, const int line, const char *s, size_t n) {
   char *dup;
   tor_assert(s);
-  dup = tor_malloc(n+1);
+  dup = _tor_malloc(file, line, n+1);
   /* Performance note: Ordinarily we prefer strlcpy to strncpy.  But
    * this function gets called a whole lot, and platform strncpy is
    * much faster than strlcpy when strlen(s) is much longer than n.

+ 11 - 5
src/common/util.h

@@ -47,13 +47,19 @@
 #endif
 
 /* Memory management */
-void *tor_malloc(size_t size);
-void *tor_malloc_zero(size_t size);
-void *tor_realloc(void *ptr, size_t size);
-char *tor_strdup(const char *s);
-char *tor_strndup(const char *s, size_t n);
+void *_tor_malloc(const char *file, const int line, size_t size);
+void *_tor_malloc_zero(const char *file, const int line, size_t size);
+void *_tor_realloc(const char *file, const int line, void *ptr, size_t size);
+char *_tor_strdup(const char *file, const int line, const char *s);
+char *_tor_strndup(const char *file, const int line, const char *s, size_t n);
 #define tor_free(p) do { if (p) {free(p); (p)=NULL;} } while (0)
 
+#define tor_malloc(size)       _tor_malloc(_SHORT_FILE_, __LINE__, size)
+#define tor_malloc_zero(size)  _tor_malloc_zero(_SHORT_FILE_, __LINE__, size)
+#define tor_realloc(ptr, size) _tor_realloc(_SHORT_FILE_, __LINE__, ptr, size)
+#define tor_strdup(s)          _tor_strdup(_SHORT_FILE_, __LINE__, s)
+#define tor_strndup(s, n)      _tor_strndup(_SHORT_FILE_, __LINE__, s, n)
+
 /* String manipulation */
 #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
 void tor_strlower(char *s);

+ 8 - 0
src/or/main.c

@@ -11,6 +11,10 @@ const char main_c_id[] = "$Id$";
  **/
 
 #include "or.h"
+#include "orconfig.h"
+#ifdef USE_DMALLOC
+#include <dmalloc.h>
+#endif
 
 /* These signals are defined to help control_signal_act work. */
 #ifndef SIGHUP
@@ -1314,7 +1318,11 @@ void tor_cleanup(void) {
   crypto_global_cleanup();
   if (accounting_is_enabled(options))
     accounting_record_bandwidth_usage(time(NULL));
+#ifdef USE_DMALLOC
   tor_free_all();
+  dmalloc_log_unfreed();
+  dmalloc_shutdown();
+#endif
 }
 
 /** Read/create keys as needed, and echo our fingerprint to stdout. */