29 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>
33 #define _MPRINTF_REPLACE
44 #define MEMDEBUG_NODEFINES
62 #define logfile curl_debuglogfile
63 FILE *curl_debuglogfile;
71 logfile = fopen(logname,
"w");
78 void curl_memlimit(
long limit)
85 static bool countcheck(
const char *func,
int line,
const char *
source)
89 if(memlimit && source) {
92 fprintf(logfile,
"LIMIT %s:%d %s reached memlimit\n",
100 if(logfile && source)
101 fprintf(logfile,
"LIMIT %s:%d %ld ALLOCS left\n",
102 source, line, memsize);
109 void *curl_domalloc(
size_t wantedsize,
int line,
const char *source)
111 struct memdebug *mem;
114 if(countcheck(
"malloc", line, source))
118 size =
sizeof(
struct memdebug)+wantedsize;
120 mem=(
struct memdebug *)(malloc)(
size);
123 memset(mem->mem, 0xA5, wantedsize);
124 mem->size = wantedsize;
127 if(logfile && source)
128 fprintf(logfile,
"MEM %s:%d malloc(%zd) = %p\n",
129 source, line, wantedsize, mem->mem);
133 void *curl_docalloc(
size_t wanted_elements,
size_t wanted_size,
134 int line,
const char *source)
136 struct memdebug *mem;
137 size_t size, user_size;
139 if(countcheck(
"calloc", line, source))
143 user_size = wanted_size * wanted_elements;
144 size =
sizeof(
struct memdebug) + user_size;
146 mem = (
struct memdebug *)(malloc)(
size);
149 memset(mem->mem, 0, user_size);
150 mem->size = user_size;
153 if(logfile && source)
154 fprintf(logfile,
"MEM %s:%d calloc(%u,%u) = %p\n",
155 source, line, wanted_elements, wanted_size, mem->mem);
159 char *curl_dostrdup(
const char *str,
int line,
const char *source)
166 if(countcheck(
"strdup", line, source))
171 mem=curl_domalloc(len, 0,
NULL);
172 memcpy(mem, str, len);
175 fprintf(logfile,
"MEM %s:%d strdup(%p) (%zd) = %p\n",
176 source, line, str, len, mem);
183 void *curl_dorealloc(
void *ptr,
size_t wantedsize,
184 int line,
const char *source)
186 struct memdebug *mem=
NULL;
188 size_t size =
sizeof(
struct memdebug)+wantedsize;
190 if(countcheck(
"realloc", line, source))
194 mem = (
struct memdebug *)((
char *)ptr - offsetof(
struct memdebug, mem));
196 mem=(
struct memdebug *)(realloc)(mem,
size);
198 fprintf(logfile,
"MEM %s:%d realloc(0x%x, %zd) = %p\n",
199 source, line, ptr, wantedsize, mem?mem->mem:
NULL);
202 mem->size = wantedsize;
209 void curl_dofree(
void *ptr,
int line,
const char *source)
211 struct memdebug *mem;
215 mem = (
struct memdebug *)((
char *)ptr - offsetof(
struct memdebug, mem));
218 memset(mem->mem, 0x13, mem->size);
224 fprintf(logfile,
"MEM %s:%d free(%p)\n", source, line, ptr);
227 int curl_socket(
int domain,
int type,
int protocol,
int line,
230 int sockfd=(socket)(domain, type, protocol);
231 if(logfile && (sockfd!=-1))
232 fprintf(logfile,
"FD %s:%d socket() = %d\n",
233 source, line, sockfd);
237 int curl_accept(
int s,
void *saddr,
void *saddrlen,
238 int line,
const char *source)
240 struct sockaddr *
addr = (
struct sockaddr *)saddr;
242 int sockfd=(accept)(s, addr, addrlen);
244 fprintf(logfile,
"FD %s:%d accept() = %d\n",
245 source, line, sockfd);
250 int curl_sclose(
int sockfd,
int line,
const char *source)
254 fprintf(logfile,
"FD %s:%d sclose(%d)\n",
255 source, line, sockfd);
259 FILE *curl_fopen(
const char *file,
const char *
mode,
260 int line,
const char *source)
262 FILE *res=(fopen)(file, mode);
264 fprintf(logfile,
"FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
265 source, line, file, mode, res);
269 int curl_fclose(FILE *file,
int line,
const char *source)
277 fprintf(logfile,
"FILE %s:%d fclose(%p)\n",
283 int VOID_VAR_MEMDEBUG;
GLuint GLuint GLsizei GLenum type
GLsizei GLsizei GLcharARB * source
GLenum const GLvoid * addr