Back to index

texmacs  1.0.7.15
mem.c
Go to the documentation of this file.
00001 /*  $Header: /home/cvsroot/dvipdfmx/src/mem.c,v 1.8 2009/09/18 23:56:02 matthias Exp $
00002 
00003     This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
00004 
00005     Copyright (C) 2002 by Jin-Hwan Cho and Shunsaku Hirata,
00006     the dvipdfmx project team <dvipdfmx@project.ktug.or.kr>
00007     
00008     Copyright (C) 1998, 1999 by Mark A. Wicks <mwicks@kettering.edu>
00009 
00010     This program is free software; you can redistribute it and/or modify
00011     it under the terms of the GNU General Public License as published by
00012     the Free Software Foundation; either version 2 of the License, or
00013     (at your option) any later version.
00014     
00015     This program is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU General Public License for more details.
00019     
00020     You should have received a copy of the GNU General Public License
00021     along with this program; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00023 */
00024 
00025 #include <stdio.h>   
00026 #include <stdlib.h>
00027 
00028 #include "system.h"
00029 #include "mem.h"
00030 #include "error.h"
00031 
00032 #ifdef MEM_DEBUG
00033 
00034 #include "dpxutil.h"
00035 
00036 #define MEM_LINE   128
00037 
00038 static struct ht_table *mem_ht;
00039 static long int mem_event;
00040 static int mem_internal;
00041 char *mem_str;
00042 
00043 void mem_debug_init(void)
00044 {
00045   mem_event = 0;
00046   mem_internal = 0;
00047   mem_ht = malloc(sizeof(struct ht_table));
00048   ht_init_table(mem_ht, NULL);
00049   fprintf(stderr, "*** Memory debugging started ***\n");
00050 }
00051 
00052 void mem_debug_check(void)
00053 {
00054   struct ht_iter iter;
00055 
00056   fprintf(stderr, "*** Memory still in use ***\n");
00057 
00058   if (ht_set_iter(mem_ht, &iter) == 0) {
00059     do {
00060       int pl;
00061       void *p = *((void **) ht_iter_getkey(&iter, &pl));
00062       char *s = ht_iter_getval(&iter);
00063 
00064       fprintf(stderr, "%p %s\n", p, s);
00065     } while (!ht_iter_next(&iter));
00066   }
00067   ht_clear_iter(&iter);
00068 
00069   fprintf(stderr, "*** End of used memory ***\n");
00070 }
00071 
00072 void *mem_add(void *ptr, const char *file, const char *function, int line) {
00073   if (ptr && !mem_internal) {
00074     mem_internal = 1;
00075     mem_str = malloc(MEM_LINE);
00076     char **p = malloc(sizeof(ptr));
00077     *p = ptr;
00078     snprintf(mem_str, MEM_LINE, "(0x%08lx) %s (%s, %d)"
00079 #ifdef __GNUC__
00080             ", %p" //" %p"
00081 #endif
00082             , ++mem_event, function, file, line
00083 #ifdef __GNUC__
00084             , __builtin_return_address(1) //, __builtin_return_address(2)
00085 #endif
00086             );
00087     ht_append_table(mem_ht, p, sizeof(ptr), mem_str);
00088     mem_internal = 0;
00089   }
00090 
00091   return ptr;
00092 }
00093 
00094 void *mem_remove(void *ptr, const char *file, const char *function, int line) {
00095   if (ptr && !mem_internal) {
00096     mem_internal = 1;
00097     if (!(mem_ht && ht_remove_table(mem_ht, &ptr, sizeof(ptr)))) {
00098       WARN("Trying to free non-allocated memory\n"
00099           "%p %s (%s, %d)"
00100 #ifdef __GNUC__
00101           ", called from %p"
00102 #endif
00103           "\n", ptr, function, file, line
00104 #ifdef __GNUC__
00105           , __builtin_return_address(1)
00106 #endif
00107           );
00108     }
00109     mem_internal = 0;
00110   }
00111 
00112   return ptr;
00113 }
00114 
00115 
00116 #else /* ! MEM_DEBUG */
00117 
00118 static long int mem_count;
00119 
00120 void mem_debug_init(void)
00121 {
00122   mem_count = 0;
00123 }
00124 
00125 void mem_debug_check(void)
00126 {
00127   if (mem_count)
00128     WARN("%ld memory objects still allocated\n", mem_count);
00129 }
00130 
00131 void *mem_add(void *ptr) {
00132   if (ptr)
00133     mem_count++;
00134 
00135   return ptr;
00136 }
00137 
00138 void *mem_remove(void *ptr) {
00139   if (ptr)
00140     mem_count--;
00141 
00142   return ptr;
00143 }
00144 
00145 #endif /* MEM_DEBUG */
00146 
00147 
00148 void *new (size_t size)
00149 {
00150   void *result = malloc (size);
00151   if (!result) {
00152     ERROR("Out of memory - asked for %lu bytes\n", (unsigned long) size);
00153   }
00154 
00155   return result;
00156 }
00157 
00158 void *renew (void *mem, size_t size)
00159 {
00160   if (size) {
00161     void *result = realloc (mem, size);
00162     if (!result) {
00163       ERROR("Out of memory - asked for %lu bytes\n", (unsigned long) size);
00164     }
00165     return result;
00166   } else {
00167     /* realloc may not return NULL if size == 0 */
00168     free(mem);
00169     return NULL;
00170   }
00171 }