]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
Reinstate "WIN32 MemoryTracking: track wcsdup() _wcsdup() and _tcsdup() usage".
authorYang Tse <yangsita@gmail.com>
Thu, 18 Jul 2013 18:04:02 +0000 (20:04 +0200)
committerYang Tse <yangsita@gmail.com>
Thu, 18 Jul 2013 21:37:33 +0000 (23:37 +0200)
This reverts commit 7ed25cc, reinstating commit 8ec2cb5.

As of 18-jul-2013 we still do have code in libcurl that makes use of these
memory functions. Commit 8ec2cb5 comment still applies and is yet valid.

These memory functions are solely used in Windows builds, so all related
code is protected with '#ifdef WIN32' preprocessor conditional compilation
directives.

Specifically, wcsdup() _wcsdup() are used when building a Windows target with
UNICODE and USE_WINDOWS_SSPI preprocessor symbols defined. This is the case
when building a Windows UNICODE target with Windows native SSL/TLS support
enabled.

Realizing that wcsdup() _wcsdup() are used is a bit tricky given that usage
of these is hidden behind _tcsdup() which is MS way of dealing with code
that must tolerate UNICODE and non-UNICODE compilation. Additionally, MS
header files and those compatible from other compilers use this preprocessor
conditional compilation directive in order to select at compilation time
whether 'wide' or 'ansi' MS API functions are used.

Without this code, Windows build targets with Windows native SSL/TLS support
enabled and MemoryTracking support enabled misbehave in tracking memory usage,
regardless of being a UNICODE enabled build or not.

lib/curl_memory.h
lib/curl_setup.h
lib/easy.c
lib/memdebug.c
lib/memdebug.h
tests/memanalyze.pl
tests/server/getpart.c

index c1e92513fa543645db591ee97ae5e3a33e4a4c07..4bba008c9a4cc37982b260dbecff3cf0a06f2044 100644 (file)
@@ -87,6 +87,9 @@ extern curl_free_callback Curl_cfree;
 extern curl_realloc_callback Curl_crealloc;
 extern curl_strdup_callback Curl_cstrdup;
 extern curl_calloc_callback Curl_ccalloc;
+#ifdef WIN32
+extern curl_wcsdup_callback Curl_cwcsdup;
+#endif
 
 #ifndef CURLDEBUG
 
@@ -110,6 +113,19 @@ extern curl_calloc_callback Curl_ccalloc;
 #undef free
 #define free(ptr) Curl_cfree(ptr)
 
+#ifdef WIN32
+#  undef wcsdup
+#  define wcsdup(ptr) Curl_cwcsdup(ptr)
+#  undef _wcsdup
+#  define _wcsdup(ptr) Curl_cwcsdup(ptr)
+#  undef _tcsdup
+#  ifdef UNICODE
+#    define _tcsdup(ptr) Curl_cwcsdup(ptr)
+#  else
+#    define _tcsdup(ptr) Curl_cstrdup(ptr)
+#  endif
+#endif
+
 #endif /* CURLDEBUG */
 
 #else /* CURLX_NO_MEMORY_CALLBACKS */
index a46a60828aad42562ff0a8e658d33b2c89ab6101..3f55a5c0f60f42f08ee8732101a1ece089930012 100644 (file)
 #    endif
 #  endif
 #  include <tchar.h>
