Changeset 264
- Timestamp:
- Mar 19, 2004, 9:13:42 AM (22 years ago)
- File:
-
- 1 edited
-
trunk/archive/pslib/src/Utils/memory.c (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/archive/pslib/src/Utils/memory.c
r227 r264 2 2 #include <stdio.h> 3 3 #include <assert.h> 4 #define PS_ALLOW_MALLOC // we're allowed to call malloc() 4 5 #include "psUtils.h" 5 6 #include "Private/p_psMemory.h" 6 7 #undef malloc // _We_ are allowed to call these8 #undef realloc9 #undef free10 7 11 8 #undef psAlloc // don't get the __FILE__ versions … … 15 12 static int bad_memblock(const psMemBlock *m, int quiet); 16 13 /* 17 * This strawman allocation package only handles up to NMEMBLOCK allocations 18 */ 19 #define NMEMBLOCK 10000 // max. number of calls to psAlloc 20 static psMemBlock *memBlocks[NMEMBLOCK]; 14 * This strawman allocation package uses an array to track calls to psAlloc; 15 * not nearly good enough for production work! 16 */ 17 static int nMemBlock = 0; // size of memBlocks 18 static psMemBlock **memBlocks = NULL; 21 19 22 20 /*****************************************************************************/ … … 77 75 } 78 76 fprintf(stderr, "\n"); 79 } else if (ptr->refCounter <= 0) {77 } else if (ptr->refCounter <= 0) { 80 78 psError(__func__, "Block %ld allocated at %s:%d freed more than once at %s:%d\n", 81 79 ptr->id, ptr->file, ptr->lineno, file, lineno); 82 } else if (ptr->refCounter > 1) {80 } else if (ptr->refCounter > 1) { 83 81 psError(__func__, "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n", 84 82 ptr->id, ptr->file, ptr->lineno, file, lineno); … … 138 136 } 139 137 138 static int memFreeCB0(const psMemBlock *ptr) 139 { 140 static int incr = 0; // "p_psMemFreeID += incr" 141 142 assert (ptr != NULL); // prevent compiler warnings 143 144 return incr; 145 } 146 140 147 /* 141 148 * The default callbacks, and the routines to change them 142 149 */ 143 150 static psMemCallback memAllocateCB = memAllocateCB0; 144 static psMemCallback memFreeCB = mem AllocateCB0;151 static psMemCallback memFreeCB = memFreeCB0; 145 152 146 153 psMemCallback psMemAllocateSetCB(psMemCallback func) … … 157 164 psMemCallback old = memFreeCB; 158 165 159 memFreeCB = (func != NULL) ? func : mem AllocateCB0;166 memFreeCB = (func != NULL) ? func : memFreeCB0; 160 167 161 168 return old; … … 183 190 int quiet) // be quiet? 184 191 { 185 if (m == NULL) {192 if (m == NULL) { 186 193 if (!quiet) { 187 194 psError(__func__, "psMemCheckCorruption: NULL memory block\n"); … … 190 197 } 191 198 192 if (!ALIGNED(m)) {199 if (!ALIGNED(m)) { 193 200 if (!quiet) { 194 201 psError(__func__, "psMemCheckCorruption: non-aligned memory block\n"); … … 197 204 } 198 205 199 if (m->magic0 != P_PS_MEMMAGIC || m->magic != P_PS_MEMMAGIC) {206 if (m->magic0 != P_PS_MEMMAGIC || m->magic != P_PS_MEMMAGIC) { 200 207 if (!quiet) { 201 208 psError(__func__, … … 211 218 { 212 219 int nbad = 0; // number of bad blocks 220 221 if (memBlocks == NULL) { 222 return 0; // no memory is allocated to be corrupted 223 } 213 224 214 225 for (int i = 1; i <= memid; i++) { … … 252 263 * Did the user ask to be informed about this allocation? 253 264 */ 254 if(ptr->id == p_psMemAllocateID) { 265 266 if (ptr->id == p_psMemAllocateID && p_psMemAllocateID > 0) { 255 267 p_psMemAllocateID += memAllocateCB(ptr); 256 268 } 257 269 /* 258 * Register the allocation. This is not production quality!270 * Register the allocation. This is not a production quality algorithm! 259 271 */ 260 if (memid == NMEMBLOCK) {261 psAbort(__func__, "I'm sorry, but I can only handle %d allocations",262 NMEMBLOCK);272 if (memid >= nMemBlock) { 273 nMemBlock = (nMemBlock == 0) ? 10000 : 2*nMemBlock; 274 memBlocks = realloc(memBlocks, nMemBlock*sizeof(psMemBlock *)); // NOT psRealloc 263 275 } 264 276 memBlocks[ptr->id] = ptr; … … 292 304 * Did the user ask to be informed about this deallocation? 293 305 */ 294 if (ptr->id == p_psMemFreeID ) {306 if (ptr->id == p_psMemFreeID && p_psMemFreeID > 0) { 295 307 p_psMemFreeID += memFreeCB(ptr); 296 308 } … … 303 315 } 304 316 ptr->refCounter--; 317 #if 1 305 318 memBlocks[ptr->id] = NULL; // XXX sub-optimal! Can't check 306 // freed blocks for corruption 319 // freed blocks for corruption/double frees 307 320 free(ptr); 321 #endif 308 322 } 309 323 } … … 320 334 { 321 335 int nleak = 0; 322 336 337 if (memBlocks == NULL) { // nothing was allocated 338 return 0; 339 } 340 323 341 for (int i = id0; i <= memid; i++) { 324 342 if (memBlocks[i] != NULL) { … … 408 426 return vptr; 409 427 } 410 411 /*****************************************************************************/412 #if 0 // Testing413 #include <signal.h>414 /*415 * These are usually defined in psMemory.h, but in this file (only!)416 * they are undefined explicitly, so let's get them back417 */418 #define psAlloc(size) p_psAlloc(size, __FILE__, __LINE__)419 #define psRealloc(ptr, size) p_psRealloc(ptr, size, __FILE__, __LINE__)420 #define psFree(size) p_psFree(size, __FILE__, __LINE__)421 422 /*****************************************************************************/423 /*424 * My callbacks425 */426 static int my_MemAllocateCB(const psMemBlock *ptr)427 {428 static int incr = 0; // "p_psMemAllocateID += incr"429 430 assert (ptr != NULL); // prevent compiler warnings431 432 fprintf(stderr, "Allocating block %ld (%s:%d)\n",433 ptr->id, ptr->file, ptr->lineno);434 435 incr = 2; // See every other allocation436 437 return incr;438 }439 440 static void *my_MemExhaustedCB(size_t size)441 {442 psError(__func__, "Bye bye sweet world\n");443 444 return NULL;445 }446 447 static psMemProblemCallback default_MemProblemCB = NULL;448 449 static void my_MemProblemCB(const psMemBlock *ptr,450 const char *file, int lineno)451 {452 fprintf(stderr, "I foresee trouble:\n");453 454 default_MemProblemCB(ptr, file, lineno);455 }456 457 /*****************************************************************************/458 459 int main(void)460 {461 /*462 * Survive any aborts that are generated463 * This doesn't seem to work on mac os/x 10.2.8464 */465 if (signal(SIGABRT, SIG_IGN) == SIG_ERR) {466 perror("Installing a SIGABRT handler");467 }468 /*469 * Install my callbacks470 */471 (void)psMemAllocateSetCB(my_MemAllocateCB);472 default_MemProblemCB = psMemProblemSetCB(my_MemProblemCB);473 (void)psMemExhaustedSetCB(my_MemExhaustedCB);474 /*475 * Request callback on first allocation476 */477 (void)psMemSetAllocateID(1);478 /*479 * Start allocating480 */481 char *str = psAlloc(10);482 double *x = psAlloc(1);483 int *i = psAlloc(1);484 *i = 1;485 *x = 1.234;486 487 psFree(i);488 489 str = psRealloc(str, 20);490 psFree(str);491 #if 1492 /*493 * An illegal double-free494 */495 psFree(str);496 #endif497 498 #if 1499 /*500 * Free memory not allocated with psAlloc501 */502 psFree(malloc(1));503 #endif504 505 #if 0506 /*507 * Ask for more memory than we can get508 */509 psAlloc((size_t)-1 - sizeof(psMemBlock));510 #endif511 512 #if 0513 /*514 * Increment refCounter on non-malloced pointer515 */516 psMemGetRefCounter(malloc(1));517 #endif518 519 #if 0520 /*521 * Mess with the refCounter522 */523 (void)psMemIncrRefCounter(x);524 (void)psMemIncrRefCounter(x);525 (void)psMemDecrRefCounter(x); // now == 2526 #endif527 528 #if 0529 /*530 * A memory leak531 */532 {533 char *str2 = psAlloc(20);534 535 sprintf(str2, "XXX");536 }537 #else538 psFree(x);539 #endif540 541 #if 1542 /*543 * Corrupt some memory544 */545 char *c = psAlloc(1);546 c[-4] = 0;547 #endif548 549 /*************************************************************************/550 /*551 * Check for memory corruption552 */553 if(psMemCheckCorruption(1)) {554 psError(__func__, "Detected corrupted memory\n\n");555 }556 /*557 * Check for memory leaks558 */559 psMemBlock **leaks = NULL;560 int nleak = psMemCheckLeaks(0, &leaks, NULL);561 562 if (nleak > 0) {563 fprintf(stderr, "Memory is leaking (%d blocks):\n", nleak);564 fprintf(stderr, " %20s:line ID\n", "file");565 for (int i = 0; i < nleak; i++) {566 fprintf(stderr, " %20s:%-4d %ld\n",567 leaks[i]->file, leaks[i]->lineno, leaks[i]->id);568 }569 psFree(leaks);570 }571 572 if ((nleak = psMemCheckLeaks(0, NULL, NULL)) > 0) {573 fprintf(stderr, "Memory is still leaking (%d blocks):\n", nleak);574 psMemCheckLeaks(0, NULL, stderr);575 }576 577 return 0;578 }579 #endif
Note:
See TracChangeset
for help on using the changeset viewer.
