1 // This may look like C code, but it is really -*- C++ -*- 2 3 // ------------------------------------------------------------------ 4 // The Goldware Library 5 // Copyright (C) 1990-1999 Odinn Sorensen 6 // Copyright (C) 2000 Alexander S. Aganichev 7 // ------------------------------------------------------------------ 8 // This library is free software; you can redistribute it and/or 9 // modify it under the terms of the GNU Library General Public 10 // License as published by the Free Software Foundation; either 11 // version 2 of the License, or (at your option) any later version. 12 // 13 // This library is distributed in the hope that it will be useful, 14 // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 // Library General Public License for more details. 17 // 18 // You should have received a copy of the GNU Library General Public 19 // License along with this program; if not, write to the Free 20 // Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 21 // MA 02111-1307, USA 22 // ------------------------------------------------------------------ 23 // $Id: gmemdbg.h,v 1.1.1.1 2000/02/25 10:11:05 asa Exp $ 24 // ------------------------------------------------------------------ 25 // Memory management routines with debugging features. 26 // Based on free code from SNIPPETS 9404 by Walter Bright. 27 // ------------------------------------------------------------------ 28 29 #ifndef __gdbgtmem_h 30 #define __gdbgtmem_h 31 32 33 // ------------------------------------------------------------------ 34 35 #include <gdefs.h> 36 37 38 // ------------------------------------------------------------------ 39 // Global variables 40 41 extern int throw_alloc_extra; 42 43 #if defined(GTHROW_LOG) 44 class glog; 45 extern glog* throw_log; 46 #endif 47 48 49 // ------------------------------------------------------------------ 50 // Prototypes 51 52 void throw_init(); 53 54 void throw_check(); 55 void throw_checkptr(void* ptr); 56 57 char* throw_strdup(const char* str); 58 void* throw_malloc(size_t size); 59 void* throw_calloc(size_t items, size_t size); 60 void* throw_realloc(void* ptr, size_t size); 61 void throw_free(void* ptr); 62 63 char* throw_xstrdup(const char* str); 64 void* throw_xmalloc(size_t size); 65 void* throw_xcalloc(size_t items, size_t size); 66 void* throw_xrealloc(void* ptr, size_t size); 67 void throw_xfree(void* ptr); 68 69 void* throw_outofmem(); 70 void* throw_outofmem_report(const char* file, int line); 71 72 73 // ------------------------------------------------------------------ 74 // Debugging prototypes 75 76 void throw_new_debug(const void* ptr, const char* file, int line); 77 char* throw_strdup_debug(const char* str, const char* file, int line); 78 void* throw_malloc_debug(size_t size, const char* file, int line); 79 void* throw_calloc_debug(size_t items, size_t size, const char* file, int line); 80 void* throw_realloc_debug(void* ptr, size_t size, const char* file, int line); 81 void throw_free_debug(void* ptr, const char* file, int line); 82 void throw_check_debug(const char* file, int line); 83 void throw_checkptr_debug(const void* ptr, const char* file, int line); 84 85 void throw_xnew_debug(void* ptr, const char* file, int line); 86 char* throw_xstrdup_debug(const char* str, const char* file, int line); 87 void* throw_xmalloc_debug(size_t size, const char* file, int line); 88 void* throw_xcalloc_debug(size_t items, size_t size, const char* file, int line); 89 void* throw_xrealloc_debug(void* ptr, size_t size, const char* file, int line); 90 void throw_xfree_debug(void* ptr, const char* file, int line); 91 92 93 // ------------------------------------------------------------------ 94 // Macro functions 95 96 #if defined(GTHROW_DEBUG) 97 # define throw_new(p) throw_new_debug((p),__FILE__,__LINE__) 98 # define throw_alloc(p) throw_new_debug((p),__FILE__,__LINE__) 99 # define throw_strdup(p) throw_strdup_debug((p),__FILE__,__LINE__) 100 # define throw_malloc(s) throw_malloc_debug((s),__FILE__,__LINE__) 101 # define throw_calloc(i,s) throw_calloc_debug((i),(s),__FILE__,__LINE__) 102 # define throw_realloc(p,s) throw_realloc_debug((p),(s),__FILE__,__LINE__) 103 # define throw_free(p) throw_free_debug((p),__FILE__,__LINE__) 104 # define throw_check() throw_check_debug(__FILE__,__LINE__) 105 # define throw_checkptr(p) throw_checkptr_debug((p),__FILE__,__LINE__) 106 #else 107 # define throw_new(p) throw_xnew_debug((p),__FILE__,__LINE__) 108 # define throw_alloc(p) throw_xnew_debug((p),__FILE__,__LINE__) 109 # define throw_strdup(p) throw_xstrdup_debug((p),__FILE__,__LINE__) 110 # define throw_malloc(s) throw_xmalloc_debug((s),__FILE__,__LINE__) 111 # define throw_calloc(i,s) throw_xcalloc_debug((i),(s),__FILE__,__LINE__) 112 # define throw_realloc(p,s) throw_xrealloc_debug((p),(s),__FILE__,__LINE__) 113 # define throw_free(p) throw_xfree_debug((p),__FILE__,__LINE__) 114 # define throw_check() 115 # define throw_checkptr(p) 116 # define throw_init() 117 #endif 118 119 #define throw_xnew(p) throw_xnew_debug((p),__FILE__,__LINE__) 120 #define throw_xstrdup(p) throw_xstrdup_debug((p),__FILE__,__LINE__) 121 #define throw_xmalloc(s) throw_xmalloc_debug((s),__FILE__,__LINE__) 122 #define throw_xcalloc(i,s) throw_xcalloc_debug((i),(s),__FILE__,__LINE__) 123 #define throw_xrealloc(p,s) throw_xrealloc_debug((p),(s),__FILE__,__LINE__) 124 #define throw_xfree(p) throw_xfree_debug((p),__FILE__,__LINE__) 125 126 127 // ------------------------------------------------------------------ 128 // Special purpose macros 129 130 #define throw_release(m) { throw_free(m); (m) = NULL; } 131 #define throw_xrelease(m) { throw_xfree(m); (m) = NULL; } 132 133 #define throw_delete(p) { delete (p); (p) = NULL; } 134 #define throw_xdelete(p) { if(p) delete (p); (p) = NULL; } 135 136 #define throw_deletearray(p) { delete[] (p); (p) = NULL; } 137 #define throw_xdeletearray(p) { if(p) delete[] (p); (p) = NULL; } 138 139 #define throw_mallox(__items, __size, __cache) throw_malloc((__items+__cache+1)*__size) 140 #define throw_callox(__items, __size, __cache) throw_calloc(__items+__cache+1, __size) 141 #define throw_reallox(__ptr, __items, __size, __cache) (((__items % __cache) and __ptr) ? (void*)__ptr : throw_realloc(__ptr, (__items+__cache+1)*__size)); 142 143 #define throw_xmallox(__items, __size, __cache) throw_xmalloc((__items+__cache+1)*__size) 144 #define throw_xcallox(__items, __size, __cache) throw_xcalloc(__items+__cache+1, __size) 145 #define throw_xreallox(__ptr, __items, __size, __cache) (((__items % __cache) and __ptr) ? (void*)__ptr : throw_xrealloc(__ptr, (__items+__cache+1)*__size)); 146 147 148 // ------------------------------------------------------------------ 149 150 #if defined(GTHROWCHKPTR_ENABLE) 151 # define THROW_CHECKPTR(p) throw_checkptr(p) 152 # define THROW_CHECK() throw_check() 153 #else 154 # define THROW_CHECKPTR(p) 155 # define THROW_CHECK() 156 #endif 157 158 159 // ------------------------------------------------------------------ 160 161 #endif 162 163 // ------------------------------------------------------------------ 164