Changeset 432 for trunk/psLib/src/sys/psMemory.c
- Timestamp:
- Apr 15, 2004, 4:18:57 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sys/psMemory.c (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
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
Note:
See TracChangeset
for help on using the changeset viewer.
