Changeset 2204 for trunk/psLib/src/sysUtils/psMemory.c
- Timestamp:
- Oct 26, 2004, 2:57:34 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sysUtils/psMemory.c (modified) (28 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sysUtils/psMemory.c
r1840 r2204 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1.4 1$ $Name: not supported by cvs2svn $11 * @date $Date: 2004- 09-21 22:30:19$10 * @version $Revision: 1.42 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-10-27 00:57:31 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 18 18 #include <stdlib.h> 19 19 #include <stdio.h> 20 #include <stdbool.h>21 20 #include <stdint.h> 22 21 … … 28 27 #include "psSysUtilsErrors.h" 29 28 30 #define P_PS_MEMMAGIC ( void *)0xdeadbeef // Magic number in psMemBlock header29 #define P_PS_MEMMAGIC (psPtr )0xdeadbeef // Magic number in psMemBlock header 31 30 32 31 #define P_PS_LARGE_BLOCK_SIZE 65536 // size where under, we try to recycle 33 32 34 static intcheckMemBlock(const psMemBlock* m, const char *funcName);33 static psS32 checkMemBlock(const psMemBlock* m, const char *funcName); 35 34 static psMemBlock* lastMemBlockAllocated = NULL; 36 35 static pthread_mutex_t memBlockListMutex = PTHREAD_MUTEX_INITIALIZER; … … 39 38 static pthread_mutex_t recycleMemBlockListMutex = PTHREAD_MUTEX_INITIALIZER; 40 39 41 static intrecycleBins = 13;42 static intrecycleBinSize[14] = {43 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, P_PS_LARGE_BLOCK_SIZE44 };40 static psS32 recycleBins = 13; 41 static psS32 recycleBinSize[14] = { 42 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, P_PS_LARGE_BLOCK_SIZE 43 }; 45 44 46 45 // N.B. recycleBinSize should be terminated by P_PS_LARGE_BLOCK_SIZE (simplifies search loops) … … 61 60 * Default memExhausted callback. 62 61 */ 63 static void *memExhaustedCallbackDefault(size_t size)64 { 65 void *ptr = NULL;62 static psPtr memExhaustedCallbackDefault(size_t size) 63 { 64 psPtr ptr = NULL; 66 65 67 66 pthread_mutex_lock(&recycleMemBlockListMutex); 68 intlevel = recycleBins - 1;67 psS32 level = recycleBins - 1; 69 68 70 69 while (level >= 0 && ptr == NULL) { … … 98 97 } 99 98 100 static void memProblemCallbackDefault(const psMemBlock* ptr, const char *file, intlineno)99 static void memProblemCallbackDefault(const psMemBlock* ptr, const char *file, psS32 lineno) 101 100 { 102 101 if (ptr->refCounter < 1) { … … 223 222 */ 224 223 225 static intcheckMemBlock(const psMemBlock* m, const char *funcName)224 static psS32 checkMemBlock(const psMemBlock* m, const char *funcName) 226 225 { 227 226 // n.b. since this is called by psMemCheckCorruption while the memblock list is mutex locked, … … 248 247 return 1; 249 248 } 250 if (*( void **)((int8_t *) (m + 1) + m->userMemorySize) != P_PS_MEMMAGIC) {249 if (*(psPtr *)((int8_t *) (m + 1) + m->userMemorySize) != P_PS_MEMMAGIC) { 251 250 psErrorMsg(PS_ERRORNAME_DOMAIN "checkMemBlock", true, PS_ERR_MEMORY_CORRUPTION, 252 251 PS_ERRORTEXT_psMemory_OVERFLOW, … … 258 257 } 259 258 260 int psMemCheckCorruption(bool abort_on_error)261 { 262 intnbad = 0; // number of bad blocks263 bool failure = false;259 psS32 psMemCheckCorruption(psBool abort_on_error) 260 { 261 psS32 nbad = 0; // number of bad blocks 262 psBool failure = false; 264 263 265 264 // get exclusive access to the memBlock list to avoid it changing on us while we use it. … … 289 288 } 290 289 291 void *p_psAlloc(size_t size, const char *file, intlineno)290 psPtr p_psAlloc(size_t size, const char *file, psS32 lineno) 292 291 { 293 292 … … 297 296 if (size < P_PS_LARGE_BLOCK_SIZE) { 298 297 // find the bin we need. 299 intlevel = 0;298 psS32 level = 0; 300 299 301 300 while (size > recycleBinSize[level]) { … … 323 322 324 323 if (ptr == NULL) { 325 ptr = malloc(sizeof(psMemBlock) + size + sizeof( void *));324 ptr = malloc(sizeof(psMemBlock) + size + sizeof(psPtr )); 326 325 327 326 if (ptr == NULL) { … … 332 331 } 333 332 334 ptr->startblock = P_PS_MEMMAGIC;335 ptr->endblock = P_PS_MEMMAGIC;333 *(psPtr*)&ptr->startblock = P_PS_MEMMAGIC; 334 *(psPtr*)&ptr->endblock = P_PS_MEMMAGIC; 336 335 ptr->userMemorySize = size; 337 336 pthread_mutex_init(&ptr->refCounterMutex, NULL); … … 345 344 ptr->freeFcn = NULL; 346 345 ptr->persistent = false; 347 *( unsigned int*)&ptr->lineno = lineno;348 *( void **)((int8_t *) (ptr + 1) + size) = P_PS_MEMMAGIC;346 *(psU32 *)&ptr->lineno = lineno; 347 *(psPtr *)((int8_t *) (ptr + 1) + size) = P_PS_MEMMAGIC; 349 348 ptr->previousBlock = NULL; 350 349 … … 371 370 } 372 371 373 void *p_psRealloc(void *vptr, size_t size, const char *file, intlineno)372 psPtr p_psRealloc(psPtr vptr, size_t size, const char *file, psS32 lineno) 374 373 { 375 374 if (vptr == NULL) { … … 377 376 } else { 378 377 psMemBlock* ptr = ((psMemBlock* ) vptr) - 1; 379 bool isBlockLast = false;378 psBool isBlockLast = false; 380 379 381 380 if (checkMemBlock(ptr, __func__) != 0) { … … 389 388 isBlockLast = (ptr == lastMemBlockAllocated); 390 389 391 ptr = (psMemBlock* ) realloc(ptr, sizeof(psMemBlock) + size + sizeof( void *));390 ptr = (psMemBlock* ) realloc(ptr, sizeof(psMemBlock) + size + sizeof(psPtr )); 392 391 393 392 if (ptr == NULL) { … … 399 398 400 399 ptr->userMemorySize = size; 401 *( void **)((int8_t *) (ptr + 1) + size) = P_PS_MEMMAGIC;400 *(psPtr *)((int8_t *) (ptr + 1) + size) = P_PS_MEMMAGIC; 402 401 403 402 if (isBlockLast) { … … 423 422 } 424 423 425 void p_psFree( void *vptr, const char *file, intlineno)424 void p_psFree(psPtr vptr, const char *file, psS32 lineno) 426 425 { 427 426 if (vptr == NULL) { … … 444 443 * Check for memory leaks. 445 444 */ 446 intpsMemCheckLeaks(psMemoryId id0, psMemBlock* ** arr, FILE * fd)447 { 448 intnleak = 0;449 intj = 0;445 psS32 psMemCheckLeaks(psMemoryId id0, psMemBlock* ** arr, FILE * fd) 446 { 447 psS32 nleak = 0; 448 psS32 j = 0; 450 449 psMemBlock* topBlock = lastMemBlockAllocated; 451 450 … … 499 498 */ 500 499 // return refCounter 501 psReferenceCount psMemGetRefCounter( void *vptr)500 psReferenceCount psMemGetRefCounter(psPtr vptr) 502 501 { 503 502 psMemBlock* ptr; 504 unsigned intrefCount;503 psU32 refCount; 505 504 506 505 if (vptr == NULL) { … … 522 521 523 522 // increment and return refCounter 524 void *p_psMemIncrRefCounter(void *vptr, const char *file, intlineno)523 psPtr p_psMemIncrRefCounter(psPtr vptr, const char *file, psS32 lineno) 525 524 { 526 525 psMemBlock* ptr; … … 544 543 545 544 // decrement and return refCounter 546 void *p_psMemDecrRefCounter(void *vptr, const char *file, intlineno)545 psPtr p_psMemDecrRefCounter(psPtr vptr, const char *file, psS32 lineno) 547 546 { 548 547 if (vptr == NULL) { … … 593 592 if (ptr->userMemorySize < P_PS_LARGE_BLOCK_SIZE) { 594 593 595 intlevel = 1;594 psS32 level = 1; 596 595 597 596 while (ptr->userMemorySize >= recycleBinSize[level]) { … … 635 634 } 636 635 637 void p_psMemSetDeallocator( void *vptr, psFreeFcn freeFcn)636 void p_psMemSetDeallocator(psPtr vptr, psFreeFcn freeFcn) 638 637 { 639 638 if (vptr == NULL) { … … 650 649 651 650 } 652 psFreeFcn p_psMemGetDeallocator( void *vptr)651 psFreeFcn p_psMemGetDeallocator(psPtr vptr) 653 652 { 654 653 if (vptr == NULL) { … … 665 664 } 666 665 667 bool p_psMemGetPersistent(void *vptr)666 psBool p_psMemGetPersistent(psPtr vptr) 668 667 { 669 668 if (vptr == NULL) { … … 680 679 } 681 680 682 void p_psMemSetPersistent( void *vptr,bool value)681 void p_psMemSetPersistent(psPtr vptr,psBool value) 683 682 { 684 683 if (vptr == NULL) {
Note:
See TracChangeset
for help on using the changeset viewer.
