mirror of
https://github.com/curl/curl.git
synced 2026-07-26 14:17:50 +03:00
Constantine Sapuntzakis enhancements to make memory tracking log file writing
of messages atomic, on systems where an fwrite of a memory buffer is atomic.
This commit is contained in:
parent
5b5ff41ef1
commit
ccfe279117
5 changed files with 96 additions and 69 deletions
113
lib/memdebug.c
113
lib/memdebug.c
|
|
@ -5,7 +5,7 @@
|
|||
* | (__| |_| | _ <| |___
|
||||
* \___|\___/|_| \_\_____|
|
||||
*
|
||||
* Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
|
||||
* Copyright (C) 1998 - 2010, 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
|
||||
|
|
@ -36,6 +36,7 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifdef HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
|
|
@ -104,12 +105,14 @@ static bool countcheck(const char *func, int line, const char *source)
|
|||
should not be made */
|
||||
if(memlimit && source) {
|
||||
if(!memsize) {
|
||||
if(logfile && source)
|
||||
fprintf(logfile, "LIMIT %s:%d %s reached memlimit\n",
|
||||
source, line, func);
|
||||
if(source)
|
||||
if(source) {
|
||||
/* log to file */
|
||||
curl_memlog("LIMIT %s:%d %s reached memlimit\n",
|
||||
source, line, func);
|
||||
/* log to stderr also */
|
||||
fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
|
||||
source, line, func);
|
||||
}
|
||||
SET_ERRNO(ENOMEM);
|
||||
return TRUE; /* RETURN ERROR! */
|
||||
}
|
||||
|
|
@ -117,9 +120,9 @@ static bool countcheck(const char *func, int line, const char *source)
|
|||
memsize--; /* countdown */
|
||||
|
||||
/* log the countdown */
|
||||
if(logfile && source)
|
||||
fprintf(logfile, "LIMIT %s:%d %ld ALLOCS left\n",
|
||||
source, line, memsize);
|
||||
if(source)
|
||||
curl_memlog("LIMIT %s:%d %ld ALLOCS left\n",
|
||||
source, line, memsize);
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -146,9 +149,9 @@ void *curl_domalloc(size_t wantedsize, int line, const char *source)
|
|||
mem->size = wantedsize;
|
||||
}
|
||||
|
||||
if(logfile && source)
|
||||
fprintf(logfile, "MEM %s:%d malloc(%zd) = %p\n",
|
||||
source, line, wantedsize, mem ? mem->mem : 0);
|
||||
if(source)
|
||||
curl_memlog("MEM %s:%d malloc(%zd) = %p\n",
|
||||
source, line, wantedsize, mem ? mem->mem : 0);
|
||||
return (mem ? mem->mem : NULL);
|
||||
}
|
||||
|
||||
|
|
@ -175,9 +178,9 @@ void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
|
|||
mem->size = user_size;
|
||||
}
|
||||
|
||||
if(logfile && source)
|
||||
fprintf(logfile, "MEM %s:%d calloc(%zu,%zu) = %p\n",
|
||||
source, line, wanted_elements, wanted_size, mem ? mem->mem : 0);
|
||||
if(source)
|
||||
curl_memlog("MEM %s:%d calloc(%zu,%zu) = %p\n",
|
||||
source, line, wanted_elements, wanted_size, mem?mem->mem:0);
|
||||
return (mem ? mem->mem : NULL);
|
||||
}
|
||||
|
||||
|
|
@ -197,9 +200,9 @@ char *curl_dostrdup(const char *str, int line, const char *source)
|
|||
if(mem)
|
||||
memcpy(mem, str, len);
|
||||
|
||||
if(logfile)
|
||||
fprintf(logfile, "MEM %s:%d strdup(%p) (%zu) = %p\n",
|
||||
source, line, str, len, mem);
|
||||
if(source)
|
||||
curl_memlog("MEM %s:%d strdup(%p) (%zu) = %p\n",
|
||||
source, line, str, len, mem);
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
|
@ -222,9 +225,9 @@ void *curl_dorealloc(void *ptr, size_t wantedsize,
|
|||
mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
|
||||
|
||||
mem = (Curl_crealloc)(mem, size);
|
||||
if(logfile)
|
||||
fprintf(logfile, "MEM %s:%d realloc(%p, %zu) = %p\n",
|
||||
source, line, ptr, wantedsize, mem?mem->mem:NULL);
|
||||
if(source)
|
||||
curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n",
|
||||
source, line, ptr, wantedsize, mem?mem->mem:NULL);
|
||||
|
||||
if(mem) {
|
||||
mem->size = wantedsize;
|
||||
|
|
@ -248,17 +251,17 @@ void curl_dofree(void *ptr, int line, const char *source)
|
|||
/* free for real */
|
||||
(Curl_cfree)(mem);
|
||||
|
||||
if(logfile)
|
||||
fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
|
||||
if(source)
|
||||
curl_memlog("MEM %s:%d free(%p)\n", source, line, ptr);
|
||||
}
|
||||
|
||||
int curl_socket(int domain, int type, int protocol, int line,
|
||||
const char *source)
|
||||
{
|
||||
int sockfd=socket(domain, type, protocol);
|
||||
if(logfile && (sockfd!=-1))
|
||||
fprintf(logfile, "FD %s:%d socket() = %d\n",
|
||||
source, line, sockfd);
|
||||
if(source && (sockfd!=-1))
|
||||
curl_memlog("FD %s:%d socket() = %d\n",
|
||||
source, line, sockfd);
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
|
|
@ -268,18 +271,18 @@ int curl_accept(int s, void *saddr, void *saddrlen,
|
|||
struct sockaddr *addr = (struct sockaddr *)saddr;
|
||||
curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
|
||||
int sockfd=accept(s, addr, addrlen);
|
||||
if(logfile)
|
||||
fprintf(logfile, "FD %s:%d accept() = %d\n",
|
||||
source, line, sockfd);
|
||||
if(source)
|
||||
curl_memlog("FD %s:%d accept() = %d\n",
|
||||
source, line, sockfd);
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
/* separate function to allow libcurl to mark a "faked" close */
|
||||
void curl_mark_sclose(int sockfd, int line, const char *source)
|
||||
{
|
||||
if(logfile)
|
||||
fprintf(logfile, "FD %s:%d sclose(%d)\n",
|
||||
source, line, sockfd);
|
||||
if(source)
|
||||
curl_memlog("FD %s:%d sclose(%d)\n",
|
||||
source, line, sockfd);
|
||||
}
|
||||
|
||||
/* this is our own defined way to close sockets on *ALL* platforms */
|
||||
|
|
@ -294,9 +297,9 @@ FILE *curl_fopen(const char *file, const char *mode,
|
|||
int line, const char *source)
|
||||
{
|
||||
FILE *res=fopen(file, mode);
|
||||
if(logfile)
|
||||
fprintf(logfile, "FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
|
||||
source, line, file, mode, res);
|
||||
if(source)
|
||||
curl_memlog("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
|
||||
source, line, file, mode, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
|
@ -305,9 +308,9 @@ FILE *curl_fdopen(int filedes, const char *mode,
|
|||
int line, const char *source)
|
||||
{
|
||||
FILE *res=fdopen(filedes, mode);
|
||||
if(logfile)
|
||||
fprintf(logfile, "FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
|
||||
source, line, filedes, mode, res);
|
||||
if(source)
|
||||
curl_memlog("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
|
||||
source, line, filedes, mode, res);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
|
@ -319,9 +322,39 @@ int curl_fclose(FILE *file, int line, const char *source)
|
|||
assert(file != NULL);
|
||||
|
||||
res=fclose(file);
|
||||
if(logfile)
|
||||
fprintf(logfile, "FILE %s:%d fclose(%p)\n",
|
||||
source, line, file);
|
||||
if(source)
|
||||
curl_memlog("FILE %s:%d fclose(%p)\n",
|
||||
source, line, file);
|
||||
return res;
|
||||
}
|
||||
|
||||
#define LOGLINE_BUFSIZE 1024
|
||||
|
||||
/* this does the writting to the memory tracking log file */
|
||||
void curl_memlog(const char *format, ...)
|
||||
{
|
||||
char *buf;
|
||||
int nchars;
|
||||
va_list ap;
|
||||
|
||||
if(!logfile)
|
||||
return;
|
||||
|
||||
buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
|
||||
if(!buf)
|
||||
return;
|
||||
|
||||
va_start(ap, format);
|
||||
nchars = vsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
|
||||
va_end(ap);
|
||||
|
||||
if(nchars > LOGLINE_BUFSIZE - 1)
|
||||
nchars = LOGLINE_BUFSIZE - 1;
|
||||
|
||||
if(nchars > 0)
|
||||
fwrite(buf, 1, nchars, logfile);
|
||||
|
||||
(Curl_cfree)(buf);
|
||||
}
|
||||
|
||||
#endif /* CURLDEBUG */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue