Changeset 432
- Timestamp:
- Apr 15, 2004, 4:18:57 PM (22 years ago)
- Location:
- trunk/psLib/src
- Files:
-
- 14 edited
-
Makefile (modified) (3 diffs)
-
sys/psAbort.c (modified) (2 diffs)
-
sys/psAbort.h (modified) (2 diffs)
-
sys/psError.c (modified) (2 diffs)
-
sys/psError.h (modified) (2 diffs)
-
sys/psMemory.c (modified) (15 diffs)
-
sys/psMemory.h (modified) (3 diffs)
-
sysUtils/Makefile (modified) (1 diff)
-
sysUtils/psAbort.c (modified) (2 diffs)
-
sysUtils/psAbort.h (modified) (2 diffs)
-
sysUtils/psError.c (modified) (2 diffs)
-
sysUtils/psError.h (modified) (2 diffs)
-
sysUtils/psMemory.c (modified) (15 diffs)
-
sysUtils/psMemory.h (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/Makefile
r423 r432 4 4 5 5 include Makefile.Globals 6 7 .PHONY: sysUtils installSysUtils cleanSysUtils 6 8 7 9 NAME:= "$Name: not supported by cvs2svn $ " … … 15 17 INSTALLTARGETS = installSysUtils 16 18 CLEANTARGETS = cleanSysUtils 19 DISTCLEANTARGETS = distcleanSysUtils 17 20 18 21 all: $(ALLTARGETS) … … 29 32 cleanSysUtils: 30 33 $(MAKE) --directory=sysUtils clean 34 35 distcleanSysUtils: 36 $(MAKE) --directory=sysUtils distclean 31 37 32 38 -
trunk/psLib/src/sys/psAbort.c
r429 r432 9 9 * @author Eric Van Alst, MHPCC 10 10 * 11 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-04-1 5 20:37:56$11 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-04-16 02:18:57 $ 13 13 * 14 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 57 57 /*****************************************************************************/ 58 58 59 void psAbort( 60 char *name, 61 char *fmt, 62 ... 63 ) 59 void psAbort(const char *name, const char *fmt,... ) 64 60 { 65 61 va_list argPtr; // variable list arguement pointer -
trunk/psLib/src/sys/psAbort.h
r428 r432 9 9 * @author Eric Van Alst, MHPCC 10 10 * 11 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-04-1 5 20:37:12$11 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-04-16 02:18:57 $ 13 13 * 14 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 45 45 */ 46 46 void psAbort( 47 c har *name, /**< Source of abort such as file or function detected */48 c har *fmt, /**< A printf style formatting statement defining msg */47 const char* name, ///< Source of abort such as file or function detected 48 const char* fmt, ///< A printf style formatting statement defining msg 49 49 ... 50 50 ); -
trunk/psLib/src/sys/psError.c
r427 r432 10 10 * @author Eric Van Alst, MHPCC 11 11 * 12 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $13 * @date $Date: 2004-04-1 5 19:04:58$12 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-04-16 02:18:57 $ 14 14 * 15 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 57 57 /*****************************************************************************/ 58 58 59 void psError( 60 char *name, 61 char *fmt, 62 ... 63 ) 59 void psError(const char *name,const char *fmt, ...) 64 60 { 65 61 va_list argPtr; // variable list arguement pointer -
trunk/psLib/src/sys/psError.h
r426 r432 10 10 * @author Eric Van Alst, MHPCC 11 11 * 12 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $13 * @date $Date: 2004-04-1 5 19:03:41$12 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-04-16 02:18:57 $ 14 14 * 15 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 45 45 */ 46 46 void psError( 47 c har *name, /**< Source of error such as file or function detected */48 c har *fmt, /**< A printf style formatting statement defining msg */47 const char *name, ///< Source of error such as file or function detected 48 const char *fmt, ///< A printf style formatting statement defining msg 49 49 ... 50 50 ); -
trunk/psLib/src/sys/psMemory.c
r430 r432 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-04-1 5 21:22:09$10 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-04-16 02:18:57 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 20 20 #include "psMemory.h" 21 21 #include "psError.h" 22 #include "psAbort.h" 22 23 23 24 #define P_PS_MEMMAGIC (void *)0xdeadbeef // Magic number in psMemBlock header 24 25 25 static int bad_memblock(const psMemBlock *m, int quiet);26 static int checkMemBlock(const psMemBlock *m, const char* funcName); 26 27 static int numMemBlock = 0; // size of memBlocks 27 28 static psMemBlock **memBlocks = NULL; … … 58 59 const char *file, int lineno) 59 60 { 60 if (bad_memblock(ptr, 0)) { 61 fprintf(stderr, "Memory corruption or an attempt to manipulate memory " 62 "not allocated with psAlloc at %s", file); 63 if (lineno > 0) { 64 fprintf(stderr, ":%d", lineno); 65 } 66 fprintf(stderr, "\n"); 67 } else if (ptr->refCounter <= 0) { 68 psError(__func__, PS_ERR_BADFREE, PS_NEW_ERROR, 69 "Block %ld allocated at %s:%d freed more than once at %s:%d\n", 70 ptr->id, ptr->file, ptr->lineno, file, lineno); 71 } else if (ptr->refCounter > 1) { 72 psError(__func__, PS_ERR_BADFREE, PS_NEW_ERROR, 73 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n", 74 ptr->id, ptr->file, ptr->lineno, file, lineno); 75 } 76 77 if (lineno > 0) { 61 checkMemBlock(ptr, __func__); 62 63 if (ptr->refCounter <= 0) { 64 psError(__func__, 65 "Block %ld allocated at %s:%d freed more than once at %s:%d\n", 66 ptr->id, ptr->file, ptr->lineno, file, lineno); 67 } else if (ptr->refCounter > 1) { 68 psError(__func__, 69 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n", 70 ptr->id, ptr->file, ptr->lineno, file, lineno); 71 } 72 73 if (lineno > 0) { 78 74 psAbort(__func__, "Detected a problem in the memory system at %s:%d", file, lineno); 79 75 } … … 123 119 * isn't resignalled) 124 120 */ 125 static intmemAllocateCallbackDefault(const psMemBlock *ptr)121 static long memAllocateCallbackDefault(const psMemBlock *ptr) 126 122 { 127 123 static int incr = 0; // "p_psMemAllocateID += incr" 128 124 129 assert (ptr != NULL); // prevent compiler warnings130 131 125 return incr; 132 126 } 133 127 134 static intmemFreeCallbackDefault(const psMemBlock *ptr)128 static long memFreeCallbackDefault(const psMemBlock *ptr) 135 129 { 136 130 static int incr = 0; // "p_psMemFreeID += incr" 137 131 138 assert (ptr != NULL); // prevent compiler warnings139 140 132 return incr; 141 133 } … … 144 136 * The default callbacks, and the routines to change them 145 137 */ 146 static psMem Callback memAllocateCallback = memAllocateCallbackDefault;147 static psMem Callback memFreeCallback = memFreeCallbackDefault;148 149 psMem Callback psMemAllocateSetCallback(psMemCallback func)150 { 151 psMem Callback old = memAllocateCallback;138 static psMemAllocateCallback memAllocateCallback = memAllocateCallbackDefault; 139 static psMemFreeCallback memFreeCallback = memFreeCallbackDefault; 140 141 psMemAllocateCallback psMemAllocateSetCallback(psMemAllocateCallback func) 142 { 143 psMemFreeCallback old = memAllocateCallback; 152 144 153 145 if (func != NULL) { … … 160 152 } 161 153 162 psMem Callback psMemFreeSetCallback(psMemCallback func)163 { 164 psMem Callback old = memFreeCallback;154 psMemFreeCallback psMemFreeSetCallback(psMemFreeCallback func) 155 { 156 psMemFreeCallback old = memFreeCallback; 165 157 166 158 if (func != NULL) { … … 191 183 192 184 static int 193 bad_memblock(const psMemBlock *m, // block to check194 int quiet) // be quiet?185 checkMemBlock(const psMemBlock *m, // block to check 186 const char* funcName) // be quiet? 195 187 { 196 188 if (m == NULL) { 197 if (!quiet) { 198 psError(__func__, PS_ERR_MEMORY_CORRUPTION, PS_NEW_ERROR, 199 "psMemCheckCorruption: NULL memory block\n"); 200 } 189 psError(funcName,"Memory Corruption: NULL memory block found."); 201 190 return(1); 202 191 } 203 192 204 193 if (!ALIGNED(m)) { 205 if (!quiet) { 206 psError(__func__, PS_ERR_MEMORY_CORRUPTION, PS_NEW_ERROR, 207 "psMemCheckCorruption: non-aligned memory block\n"); 208 } 194 psError(funcName, "psMemCheckCorruption: non-aligned memory block"); 209 195 return(1); 210 196 } 211 197 212 if (m->magic0 != P_PS_MEMMAGIC || m->magic != P_PS_MEMMAGIC) { 213 if (!quiet) { 214 psError(__func__, PS_ERR_MEMORY_CORRUPTION, PS_NEW_ERROR, 215 "psMemCheckCorruption: memory block %ld is corrupted\n", m->id); 216 } 198 if (m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC) { 199 psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted", m->id); 217 200 return(1); 218 201 } … … 231 214 for (int i = 1; i <= memid; i++) { 232 215 if (memBlocks[i] != NULL) { 233 if ( bad_memblock(memBlocks[i], 1)) {216 if (checkMemBlock(memBlocks[i], __func__)) { 234 217 nbad++; 235 218 236 memProblemCallback(memBlocks[i], "(psMemCheckCorruption)", 0);219 memProblemCallback(memBlocks[i], __func__, __LINE__); 237 220 238 221 if (abort_on_error) { … … 263 246 ptr->file = file; 264 247 *(int *)&ptr->lineno = lineno; 265 *(void **)&ptr->magic0 = *(void **)&ptr->magic = P_PS_MEMMAGIC; 248 ptr->startblock = P_PS_MEMMAGIC; 249 ptr->endblock = P_PS_MEMMAGIC; 266 250 267 251 ptr->refCounter = 1; // one user so far … … 273 257 p_psMemAllocateID += memAllocateCallback(ptr); 274 258 } 275 if (memid >= n MemBlock) {276 n MemBlock = (nMemBlock == 0) ? 100000 : 2*nMemBlock;277 memBlocks = realloc(memBlocks, n MemBlock*sizeof(psMemBlock *)); // NOT psRealloc259 if (memid >= numMemBlock) { 260 numMemBlock = (numMemBlock == 0) ? 100000 : 2*numMemBlock; 261 memBlocks = realloc(memBlocks, numMemBlock*sizeof(psMemBlock *)); // NOT psRealloc 278 262 } 279 263 memBlocks[ptr->id] = ptr; … … 311 295 } 312 296 313 if ( bad_memblock(ptr, 0)) {297 if (checkMemBlock(ptr, "psFree") != 0) { 314 298 memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it 315 299 } else { … … 369 353 } 370 354 371 assert (*arr == NULL); // Don't generate a memory leak!355 p_psFree(*arr,__FILE__, __LINE__); // Don't generate a memory leak! 372 356 373 357 *arr = p_psAlloc(nleak*sizeof(psMemBlock), __FILE__, __LINE__); … … 395 379 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 396 380 397 if ( bad_memblock(ptr, 0))398 { 399 memProblemCallback(ptr, "(psMemGetRefCounter)", -1);381 if (checkMemBlock(ptr, __func__) != 0) 382 { 383 memProblemCallback(ptr, __func__, __LINE__); 400 384 } 401 385 … … 412 396 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 413 397 414 if ( bad_memblock(ptr, 0))415 { 416 memProblemCallback(ptr, "(psMemIncrRefCounter)", -1);398 if (checkMemBlock(ptr, __func__)) 399 { 400 memProblemCallback(ptr, __func__, __LINE__); 417 401 } 418 402 … … 431 415 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 432 416 433 if ( bad_memblock(ptr, 0))434 { 435 memProblemCallback(ptr, "(psMemDecrRefCounter)", -1);417 if (checkMemBlock(ptr, __func__)) 418 { 419 memProblemCallback(ptr, __func__, __LINE__); 436 420 } 437 421 -
trunk/psLib/src/sys/psMemory.h
r430 r432 14 14 * @author Robert Lupton, Princeton University 15 15 * 16 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $17 * @date $Date: 2004-04-1 5 21:22:09$16 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 17 * @date $Date: 2004-04-16 02:18:57 $ 18 18 * 19 19 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 79 79 80 80 /// Check for memory leaks 81 int psMemCheckLeaks(int id0, ///< don't list blocks with id < id0 82 psMemBlock ***arr, ///< pointer to array of pointers to leaked blocks, or NULL 83 FILE *fd) ///< print list of leaks to fd (or NULL) 84 ; 81 int psMemCheckLeaks( 82 int id0, ///< don't list blocks with id < id0 83 psMemBlock ***arr, ///< pointer to array of pointers to leaked blocks, or NULL 84 FILE *fd ///< print list of leaks to fd (or NULL) 85 ); 85 86 86 87 /// Check for memory corruption 87 int psMemCheckCorruption(int abort_on_error) ///< Abort on detecting corruption? 88 ; 88 int psMemCheckCorruption( 89 int abort_on_error ///< Abort on detecting corruption? 90 ); 89 91 90 92 /// Return reference counter 91 int psMemGetRefCounter(void *vptr) ///< Pointer to get refCounter for 92 ; 93 int psMemGetRefCounter( 94 void *vptr ///< Pointer to get refCounter for 95 ); 93 96 94 97 /// Increment reference counter and return the pointer 95 void *psMemIncrRefCounter(void *vptr) ///< Pointer to increment refCounter, and return 96 ; 98 void *psMemIncrRefCounter( 99 void *vptr ///< Pointer to increment refCounter, and return 100 ); 97 101 98 102 /// Decrement reference counter and return the pointer 99 void *psMemDecrRefCounter(void *vptr) ///< Pointer to decrement refCounter, and return 100 ; 103 void *psMemDecrRefCounter( 104 void *vptr ///< Pointer to decrement refCounter, and return 105 ); 101 106 102 107 /// Set callback for problems 103 psMemProblemCallback psMemProblemCallbackSet(psMemProblemCallback func) ///< Function to run 104 ; 108 psMemProblemCallback psMemProblemCallbackSet( 109 psMemProblemCallback func ///< Function to run 110 ); 105 111 106 112 /// Set callback for out-of-memory 107 psMemExhaustedCallback psMemExhaustedCallbackSet(psMemExhaustedCallback func) ///< Function to run 108 ; 113 psMemExhaustedCallback psMemExhaustedCallbackSet( 114 psMemExhaustedCallback func ///< Function to run 115 ); 109 116 110 117 /// Set call back for when a particular memory block is allocated 111 psMemCallback psMemAllocateCallbackSet(psMemAllocateCallback func) ///< Function to run 112 ; 118 psMemAllocateCallback psMemAllocateCallbackSet( 119 psMemAllocateCallback func ///< Function to run 120 ); 113 121 114 122 /// Set call back for when a particular memory block is freed 115 psMemCallback psMemFreeCallbackSet(psMemFreeCallback func) ///< Function to run 116 ; 123 psMemFreeCallback psMemFreeCallbackSet( 124 psMemFreeCallback func ///< Function to run 125 ); 117 126 118 127 /// get next memory ID … … 121 130 122 131 /// set p_psMemAllocateID to id 123 long psMemAllocateIDSet(long id) ///< ID to set 124 ; 132 long psMemAllocateIDSet( 133 long id ///< ID to set 134 ); 125 135 126 136 /// set p_psMemFreeID to id 127 long psMemFreeIDSet(long id) ///< ID to set 128 ; 137 long psMemFreeIDSet( 138 long id ///< ID to set 139 ); 129 140 130 141 //@} End of public Functions -
trunk/psLib/src/sysUtils/Makefile
r423 r432 13 13 include ../Makefile.Globals 14 14 15 CFLAGS = $(CFLAGS_MODULE) $(INCLUDE_GLOBAL) 16 17 SRC_OBJS = psTrace.o 15 SRC_OBJS = psMemory.o psError.o psAbort.o 18 16 19 17 libpsSysUtils.a: $(SRC_OBJS) -
trunk/psLib/src/sysUtils/psAbort.c
r429 r432 9 9 * @author Eric Van Alst, MHPCC 10 10 * 11 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-04-1 5 20:37:56$11 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-04-16 02:18:57 $ 13 13 * 14 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 57 57 /*****************************************************************************/ 58 58 59 void psAbort( 60 char *name, 61 char *fmt, 62 ... 63 ) 59 void psAbort(const char *name, const char *fmt,... ) 64 60 { 65 61 va_list argPtr; // variable list arguement pointer -
trunk/psLib/src/sysUtils/psAbort.h
r428 r432 9 9 * @author Eric Van Alst, MHPCC 10 10 * 11 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-04-1 5 20:37:12$11 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-04-16 02:18:57 $ 13 13 * 14 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 45 45 */ 46 46 void psAbort( 47 c har *name, /**< Source of abort such as file or function detected */48 c har *fmt, /**< A printf style formatting statement defining msg */47 const char* name, ///< Source of abort such as file or function detected 48 const char* fmt, ///< A printf style formatting statement defining msg 49 49 ... 50 50 ); -
trunk/psLib/src/sysUtils/psError.c
r427 r432 10 10 * @author Eric Van Alst, MHPCC 11 11 * 12 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $13 * @date $Date: 2004-04-1 5 19:04:58$12 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-04-16 02:18:57 $ 14 14 * 15 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 57 57 /*****************************************************************************/ 58 58 59 void psError( 60 char *name, 61 char *fmt, 62 ... 63 ) 59 void psError(const char *name,const char *fmt, ...) 64 60 { 65 61 va_list argPtr; // variable list arguement pointer -
trunk/psLib/src/sysUtils/psError.h
r426 r432 10 10 * @author Eric Van Alst, MHPCC 11 11 * 12 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $13 * @date $Date: 2004-04-1 5 19:03:41$12 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-04-16 02:18:57 $ 14 14 * 15 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 45 45 */ 46 46 void psError( 47 c har *name, /**< Source of error such as file or function detected */48 c har *fmt, /**< A printf style formatting statement defining msg */47 const char *name, ///< Source of error such as file or function detected 48 const char *fmt, ///< A printf style formatting statement defining msg 49 49 ... 50 50 ); -
trunk/psLib/src/sysUtils/psMemory.c
r430 r432 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-04-1 5 21:22:09$10 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-04-16 02:18:57 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 20 20 #include "psMemory.h" 21 21 #include "psError.h" 22 #include "psAbort.h" 22 23 23 24 #define P_PS_MEMMAGIC (void *)0xdeadbeef // Magic number in psMemBlock header 24 25 25 static int bad_memblock(const psMemBlock *m, int quiet);26 static int checkMemBlock(const psMemBlock *m, const char* funcName); 26 27 static int numMemBlock = 0; // size of memBlocks 27 28 static psMemBlock **memBlocks = NULL; … … 58 59 const char *file, int lineno) 59 60 { 60 if (bad_memblock(ptr, 0)) { 61 fprintf(stderr, "Memory corruption or an attempt to manipulate memory " 62 "not allocated with psAlloc at %s", file); 63 if (lineno > 0) { 64 fprintf(stderr, ":%d", lineno); 65 } 66 fprintf(stderr, "\n"); 67 } else if (ptr->refCounter <= 0) { 68 psError(__func__, PS_ERR_BADFREE, PS_NEW_ERROR, 69 "Block %ld allocated at %s:%d freed more than once at %s:%d\n", 70 ptr->id, ptr->file, ptr->lineno, file, lineno); 71 } else if (ptr->refCounter > 1) { 72 psError(__func__, PS_ERR_BADFREE, PS_NEW_ERROR, 73 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n", 74 ptr->id, ptr->file, ptr->lineno, file, lineno); 75 } 76 77 if (lineno > 0) { 61 checkMemBlock(ptr, __func__); 62 63 if (ptr->refCounter <= 0) { 64 psError(__func__, 65 "Block %ld allocated at %s:%d freed more than once at %s:%d\n", 66 ptr->id, ptr->file, ptr->lineno, file, lineno); 67 } else if (ptr->refCounter > 1) { 68 psError(__func__, 69 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n", 70 ptr->id, ptr->file, ptr->lineno, file, lineno); 71 } 72 73 if (lineno > 0) { 78 74 psAbort(__func__, "Detected a problem in the memory system at %s:%d", file, lineno); 79 75 } … … 123 119 * isn't resignalled) 124 120 */ 125 static intmemAllocateCallbackDefault(const psMemBlock *ptr)121 static long memAllocateCallbackDefault(const psMemBlock *ptr) 126 122 { 127 123 static int incr = 0; // "p_psMemAllocateID += incr" 128 124 129 assert (ptr != NULL); // prevent compiler warnings130 131 125 return incr; 132 126 } 133 127 134 static intmemFreeCallbackDefault(const psMemBlock *ptr)128 static long memFreeCallbackDefault(const psMemBlock *ptr) 135 129 { 136 130 static int incr = 0; // "p_psMemFreeID += incr" 137 131 138 assert (ptr != NULL); // prevent compiler warnings139 140 132 return incr; 141 133 } … … 144 136 * The default callbacks, and the routines to change them 145 137 */ 146 static psMem Callback memAllocateCallback = memAllocateCallbackDefault;147 static psMem Callback memFreeCallback = memFreeCallbackDefault;148 149 psMem Callback psMemAllocateSetCallback(psMemCallback func)150 { 151 psMem Callback old = memAllocateCallback;138 static psMemAllocateCallback memAllocateCallback = memAllocateCallbackDefault; 139 static psMemFreeCallback memFreeCallback = memFreeCallbackDefault; 140 141 psMemAllocateCallback psMemAllocateSetCallback(psMemAllocateCallback func) 142 { 143 psMemFreeCallback old = memAllocateCallback; 152 144 153 145 if (func != NULL) { … … 160 152 } 161 153 162 psMem Callback psMemFreeSetCallback(psMemCallback func)163 { 164 psMem Callback old = memFreeCallback;154 psMemFreeCallback psMemFreeSetCallback(psMemFreeCallback func) 155 { 156 psMemFreeCallback old = memFreeCallback; 165 157 166 158 if (func != NULL) { … … 191 183 192 184 static int 193 bad_memblock(const psMemBlock *m, // block to check194 int quiet) // be quiet?185 checkMemBlock(const psMemBlock *m, // block to check 186 const char* funcName) // be quiet? 195 187 { 196 188 if (m == NULL) { 197 if (!quiet) { 198 psError(__func__, PS_ERR_MEMORY_CORRUPTION, PS_NEW_ERROR, 199 "psMemCheckCorruption: NULL memory block\n"); 200 } 189 psError(funcName,"Memory Corruption: NULL memory block found."); 201 190 return(1); 202 191 } 203 192 204 193 if (!ALIGNED(m)) { 205 if (!quiet) { 206 psError(__func__, PS_ERR_MEMORY_CORRUPTION, PS_NEW_ERROR, 207 "psMemCheckCorruption: non-aligned memory block\n"); 208 } 194 psError(funcName, "psMemCheckCorruption: non-aligned memory block"); 209 195 return(1); 210 196 } 211 197 212 if (m->magic0 != P_PS_MEMMAGIC || m->magic != P_PS_MEMMAGIC) { 213 if (!quiet) { 214 psError(__func__, PS_ERR_MEMORY_CORRUPTION, PS_NEW_ERROR, 215 "psMemCheckCorruption: memory block %ld is corrupted\n", m->id); 216 } 198 if (m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC) { 199 psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted", m->id); 217 200 return(1); 218 201 } … … 231 214 for (int i = 1; i <= memid; i++) { 232 215 if (memBlocks[i] != NULL) { 233 if ( bad_memblock(memBlocks[i], 1)) {216 if (checkMemBlock(memBlocks[i], __func__)) { 234 217 nbad++; 235 218 236 memProblemCallback(memBlocks[i], "(psMemCheckCorruption)", 0);219 memProblemCallback(memBlocks[i], __func__, __LINE__); 237 220 238 221 if (abort_on_error) { … … 263 246 ptr->file = file; 264 247 *(int *)&ptr->lineno = lineno; 265 *(void **)&ptr->magic0 = *(void **)&ptr->magic = P_PS_MEMMAGIC; 248 ptr->startblock = P_PS_MEMMAGIC; 249 ptr->endblock = P_PS_MEMMAGIC; 266 250 267 251 ptr->refCounter = 1; // one user so far … … 273 257 p_psMemAllocateID += memAllocateCallback(ptr); 274 258 } 275 if (memid >= n MemBlock) {276 n MemBlock = (nMemBlock == 0) ? 100000 : 2*nMemBlock;277 memBlocks = realloc(memBlocks, n MemBlock*sizeof(psMemBlock *)); // NOT psRealloc259 if (memid >= numMemBlock) { 260 numMemBlock = (numMemBlock == 0) ? 100000 : 2*numMemBlock; 261 memBlocks = realloc(memBlocks, numMemBlock*sizeof(psMemBlock *)); // NOT psRealloc 278 262 } 279 263 memBlocks[ptr->id] = ptr; … … 311 295 } 312 296 313 if ( bad_memblock(ptr, 0)) {297 if (checkMemBlock(ptr, "psFree") != 0) { 314 298 memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it 315 299 } else { … … 369 353 } 370 354 371 assert (*arr == NULL); // Don't generate a memory leak!355 p_psFree(*arr,__FILE__, __LINE__); // Don't generate a memory leak! 372 356 373 357 *arr = p_psAlloc(nleak*sizeof(psMemBlock), __FILE__, __LINE__); … … 395 379 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 396 380 397 if ( bad_memblock(ptr, 0))398 { 399 memProblemCallback(ptr, "(psMemGetRefCounter)", -1);381 if (checkMemBlock(ptr, __func__) != 0) 382 { 383 memProblemCallback(ptr, __func__, __LINE__); 400 384 } 401 385 … … 412 396 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 413 397 414 if ( bad_memblock(ptr, 0))415 { 416 memProblemCallback(ptr, "(psMemIncrRefCounter)", -1);398 if (checkMemBlock(ptr, __func__)) 399 { 400 memProblemCallback(ptr, __func__, __LINE__); 417 401 } 418 402 … … 431 415 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 432 416 433 if ( bad_memblock(ptr, 0))434 { 435 memProblemCallback(ptr, "(psMemDecrRefCounter)", -1);417 if (checkMemBlock(ptr, __func__)) 418 { 419 memProblemCallback(ptr, __func__, __LINE__); 436 420 } 437 421 -
trunk/psLib/src/sysUtils/psMemory.h
r430 r432 14 14 * @author Robert Lupton, Princeton University 15 15 * 16 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $17 * @date $Date: 2004-04-1 5 21:22:09$16 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 17 * @date $Date: 2004-04-16 02:18:57 $ 18 18 * 19 19 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 79 79 80 80 /// Check for memory leaks 81 int psMemCheckLeaks(int id0, ///< don't list blocks with id < id0 82 psMemBlock ***arr, ///< pointer to array of pointers to leaked blocks, or NULL 83 FILE *fd) ///< print list of leaks to fd (or NULL) 84 ; 81 int psMemCheckLeaks( 82 int id0, ///< don't list blocks with id < id0 83 psMemBlock ***arr, ///< pointer to array of pointers to leaked blocks, or NULL 84 FILE *fd ///< print list of leaks to fd (or NULL) 85 ); 85 86 86 87 /// Check for memory corruption 87 int psMemCheckCorruption(int abort_on_error) ///< Abort on detecting corruption? 88 ; 88 int psMemCheckCorruption( 89 int abort_on_error ///< Abort on detecting corruption? 90 ); 89 91 90 92 /// Return reference counter 91 int psMemGetRefCounter(void *vptr) ///< Pointer to get refCounter for 92 ; 93 int psMemGetRefCounter( 94 void *vptr ///< Pointer to get refCounter for 95 ); 93 96 94 97 /// Increment reference counter and return the pointer 95 void *psMemIncrRefCounter(void *vptr) ///< Pointer to increment refCounter, and return 96 ; 98 void *psMemIncrRefCounter( 99 void *vptr ///< Pointer to increment refCounter, and return 100 ); 97 101 98 102 /// Decrement reference counter and return the pointer 99 void *psMemDecrRefCounter(void *vptr) ///< Pointer to decrement refCounter, and return 100 ; 103 void *psMemDecrRefCounter( 104 void *vptr ///< Pointer to decrement refCounter, and return 105 ); 101 106 102 107 /// Set callback for problems 103 psMemProblemCallback psMemProblemCallbackSet(psMemProblemCallback func) ///< Function to run 104 ; 108 psMemProblemCallback psMemProblemCallbackSet( 109 psMemProblemCallback func ///< Function to run 110 ); 105 111 106 112 /// Set callback for out-of-memory 107 psMemExhaustedCallback psMemExhaustedCallbackSet(psMemExhaustedCallback func) ///< Function to run 108 ; 113 psMemExhaustedCallback psMemExhaustedCallbackSet( 114 psMemExhaustedCallback func ///< Function to run 115 ); 109 116 110 117 /// Set call back for when a particular memory block is allocated 111 psMemCallback psMemAllocateCallbackSet(psMemAllocateCallback func) ///< Function to run 112 ; 118 psMemAllocateCallback psMemAllocateCallbackSet( 119 psMemAllocateCallback func ///< Function to run 120 ); 113 121 114 122 /// Set call back for when a particular memory block is freed 115 psMemCallback psMemFreeCallbackSet(psMemFreeCallback func) ///< Function to run 116 ; 123 psMemFreeCallback psMemFreeCallbackSet( 124 psMemFreeCallback func ///< Function to run 125 ); 117 126 118 127 /// get next memory ID … … 121 130 122 131 /// set p_psMemAllocateID to id 123 long psMemAllocateIDSet(long id) ///< ID to set 124 ; 132 long psMemAllocateIDSet( 133 long id ///< ID to set 134 ); 125 135 126 136 /// set p_psMemFreeID to id 127 long psMemFreeIDSet(long id) ///< ID to set 128 ; 137 long psMemFreeIDSet( 138 long id ///< ID to set 139 ); 129 140 130 141 //@} End of public Functions
Note:
See TracChangeset
for help on using the changeset viewer.
