Commit ccfe2791 authored by Yang Tse's avatar Yang Tse
Browse files

Constantine Sapuntzakis enhancements to make memory tracking log file writing

of messages atomic, on systems where an fwrite of a memory buffer is atomic.
parent 5b5ff41e
Loading
Loading
Loading
Loading
+9 −14
Original line number Diff line number Diff line
@@ -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
@@ -447,8 +447,7 @@ curl_dofreeaddrinfo(struct addrinfo *freethis,
                    int line, const char *source)
{
  (freeaddrinfo)(freethis);
  if(logfile)
    fprintf(logfile, "ADDR %s:%d freeaddrinfo(%p)\n",
  curl_memlog("ADDR %s:%d freeaddrinfo(%p)\n",
              source, line, (void *)freethis);
}
#endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
@@ -471,17 +470,13 @@ curl_dogetaddrinfo(const char *hostname,
                   int line, const char *source)
{
  int res=(getaddrinfo)(hostname, service, hints, result);
  if(0 == res) {
  if(0 == res)
    /* success */
    if(logfile)
      fprintf(logfile, "ADDR %s:%d getaddrinfo() = %p\n",
    curl_memlog("ADDR %s:%d getaddrinfo() = %p\n",
                source, line, (void *)*result);
  }
  else {
    if(logfile)
      fprintf(logfile, "ADDR %s:%d getaddrinfo() failed\n",
  else
    curl_memlog("ADDR %s:%d getaddrinfo() failed\n",
                source, line);
  }
  return res;
}
#endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */
+7 −11
Original line number Diff line number Diff line
@@ -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
@@ -112,17 +112,13 @@ int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,
                          host, hostlen,
                          serv, servlen,
                          flags);
  if(0 == res) {
  if(0 == res)
    /* success */
    if(logfile)
      fprintf(logfile, "GETNAME %s:%d getnameinfo()\n",
    curl_memlog("GETNAME %s:%d getnameinfo()\n",
                source, line);
  }
  else {
    if(logfile)
      fprintf(logfile, "GETNAME %s:%d getnameinfo() failed = %d\n",
  else
    curl_memlog("GETNAME %s:%d getnameinfo() failed = %d\n",
                source, line, res);
  }
  return res;
}
#endif /* defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO) */
+73 −40
Original line number Diff line number Diff line
@@ -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",
      if(source) {
        /* log to file */
        curl_memlog("LIMIT %s:%d %s reached memlimit\n",
                    source, line, func);
      if(source)
        /* log to stderr also */
        fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
                source, line, func);
      }
      SET_ERRNO(ENOMEM);
      return TRUE; /* RETURN ERROR! */
    }
@@ -117,8 +120,8 @@ 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",
    if(source)
      curl_memlog("LIMIT %s:%d %ld ALLOCS left\n",
                  source, line, memsize);

  }
@@ -146,8 +149,8 @@ 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",
  if(source)
    curl_memlog("MEM %s:%d malloc(%zd) = %p\n",
                source, line, wantedsize, mem ? mem->mem : 0);
  return (mem ? mem->mem : NULL);
}
@@ -175,8 +178,8 @@ 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",
  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,8 +200,8 @@ 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",
  if(source)
    curl_memlog("MEM %s:%d strdup(%p) (%zu) = %p\n",
                source, line, str, len, mem);

  return mem;
@@ -222,8 +225,8 @@ 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",
  if(source)
    curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n",
                source, line, ptr, wantedsize, mem?mem->mem:NULL);

  if(mem) {
@@ -248,16 +251,16 @@ 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",
  if(source && (sockfd!=-1))
    curl_memlog("FD %s:%d socket() = %d\n",
                source, line, sockfd);
  return sockfd;
}
@@ -268,8 +271,8 @@ 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",
  if(source)
    curl_memlog("FD %s:%d accept() = %d\n",
                source, line, sockfd);
  return sockfd;
}
@@ -277,8 +280,8 @@ int curl_accept(int s, void *saddr, void *saddrlen,
/* 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",
  if(source)
    curl_memlog("FD %s:%d sclose(%d)\n",
                source, line, sockfd);
}

@@ -294,8 +297,8 @@ 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",
  if(source)
    curl_memlog("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
                source, line, file, mode, res);
  return res;
}
@@ -305,8 +308,8 @@ 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",
  if(source)
    curl_memlog("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
                source, line, filedes, mode, res);
  return res;
}
@@ -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",
  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 */
+1 −0
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@ 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);
CURL_EXTERN void curl_memdebug(const char *logname);
CURL_EXTERN void curl_memlimit(long limit);
CURL_EXTERN void curl_memlog(const char *format, ...);

/* file descriptor manipulators */
CURL_EXTERN int curl_socket(int domain, int type, int protocol, int line , const char *);
+6 −4
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2008, 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
@@ -35,6 +35,10 @@
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>

#if defined(CURLDEBUG) && defined(CURLTOOLDEBUG)
#include "memdebug.h"
#endif

static const struct
{
  const char * name;
@@ -69,9 +73,7 @@ static void internalSetEnv(const char * name, char * value)
#ifdef __riscos__
  _kernel_setenv(name, value);
#elif defined (CURLDEBUG)
  extern FILE *curl_debuglogfile;
  if (curl_debuglogfile)
     fprintf (curl_debuglogfile, "ENV %s = %s\n", name, value);
  curl_memlog("ENV %s = %s\n", name, value);
#endif
  return;
}