added for memory leak debugging etc
[platform/upstream/curl.git] / lib / memdebug.c
1 #ifdef MALLOCDEBUG
2 /*****************************************************************************
3  *                                  _   _ ____  _     
4  *  Project                     ___| | | |  _ \| |    
5  *                             / __| | | | |_) | |    
6  *                            | (__| |_| |  _ <| |___ 
7  *                             \___|\___/|_| \_\_____|
8  *
9  *  The contents of this file are subject to the Mozilla Public License
10  *  Version 1.0 (the "License"); you may not use this file except in
11  *  compliance with the License. You may obtain a copy of the License at
12  *  http://www.mozilla.org/MPL/
13  *
14  *  Software distributed under the License is distributed on an "AS IS"
15  *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
16  *  License for the specific language governing rights and limitations
17  *  under the License.
18  *
19  *  The Original Code is Curl.
20  *
21  *  The Initial Developer of the Original Code is Daniel Stenberg.
22  *
23  *  Portions created by the Initial Developer are Copyright (C) 1999.
24  *  All Rights Reserved.
25  *
26  * ------------------------------------------------------------
27  * Main author:
28  * - Daniel Stenberg <daniel@haxx.se>
29  *
30  *      http://curl.haxx.se
31  *
32  * $Source$
33  * $Revision$
34  * $Date$
35  * $Author$
36  * $State$
37  * $Locker$
38  *
39  * ------------------------------------------------------------
40  ****************************************************************************/
41
42 #include "setup.h"
43
44 #include <curl/curl.h>
45 #include "urldata.h"
46 #include <stdio.h>
47 #include <string.h>
48
49 /*
50  * Note that these debug functions are very simple and they are meant to
51  * remain so. For advanced analysis, record a log file and write perl scripts
52  * to analyze them!
53  *
54  * Don't use these with multithreaded test programs!
55  */
56
57 FILE *logfile=stderr;
58
59 /* this sets the log file name */
60 void curl_memdebug(char *logname)
61 {
62   logfile = fopen(logname, "w");
63 }
64
65
66 void *curl_domalloc(size_t size, int line, char *source)
67 {
68   void *mem=(malloc)(size);
69   fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n",
70           source, line, size, mem);
71   return mem;
72 }
73
74 char *curl_dostrdup(char *str, int line, char *source)
75 {
76   char *mem=(strdup)(str);
77   size_t len=strlen(str)+1;
78   fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n",
79           source, line, str, len, mem);
80   return mem;
81 }
82
83 void *curl_dorealloc(void *ptr, size_t size, int line, char *source)
84 {
85   void *mem=(realloc)(ptr, size);
86   fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n",
87           source, line, ptr, size, mem);
88   return mem;
89 }
90
91 void curl_dofree(void *ptr, int line, char *source)
92 {
93   (free)(ptr);
94   fprintf(logfile, "MEM %s:%d free(%p)\n",
95           source, line, ptr);
96 }
97
98 #endif /* MALLOCDEBUG */