"Fossies" - the Fresh Open Source Software Archive

Member "epstool-3.08/src/calloc.c" (10 Jun 2005, 4767 Bytes) of package /linux/misc/old/ghost/ghostgum/epstool-3.08-os2.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Copyright (C) 2001-2005 Ghostgum Software Pty Ltd.  All rights reserved.
    2 
    3   This software is provided AS-IS with no warranty, either express or
    4   implied.
    5 
    6   This software is distributed under licence and may not be copied,
    7   modified or distributed except as expressly authorised under the terms
    8   of the licence contained in the file LICENCE in this distribution.
    9 
   10   For more information about licensing, please refer to
   11   http://www.ghostgum.com.au/ or contact Ghostsgum Software Pty Ltd, 
   12   218 Gallaghers Rd, Glen Waverley VIC 3150, AUSTRALIA, 
   13   Fax +61 3 9886 6616.
   14 */
   15 
   16 /* $Id: calloc.c,v 1.10 2005/06/10 09:39:24 ghostgum Exp $ */
   17 /* A debugging malloc */
   18 
   19 /* This is prettly clunky, but it allows us to check if there
   20  * are any memory leaks and to check for double free.
   21  * If you enable (debug & DEBUG_MALLOC), then you must do
   22  * so before any memory is allocated, otherwise it will
   23  * crash when it frees memory on exit.
   24  */
   25 
   26 #include "common.h"
   27 
   28 #ifdef DEBUG_MALLOC
   29 FILE *malloc_file;
   30 #ifdef UNIX
   31 #define MALLOC_FILE "gsview.txt"
   32 #else
   33 #define MALLOC_FILE "c:\\gsview.txt"
   34 #endif
   35 
   36 /*
   37 #define DEBUG_PRINT
   38 */
   39 
   40 #undef malloc
   41 #undef calloc
   42 #undef realloc
   43 #undef free
   44 size_t allocated_memory = 0;
   45 
   46 
   47 #define SIGNATURE 0xa55a
   48 typedef struct DMEM_s {
   49     size_t size;    /* size of this chunk */
   50     int signature;  /* to make sure we aren't looking at garbage */
   51     char notes[120];    /* description of block */
   52 } DMEM;
   53 
   54 void * debug_malloc(size_t size, const char *file, int line)
   55 {
   56     if (debug & DEBUG_MEM) {
   57     char buf[MAXSTR];
   58     DMEM *mem = (DMEM *)malloc(size+sizeof(DMEM));
   59     void *p = NULL;
   60     if (mem) {
   61         memset(mem, 0, size+sizeof(DMEM));
   62         mem->size = size;
   63         mem->signature = SIGNATURE;
   64         allocated_memory += size;
   65         snprintf(mem->notes, sizeof(mem->notes), "%s:%d %ld", 
   66         file, line, (long)size);
   67         p = ((char *)mem) + sizeof(DMEM);
   68     }
   69     snprintf(buf, sizeof(buf), 
   70         "%s, 0x%08lx, malloc(%ld), allocated = %ld\n", 
   71         mem->notes, (long)p, (long)size, (long)allocated_memory);
   72     buf[sizeof(buf)-1]='\0';
   73 #ifdef DEBUG_PRINT
   74     fputs(buf, stderr);
   75 #endif
   76 #ifdef DEBUG_MALLOC
   77     if (malloc_file == (FILE *)NULL)
   78         malloc_file = fopen(MALLOC_FILE, "w");
   79     if (malloc_file != (FILE *)NULL) {
   80         fputs(buf, malloc_file);
   81         fflush(malloc_file);
   82     }
   83 #endif
   84     return p;
   85     }
   86 
   87     return malloc(size);
   88 }
   89 
   90 
   91 void * debug_realloc(void *block, size_t size)
   92 {
   93     if (debug & DEBUG_MEM) {
   94     char buf[MAXSTR];
   95     char *p = NULL;
   96     size_t oldsize = 0;
   97     if (block == NULL) {
   98         strncpy(buf, "debug_realloc: null pointer\n", sizeof(buf));
   99     }
  100     else {
  101         DMEM *old = (DMEM *)(((char *)block)-sizeof(DMEM));
  102         DMEM *mem;
  103         char *p;
  104         oldsize = old->size;
  105         allocated_memory -= oldsize;
  106         mem = realloc(old, size + sizeof(DMEM));
  107         if (mem) {
  108         memset(mem, 0, size+sizeof(DMEM));
  109         mem->size = size;
  110         mem->signature = SIGNATURE;
  111         allocated_memory += size;
  112         p = ((char *)mem) + sizeof(DMEM);
  113         }
  114         snprintf(buf, sizeof(buf),
  115         "realloc old %ld, new %ld, allocated = %ld\n",
  116         (long)oldsize, (long)size, (long)allocated_memory);
  117         buf[sizeof(buf)-1]='\0';
  118     }
  119 
  120 #ifdef DEBUG_PRINT
  121     fputs(buf, stderr);
  122 #endif
  123 #ifdef DEBUG_MALLOC
  124     if (malloc_file == (FILE *)NULL)
  125         malloc_file = fopen(MALLOC_FILE, "w");
  126     if (malloc_file != (FILE *)NULL) {
  127         fputs(buf, malloc_file);
  128         fflush(malloc_file);
  129     }
  130 #endif
  131     return p;
  132     }
  133 
  134     return realloc(block, size);
  135 }
  136 
  137 void debug_free(void *block)
  138 {
  139     if (debug & DEBUG_MEM) {
  140     char buf[MAXSTR];
  141     DMEM *mem = (DMEM *)(((char *)block)-sizeof(DMEM));
  142     if (block == NULL) {
  143         strncpy(buf, "debug_free: null pointer\n", sizeof(buf));
  144     }
  145     else {
  146         size_t oldsize = mem->size;
  147         allocated_memory -= oldsize;
  148         if (mem->signature != SIGNATURE)
  149             snprintf(buf, sizeof(buf), 
  150             "free 0x%08lx NOT VALID\n", (long)block);
  151         else
  152             snprintf(buf, sizeof(buf), 
  153             "%s, 0x%08lx, free %ld, allocated = %ld\n",
  154             mem->notes, (long)block, 
  155             (long)oldsize, (long)allocated_memory);
  156         buf[sizeof(buf)-1]='\0';
  157         memset(mem, 0, oldsize + sizeof(DMEM));
  158     }
  159 #ifdef DEBUG_PRINT
  160     fputs(buf, stderr);
  161 #endif
  162 #ifdef DEBUG_MALLOC
  163     if (malloc_file == (FILE *)NULL)
  164         malloc_file = fopen(MALLOC_FILE, "w");
  165     if (malloc_file != (FILE *)NULL) {
  166         fputs(buf, malloc_file);
  167         fflush(malloc_file);
  168     }
  169 #endif
  170     if (block != NULL)
  171         free((void *)mem);
  172     return;
  173     }
  174 
  175     free(block);
  176     return;
  177 }
  178 
  179 void debug_memory_report(void)
  180 {
  181     if (debug & DEBUG_MEM) {
  182     char buf[256];
  183     snprintf(buf, sizeof(buf), "Allocated memory = %ld bytes\n", 
  184         (long)allocated_memory);
  185     buf[sizeof(buf)-1]='\0';
  186 #ifdef DEBUG_PRINT
  187     fputs(buf, stderr);
  188 #endif
  189 #ifdef DEBUG_MALLOC
  190     fputs(buf, malloc_file);
  191     fclose(malloc_file);
  192 #endif
  193     }
  194 }
  195 #endif /* DEBUG_MALLOC */