Back to index

tetex-bin  3.0
mem.c
Go to the documentation of this file.
00001 /*  $Header$
00002 
00003     This is dvipdfm, a DVI to PDF translator.
00004     Copyright (C) 1998, 1999 by Mark A. Wicks
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019     
00020     The author may be contacted via the e-mail address
00021 
00022        mwicks@kettering.edu
00023 */
00024 
00025 #include <stdio.h>   
00026 #include <stdlib.h>
00027 #include "mem.h"
00028 
00029 #ifdef MEM_DEBUG
00030 FILE *debugfile = NULL;
00031 static long int event = 0;
00032 #endif /* MEM_DEBUG */
00033 
00034 
00035 #ifdef MEM_DEBUG
00036 void mem_debug_init(void)
00037 {
00038   if (debugfile == NULL) {
00039     debugfile = fopen ("malloc.log", "w");
00040     fprintf (stderr, "*** Memory Debugging Log started ***\n");
00041   }
00042 }
00043 #endif /* MEM_DEBUG */
00044 
00045 void *new (size_t size, char *function, int line)
00046 {
00047   void *result;
00048   if ((result = malloc (size)) == NULL) {
00049     fprintf (stderr, "Out of memory in %s:%d\n", function, line);
00050     fprintf (stderr, "Asked for %lu\n", (unsigned long) size);
00051     exit (1);
00052   }
00053 
00054 #ifdef MEM_DEBUG  
00055   mem_debug_init();
00056   event += 1;
00057   fprintf (debugfile, "%p %07ld [new] %s:%d\n", result, event, function, line);
00058 #endif /* MEM_DEBUG */
00059 
00060   return result;
00061 }
00062 
00063 void *renew (void *mem, size_t size, char *function, int line)
00064 {
00065   void *result;
00066 #ifdef MEM_DEBUG
00067     mem_debug_init();
00068     event += 1;
00069   if (mem)
00070     fprintf (debugfile, "%p %07ld [fre] %s:%d\n", mem,
00071             event, function, line);
00072 #endif /* MEM_DEBUG */
00073   if ((result = realloc (mem, size)) == NULL) {
00074     fprintf (stderr, "Out of memory!\n");
00075     exit (1);
00076   }
00077 #ifdef MEM_DEBUG
00078   if (result)
00079     fprintf (debugfile, "%p %07ld [new] %s:%d\n", result, event, function, line);
00080 #endif /* MEM_DEBUG */
00081   return result;
00082 }
00083 
00084 void release (void *mem, char *function, int line)
00085 {
00086 
00087 #ifdef MEM_DEBUG
00088     mem_debug_init();
00089     event += 1;
00090     fprintf (debugfile, "%p %07ld [fre] %s:%d\n", mem, event, function, line);
00091 #endif /* MEM_DEBUG */
00092 
00093   free (mem);
00094 }
00095