Changeset 830
- Timestamp:
- Jun 2, 2004, 10:03:22 AM (22 years ago)
- Location:
- trunk/psLib/src
- Files:
-
- 4 edited
-
sys/psMemory.c (modified) (9 diffs)
-
sys/psMemory.h (modified) (3 diffs)
-
sysUtils/psMemory.c (modified) (9 diffs)
-
sysUtils/psMemory.h (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psMemory.c
r803 r830 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1. 19$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-0 5-28 03:17:39$10 * @version $Revision: 1.20 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-06-02 20:03:22 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 16 16 #define PS_ALLOW_MALLOC // we're allowed to call malloc() 17 17 18 #include <pthread.h> // we need a mutex to make this stuff thread safe.19 18 #include <stdlib.h> 20 19 #include <stdio.h> … … 25 24 #include "psError.h" 26 25 #include "psAbort.h" 26 #include "psLogMsg.h" 27 27 28 28 #define P_PS_MEMMAGIC (void *)0xdeadbeef // Magic number in psMemBlock header … … 67 67 psError(__func__, 68 68 "Block %ld allocated at %s:%d freed more than once at %s:%d\n", 69 ptr->id, ptr->file, ptr->lineno, file, lineno);70 } else if (ptr->refCounter > 1) {71 psError(__func__,72 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n",73 69 ptr->id, ptr->file, ptr->lineno, file, lineno); 74 70 } … … 268 264 ptr->startblock = P_PS_MEMMAGIC; 269 265 ptr->endblock = P_PS_MEMMAGIC; 270 ptr->refCounter = 1; // one user so far271 266 ptr->userMemorySize = size; 272 267 *(void**)((int8_t*)(ptr+1)+size) = P_PS_MEMMAGIC; 273 268 ptr->previousBlock = NULL; 269 270 pthread_mutex_init(&ptr->refCounterMutex, NULL); 271 ptr->refCounter = 1; // one user so far 274 272 275 273 // need exclusive access of the memory block list now... … … 347 345 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 348 346 349 // Did the user ask to be informed about this deallocation? 350 if (ptr->id == p_psMemFreeID) { 351 p_psMemFreeID += memFreeCallback(ptr); 352 } 353 354 if (checkMemBlock(ptr, "psFree") != 0) { 347 if (checkMemBlock(ptr, __func__) != 0) { 355 348 memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it 356 } else { 357 if (ptr->refCounter > 1) { 358 psError(__func__,"The buffer being freed is referenced elsewhere. " 359 "Buffer's reference count was decremented instead."); 360 ptr->refCounter--; 361 memProblemCallback(ptr, file, lineno); 362 } else { 363 364 // cut the memBlock out of the memBlock list 365 pthread_mutex_lock(&memBlockListMutex); 366 if (ptr->nextBlock != NULL) { 367 ptr->nextBlock->previousBlock = ptr->previousBlock; 368 } 369 if (ptr->previousBlock != NULL) { 370 ptr->previousBlock->nextBlock = ptr->nextBlock; 371 } 372 if (lastMemBlockAllocated == ptr) { 373 lastMemBlockAllocated = ptr->nextBlock; 374 } 375 376 pthread_mutex_unlock(&memBlockListMutex); 377 378 free(ptr); 379 } 380 } 349 } 350 351 psMemDecrRefCounter(vptr); // this handles the free, if required. 381 352 } 382 353 … … 397 368 for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock) 398 369 { 399 if ( ( iter->refCounter> 0) && (iter->id >= id0) ) {370 if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) { 400 371 nleak++; 401 372 … … 422 393 for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock) 423 394 { 424 if ( ( iter->refCounter> 0) && (iter->id >= id0) ) {395 if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) { 425 396 (*arr)[j++] = iter; 426 397 if (j == nleak) { // found them all … … 440 411 psReferenceCount psMemGetRefCounter(void *vptr) // return refCounter 441 412 { 413 psMemBlock *ptr; 414 unsigned int refCount; 415 416 if (vptr == NULL) 417 { 418 return 0; 419 } 420 421 ptr = ((psMemBlock *)vptr) - 1; 422 423 if (checkMemBlock(ptr, __func__) != 0) 424 { 425 memProblemCallback(ptr, __func__, __LINE__); 426 } 427 428 pthread_mutex_lock(&ptr->refCounterMutex); 429 refCount = ptr->refCounter; 430 pthread_mutex_unlock(&ptr->refCounterMutex); 431 432 return refCount; 433 } 434 435 void *psMemIncrRefCounter(void *vptr) // increment and return refCounter 436 { 437 psMemBlock *ptr; 438 439 if (vptr == NULL) 440 { 441 return vptr; 442 } 443 444 ptr = ((psMemBlock *)vptr) - 1; 445 446 if (checkMemBlock(ptr, __func__)) 447 { 448 memProblemCallback(ptr, __func__, __LINE__); 449 } 450 451 pthread_mutex_lock(&ptr->refCounterMutex); 452 ptr->refCounter++; 453 pthread_mutex_unlock(&ptr->refCounterMutex); 454 455 return vptr; 456 } 457 458 void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter 459 { 460 if (vptr == NULL) 461 { 462 return NULL; 463 } 464 442 465 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 443 466 444 if (checkMemBlock(ptr, __func__) != 0) 445 { 446 memProblemCallback(ptr, __func__, __LINE__); 447 } 448 449 return ptr->refCounter; 450 } 451 452 void *psMemIncrRefCounter(void *vptr) // increment and return refCounter 453 { 454 if (vptr == NULL) 455 { 456 return vptr; 457 } 458 459 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 460 461 if (checkMemBlock(ptr, __func__)) 462 { 463 memProblemCallback(ptr, __func__, __LINE__); 464 } 465 466 ptr->refCounter++; 467 468 pthread_mutex_lock(&ptr->refCounterMutex); 469 470 if (ptr->refCounter > 1) 471 { 472 /// XXX - Probably should have another mutex here. 473 ptr->refCounter--; // multiple references, just decrement the count. 474 pthread_mutex_unlock(&ptr->refCounterMutex); 475 476 } else 477 { 478 pthread_mutex_unlock(&ptr->refCounterMutex); 479 480 // Did the user ask to be informed about this deallocation? 481 if (ptr->id == p_psMemFreeID) { 482 p_psMemFreeID += memFreeCallback(ptr); 483 } 484 485 pthread_mutex_lock(&memBlockListMutex); 486 487 // cut the memBlock out of the memBlock list 488 if (ptr->nextBlock != NULL) { 489 ptr->nextBlock->previousBlock = ptr->previousBlock; 490 } 491 if (ptr->previousBlock != NULL) { 492 ptr->previousBlock->nextBlock = ptr->nextBlock; 493 } 494 if (lastMemBlockAllocated == ptr) { 495 lastMemBlockAllocated = ptr->nextBlock; 496 } 497 498 pthread_mutex_unlock(&memBlockListMutex); 499 500 free(ptr); 501 502 vptr = NULL; // since we freed it, make sure we return NULL. 503 } 467 504 468 505 return vptr; 469 506 } 470 507 471 void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter472 {473 if (vptr == NULL)474 {475 return vptr;476 }477 478 psMemBlock *ptr = ((psMemBlock *)vptr) - 1;479 480 if (checkMemBlock(ptr, __func__))481 {482 memProblemCallback(ptr, __func__, __LINE__);483 }484 485 ptr->refCounter--;486 487 return vptr;488 } -
trunk/psLib/src/sys/psMemory.h
r803 r830 12 12 * @author Robert Lupton, Princeton University 13 13 * 14 * @version $Revision: 1.1 4$ $Name: not supported by cvs2svn $15 * @date $Date: 2004-0 5-28 03:17:39$14 * @version $Revision: 1.15 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2004-06-02 20:03:22 $ 16 16 * 17 17 * @ingroup SystemGroup System Utilities … … 21 21 22 22 #include <stdio.h> // needed for FILE 23 23 #include <pthread.h> // we need a mutex to make this stuff thread safe. 24 24 25 25 /** @addtogroup MemoryManagement Memory Management Utilities … … 64 64 const char* file; ///< set from __FILE__ in e.g. p_psAlloc 65 65 const int lineno; ///< set from __LINE__ in e.g. p_psAlloc 66 // TODO: refCounter should be mutexed?66 pthread_mutex_t refCounterMutex; ///< mutex to ensure exclusive access to reference counter 67 67 psReferenceCount refCounter; ///< how many times pointer is referenced 68 68 const void* endblock; ///< initialised to p_psMEMMAGIC -
trunk/psLib/src/sysUtils/psMemory.c
r803 r830 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1. 19$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-0 5-28 03:17:39$10 * @version $Revision: 1.20 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-06-02 20:03:22 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 16 16 #define PS_ALLOW_MALLOC // we're allowed to call malloc() 17 17 18 #include <pthread.h> // we need a mutex to make this stuff thread safe.19 18 #include <stdlib.h> 20 19 #include <stdio.h> … … 25 24 #include "psError.h" 26 25 #include "psAbort.h" 26 #include "psLogMsg.h" 27 27 28 28 #define P_PS_MEMMAGIC (void *)0xdeadbeef // Magic number in psMemBlock header … … 67 67 psError(__func__, 68 68 "Block %ld allocated at %s:%d freed more than once at %s:%d\n", 69 ptr->id, ptr->file, ptr->lineno, file, lineno);70 } else if (ptr->refCounter > 1) {71 psError(__func__,72 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n",73 69 ptr->id, ptr->file, ptr->lineno, file, lineno); 74 70 } … … 268 264 ptr->startblock = P_PS_MEMMAGIC; 269 265 ptr->endblock = P_PS_MEMMAGIC; 270 ptr->refCounter = 1; // one user so far271 266 ptr->userMemorySize = size; 272 267 *(void**)((int8_t*)(ptr+1)+size) = P_PS_MEMMAGIC; 273 268 ptr->previousBlock = NULL; 269 270 pthread_mutex_init(&ptr->refCounterMutex, NULL); 271 ptr->refCounter = 1; // one user so far 274 272 275 273 // need exclusive access of the memory block list now... … … 347 345 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 348 346 349 // Did the user ask to be informed about this deallocation? 350 if (ptr->id == p_psMemFreeID) { 351 p_psMemFreeID += memFreeCallback(ptr); 352 } 353 354 if (checkMemBlock(ptr, "psFree") != 0) { 347 if (checkMemBlock(ptr, __func__) != 0) { 355 348 memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it 356 } else { 357 if (ptr->refCounter > 1) { 358 psError(__func__,"The buffer being freed is referenced elsewhere. " 359 "Buffer's reference count was decremented instead."); 360 ptr->refCounter--; 361 memProblemCallback(ptr, file, lineno); 362 } else { 363 364 // cut the memBlock out of the memBlock list 365 pthread_mutex_lock(&memBlockListMutex); 366 if (ptr->nextBlock != NULL) { 367 ptr->nextBlock->previousBlock = ptr->previousBlock; 368 } 369 if (ptr->previousBlock != NULL) { 370 ptr->previousBlock->nextBlock = ptr->nextBlock; 371 } 372 if (lastMemBlockAllocated == ptr) { 373 lastMemBlockAllocated = ptr->nextBlock; 374 } 375 376 pthread_mutex_unlock(&memBlockListMutex); 377 378 free(ptr); 379 } 380 } 349 } 350 351 psMemDecrRefCounter(vptr); // this handles the free, if required. 381 352 } 382 353 … … 397 368 for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock) 398 369 { 399 if ( ( iter->refCounter> 0) && (iter->id >= id0) ) {370 if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) { 400 371 nleak++; 401 372 … … 422 393 for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock) 423 394 { 424 if ( ( iter->refCounter> 0) && (iter->id >= id0) ) {395 if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) { 425 396 (*arr)[j++] = iter; 426 397 if (j == nleak) { // found them all … … 440 411 psReferenceCount psMemGetRefCounter(void *vptr) // return refCounter 441 412 { 413 psMemBlock *ptr; 414 unsigned int refCount; 415 416 if (vptr == NULL) 417 { 418 return 0; 419 } 420 421 ptr = ((psMemBlock *)vptr) - 1; 422 423 if (checkMemBlock(ptr, __func__) != 0) 424 { 425 memProblemCallback(ptr, __func__, __LINE__); 426 } 427 428 pthread_mutex_lock(&ptr->refCounterMutex); 429 refCount = ptr->refCounter; 430 pthread_mutex_unlock(&ptr->refCounterMutex); 431 432 return refCount; 433 } 434 435 void *psMemIncrRefCounter(void *vptr) // increment and return refCounter 436 { 437 psMemBlock *ptr; 438 439 if (vptr == NULL) 440 { 441 return vptr; 442 } 443 444 ptr = ((psMemBlock *)vptr) - 1; 445 446 if (checkMemBlock(ptr, __func__)) 447 { 448 memProblemCallback(ptr, __func__, __LINE__); 449 } 450 451 pthread_mutex_lock(&ptr->refCounterMutex); 452 ptr->refCounter++; 453 pthread_mutex_unlock(&ptr->refCounterMutex); 454 455 return vptr; 456 } 457 458 void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter 459 { 460 if (vptr == NULL) 461 { 462 return NULL; 463 } 464 442 465 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 443 466 444 if (checkMemBlock(ptr, __func__) != 0) 445 { 446 memProblemCallback(ptr, __func__, __LINE__); 447 } 448 449 return ptr->refCounter; 450 } 451 452 void *psMemIncrRefCounter(void *vptr) // increment and return refCounter 453 { 454 if (vptr == NULL) 455 { 456 return vptr; 457 } 458 459 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 460 461 if (checkMemBlock(ptr, __func__)) 462 { 463 memProblemCallback(ptr, __func__, __LINE__); 464 } 465 466 ptr->refCounter++; 467 468 pthread_mutex_lock(&ptr->refCounterMutex); 469 470 if (ptr->refCounter > 1) 471 { 472 /// XXX - Probably should have another mutex here. 473 ptr->refCounter--; // multiple references, just decrement the count. 474 pthread_mutex_unlock(&ptr->refCounterMutex); 475 476 } else 477 { 478 pthread_mutex_unlock(&ptr->refCounterMutex); 479 480 // Did the user ask to be informed about this deallocation? 481 if (ptr->id == p_psMemFreeID) { 482 p_psMemFreeID += memFreeCallback(ptr); 483 } 484 485 pthread_mutex_lock(&memBlockListMutex); 486 487 // cut the memBlock out of the memBlock list 488 if (ptr->nextBlock != NULL) { 489 ptr->nextBlock->previousBlock = ptr->previousBlock; 490 } 491 if (ptr->previousBlock != NULL) { 492 ptr->previousBlock->nextBlock = ptr->nextBlock; 493 } 494 if (lastMemBlockAllocated == ptr) { 495 lastMemBlockAllocated = ptr->nextBlock; 496 } 497 498 pthread_mutex_unlock(&memBlockListMutex); 499 500 free(ptr); 501 502 vptr = NULL; // since we freed it, make sure we return NULL. 503 } 467 504 468 505 return vptr; 469 506 } 470 507 471 void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter472 {473 if (vptr == NULL)474 {475 return vptr;476 }477 478 psMemBlock *ptr = ((psMemBlock *)vptr) - 1;479 480 if (checkMemBlock(ptr, __func__))481 {482 memProblemCallback(ptr, __func__, __LINE__);483 }484 485 ptr->refCounter--;486 487 return vptr;488 } -
trunk/psLib/src/sysUtils/psMemory.h
r803 r830 12 12 * @author Robert Lupton, Princeton University 13 13 * 14 * @version $Revision: 1.1 4$ $Name: not supported by cvs2svn $15 * @date $Date: 2004-0 5-28 03:17:39$14 * @version $Revision: 1.15 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2004-06-02 20:03:22 $ 16 16 * 17 17 * @ingroup SystemGroup System Utilities … … 21 21 22 22 #include <stdio.h> // needed for FILE 23 23 #include <pthread.h> // we need a mutex to make this stuff thread safe. 24 24 25 25 /** @addtogroup MemoryManagement Memory Management Utilities … … 64 64 const char* file; ///< set from __FILE__ in e.g. p_psAlloc 65 65 const int lineno; ///< set from __LINE__ in e.g. p_psAlloc 66 // TODO: refCounter should be mutexed?66 pthread_mutex_t refCounterMutex; ///< mutex to ensure exclusive access to reference counter 67 67 psReferenceCount refCounter; ///< how many times pointer is referenced 68 68 const void* endblock; ///< initialised to p_psMEMMAGIC
Note:
See TracChangeset
for help on using the changeset viewer.
