From 07de0ff0ff2e99ae18c0326c1916e07ca5847028 Mon Sep 17 00:00:00 2001
From: Daniel Stenberg <daniel@haxx.se>
Date: Thu, 26 Feb 2004 14:52:51 +0000
Subject: [PATCH] Gisle Vanem's added support calloc()-debugging and outputting
 mode for fopen() as well.

---
 lib/memdebug.c | 30 ++++++++++++++++++++++++++++--
 lib/memdebug.h |  2 ++
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/lib/memdebug.c b/lib/memdebug.c
index 1eeb5c93e..a51e1f1d1 100644
--- a/lib/memdebug.c
+++ b/lib/memdebug.c
@@ -130,6 +130,32 @@ void *curl_domalloc(size_t wantedsize, int line, const char *source)
   return mem->mem;
 }
 
+void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
+                    int line, const char *source)
+{
+  struct memdebug *mem;
+  size_t size, user_size;
+
+  if(countcheck("calloc", line, source))
+    return NULL;
+
+  /* alloc at least 64 bytes */
+  user_size = wanted_size * wanted_elements;
+  size = sizeof(struct memdebug) + user_size;
+
+  mem = (struct memdebug *)(malloc)(size);
+  if(mem) {
+    /* fill memory with zeroes */
+    memset(mem->mem, 0, user_size);
+    mem->size = user_size;
+  }
+
+  if(logfile && source)
+    fprintf(logfile, "MEM %s:%d calloc(%u,%u) = %p\n",
+            source, line, wanted_elements, wanted_size, mem->mem);
+  return mem->mem;
+}
+
 char *curl_dostrdup(const char *str, int line, const char *source)
 {
   char *mem;
@@ -235,8 +261,8 @@ FILE *curl_fopen(const char *file, const char *mode,
 {
   FILE *res=(fopen)(file, mode);
   if(logfile)
-    fprintf(logfile, "FILE %s:%d fopen(\"%s\") = %p\n",
-            source, line, file, res);
+    fprintf(logfile, "FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
+            source, line, file, mode, res);
   return res;
 }
 
diff --git a/lib/memdebug.h b/lib/memdebug.h
index 2bf75e58d..40ba08033 100644
--- a/lib/memdebug.h
+++ b/lib/memdebug.h
@@ -49,6 +49,7 @@ extern FILE *logfile;
 
 /* memory functions */
 void *curl_domalloc(size_t size, int line, const char *source);
+void *curl_docalloc(size_t elements, size_t size, int line, const char *source);
 void *curl_dorealloc(void *ptr, size_t size, int line, const char *source);
 void curl_dofree(void *ptr, int line, const char *source);
 char *curl_dostrdup(const char *str, int line, const char *source);
@@ -72,6 +73,7 @@ int curl_fclose(FILE *file, int line, const char *source);
 #undef strdup
 #define strdup(ptr) curl_dostrdup(ptr, __LINE__, __FILE__)
 #define malloc(size) curl_domalloc(size, __LINE__, __FILE__)
+#define calloc(nbelem,size) curl_docalloc(nbelem, size, __LINE__, __FILE__)
 #define realloc(ptr,size) curl_dorealloc(ptr, size, __LINE__, __FILE__)
 #define free(ptr) curl_dofree(ptr, __LINE__, __FILE__)
 
-- 
2.40.0