+   typedef wchar_t *(*curl_wcsdup_callback)(const wchar_t *str);
 #endif
 
 /*
index b82a548cb8abb1258f6ff1a4af49c39276829ef4..541e793fbe584a40d898466af9d7dd27ca80b111 100644 (file)
@@ -252,6 +252,9 @@ curl_free_callback Curl_cfree = (curl_free_callback)free;
 curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
 curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)system_strdup;
 curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
+#ifdef WIN32
+curl_wcsdup_callback Curl_cwcsdup = (curl_wcsdup_callback)wcsdup;
+#endif
 #else
 /*
  * Symbian OS doesn't support initialization to code in writeable static data.
@@ -283,6 +286,9 @@ CURLcode curl_global_init(long flags)
   Curl_crealloc = (curl_realloc_callback)realloc;
   Curl_cstrdup = (curl_strdup_callback)system_strdup;
   Curl_ccalloc = (curl_calloc_callback)calloc;
+#ifdef WIN32
+  Curl_cwcsdup = (curl_wcsdup_callback)wcsdup;
+#endif
 
   if(flags & CURL_GLOBAL_SSL)
     if(!Curl_ssl_init()) {
index 4d5f44d2f0d5dd5d75011b78727db01f12873798..b3ddfb430e24324be9530254916216f6d99e34dd 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -239,6 +239,32 @@ char *curl_dostrdup(const char *str, int line, const char *source)
   return mem;
 }
 
+#ifdef WIN32
+wchar_t *curl_dowcsdup(const wchar_t *str, int line, const char *source)
+{
+  wchar_t *mem;
+  size_t wsiz, bsiz;
+
+  assert(str != NULL);
+
+  if(countcheck("wcsdup", line, source))
+    return NULL;
+
+  wsiz = wcslen(str) + 1;
+  bsiz = wsiz * sizeof(wchar_t);
+
+  mem = curl_domalloc(bsiz, 0, NULL); /* NULL prevents logging */
+  if(mem)
+    memcpy(mem, str, bsiz);
+
+  if(source)
+    curl_memlog("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
+                source, line, str, bsiz, mem);
+
+  return mem;
+}
+#endif
+
 /* We provide a realloc() that accepts a NULL as pointer, which then
    performs a malloc(). In order to work with ares. */
 void *curl_dorealloc(void *ptr, size_t wantedsize,
index fbeb61de5d38a5e15ce465a10f80fdd52f5a9922..955e8b72b9f8e7c35fa03cbf04c6355c6adc3790 100644 (file)
@@ -8,7 +8,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -46,6 +46,11 @@ CURL_EXTERN void *curl_dorealloc(void *ptr, size_t size, int line,
                                  const char *source);
 CURL_EXTERN void curl_dofree(void *ptr, int line, const char *source);
 CURL_EXTERN char *curl_dostrdup(const char *str, int line, const char *source);
+#ifdef WIN32
+CURL_EXTERN wchar_t *curl_dowcsdup(const wchar_t *str, int line,
+                                   const char *source);
+#endif
+
 CURL_EXTERN void curl_memdebug(const char *logname);
 CURL_EXTERN void curl_memlimit(long limit);
 CURL_EXTERN void curl_memlog(const char *format, ...);
@@ -84,6 +89,19 @@ CURL_EXTERN int curl_fclose(FILE *file, int line, const char *source);
 #define realloc(ptr,size) curl_dorealloc(ptr, size, __LINE__, __FILE__)
 #define free(ptr) curl_dofree(ptr, __LINE__, __FILE__)
 
+#ifdef WIN32
+#  undef wcsdup
+#  define wcsdup(ptr) curl_dowcsdup(ptr, __LINE__, __FILE__)
+#  undef _wcsdup
+#  define _wcsdup(ptr) curl_dowcsdup(ptr, __LINE__, __FILE__)
+#  undef _tcsdup
+#  ifdef UNICODE
+#    define _tcsdup(ptr) curl_dowcsdup(ptr, __LINE__, __FILE__)
+#  else
+#    define _tcsdup(ptr) curl_dostrdup(ptr, __LINE__, __FILE__)
+#  endif
+#endif
+
 #define socket(domain,type,protocol)\
  curl_socket(domain,type,protocol,__LINE__,__FILE__)
 #undef accept /* for those with accept as a macro */
index 5246349697561ffab201db15035cd839e494f8c0..54117f8ea1ba742f6c2722e2edebae594471875a 100755 (executable)
@@ -31,6 +31,7 @@ my $mallocs=0;
 my $callocs=0;
 my $reallocs=0;
 my $strdups=0;
+my $wcsdups=0;
 my $showlimit;
 
 while(1) {
@@ -220,6 +221,25 @@ while(<FILE>) {
             newtotal($totalmem);
             $strdups++;
         }
+        elsif($function =~ /wcsdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
+            # wcsdup(a5b50) (8) = df7c0
+
+            $dup = $1;
+            $size = $2;
+            $addr = $3;
+            $getmem{$addr}="$source:$linenum";
+            $sizeataddr{$addr}=$size;
+
+            $totalmem += $size;
+
+            if($trace) {
+                printf("WCSDUP: $size bytes at %s, makes totally: %d bytes\n",
+                       $getmem{$addr}, $totalmem);
+            }
+
+            newtotal($totalmem);
+            $wcsdups++;
+        }
         else {
             print "Not recognized input line: $function\n";
         }
@@ -378,8 +398,9 @@ if($verbose) {
     "Reallocs: $reallocs\n",
     "Callocs: $callocs\n",
     "Strdups:  $strdups\n",
+    "Wcsdups:  $wcsdups\n",
     "Frees: $frees\n",
-    "Allocations: ".($mallocs + $callocs + $reallocs + $strdups)."\n";
+    "Allocations: ".($mallocs + $callocs + $reallocs + $strdups + $wcsdups)."\n";
 
     print "Maximum allocated: $maxmem\n";
 }
index f37f88cba69a550e29d437512c9d568b2af59954..188eb5dca43ec3a30fb028d6fe286e712687b2a3 100644 (file)
@@ -58,6 +58,9 @@ curl_free_callback Curl_cfree = (curl_free_callback)free;
 curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
 curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)strdup;
 curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
+#ifdef WIN32
+curl_wcsdup_callback Curl_cwcsdup = (curl_wcsdup_callback)wcsdup;
+#endif
 
 #if defined(_MSC_VER) && defined(_DLL)
 #  pragma warning(default:4232) /* MSVC extension, dllimport identity */