Changeset 1240
- Timestamp:
- Jul 19, 2004, 10:45:53 AM (22 years ago)
- Location:
- trunk/psLib
- Files:
-
- 5 edited
-
psLib.kdevses (modified) (1 diff)
-
src/astronomy/psCCD.d (modified) (1 diff)
-
src/sys/psMemory.c (modified) (8 diffs)
-
src/sysUtils/psMemory.c (modified) (8 diffs)
-
test/sysUtils/verified/tst_psMemory.stderr (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/psLib.kdevses
r1219 r1240 2 2 <!DOCTYPE KDevPrjSession> 3 3 <KDevPrjSession> 4 <DocsAndViews NumberOfDocuments=" 3" >5 <Doc0 NumberOfViews="1" URL="file:/home/desonia/psLib/src/ dataManip/tst_psImageManip.c" >6 <View0 line=" 13" Type="???" >7 <AdditionalSettings Top="2" Width="1 196" Attach="1" Height="684" Left="2" MinMaxMode="0" />4 <DocsAndViews NumberOfDocuments="5" > 5 <Doc0 NumberOfViews="1" URL="file:/home/desonia/psLib/src/astronomy/psCCD.h" > 6 <View0 line="37" Type="???" > 7 <AdditionalSettings Top="2" Width="1058" Attach="1" Height="629" Left="2" MinMaxMode="0" /> 8 8 </View0> 9 9 </Doc0> 10 <Doc1 NumberOfViews="1" URL="file:/home/desonia/psLib/ test/dataManip/Makefile" >11 <View0 line=" 0" Type="???" >12 <AdditionalSettings Top="2" Width="1 196" Attach="1" Height="659" Left="2" MinMaxMode="0" />10 <Doc1 NumberOfViews="1" URL="file:/home/desonia/psLib/src/collections/psImage.h" > 11 <View0 line="61" Type="???" > 12 <AdditionalSettings Top="2" Width="1058" Attach="1" Height="629" Left="2" MinMaxMode="0" /> 13 13 </View0> 14 14 </Doc1> 15 <Doc2 NumberOfViews="1" URL="file:/home/desonia/psLib/ test/dataManip/tst_psImageManip.c" >16 <View0 line=" 15" Type="???" >17 <AdditionalSettings Top="2" Width="1 196" Attach="1" Height="659" Left="2" MinMaxMode="0" />15 <Doc2 NumberOfViews="1" URL="file:/home/desonia/psLib/src/astronomy/psCCD.c" > 16 <View0 line="43" Type="???" > 17 <AdditionalSettings Top="2" Width="1058" Attach="1" Height="629" Left="2" MinMaxMode="0" /> 18 18 </View0> 19 19 </Doc2> 20 <Doc3 NumberOfViews="1" URL="file:/home/desonia/psLib/src/astronomy/Makefile" > 21 <View0 line="15" Type="???" > 22 <AdditionalSettings Top="2" Width="1058" Attach="1" Height="629" Left="2" MinMaxMode="0" /> 23 </View0> 24 </Doc3> 25 <Doc4 NumberOfViews="1" URL="file:/home/desonia/psLib/src/collections/Makefile" > 26 <View0 line="0" Type="???" > 27 <AdditionalSettings Top="2" Width="1058" Attach="1" Height="629" Left="2" MinMaxMode="0" /> 28 </View0> 29 </Doc4> 20 30 </DocsAndViews> 21 31 <pluginList> 22 32 <kdevbookmarks> 23 <bookmarks/> 33 <bookmarks> 34 <bookmark url="/home/desonia/psLib/test/dataManip/tst_psImageManip.c" > 35 <mark line="578" /> 36 <mark line="598" /> 37 <mark line="632" /> 38 </bookmark> 39 </bookmarks> 24 40 </kdevbookmarks> 25 41 <kdevsubversion> -
trunk/psLib/src/astronomy/psCCD.d
r1237 r1240 1 1 psCCD.o psCCD.d : psCCD.c psCCD.h ../collections/psType.h ../collections/psImage.h \ 2 ../collections/ps List.h ../collections/psCompare.h \3 ../collections/ps Array.h ../sysUtils/psMemory.h2 ../collections/psArray.h ../collections/psCompare.h \ 3 ../collections/psList.h ../sysUtils/psMemory.h -
trunk/psLib/src/sys/psMemory.c
r1233 r1240 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1.2 8$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-07-1 5 23:52:34$10 * @version $Revision: 1.29 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-07-19 20:45:53 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 28 28 #define P_PS_MEMMAGIC (void *)0xdeadbeef // Magic number in psMemBlock header 29 29 30 #define P_PS_LARGE_BLOCK_SIZE 65536 // size where under, we try to recycle 31 30 32 static int checkMemBlock(const psMemBlock *m, const char* funcName); 31 33 static psMemBlock *lastMemBlockAllocated = NULL; … … 33 35 static pthread_mutex_t memIdMutex = PTHREAD_MUTEX_INITIALIZER; 34 36 37 static pthread_mutex_t recycleMemBlockListMutex = PTHREAD_MUTEX_INITIALIZER; 38 39 static int recycleBins = 13; 40 static int recycleBinSize[14] = {8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,P_PS_LARGE_BLOCK_SIZE}; 41 // N.B. recycleBinSize should be terminated by P_PS_LARGE_BLOCK_SIZE (simplifies search loops) 42 static psMemBlock* recycleMemBlockList[13] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}; 43 44 #ifdef PS_MEM_DEBUG 45 static psMemBlock* deadBlockList; // a place to put dead memBlocks in debug mode. 46 #endif 35 47 /** 36 48 * Unique ID for allocated blocks … … 43 55 static void *memExhaustedCallbackDefault(size_t size) 44 56 { 45 return NULL; 57 void* ptr = NULL; 58 59 pthread_mutex_lock(&recycleMemBlockListMutex); 60 int level=recycleBins-1; 61 while (level >= 0 && ptr == NULL) { 62 while (recycleMemBlockList[level] != NULL && ptr == NULL) { 63 psMemBlock* old = recycleMemBlockList[level]; 64 recycleMemBlockList[level] = recycleMemBlockList[level]->nextBlock; 65 free(old); 66 ptr = malloc(size); 67 } 68 level--; 69 } 70 pthread_mutex_unlock(&recycleMemBlockListMutex); 71 72 return ptr; 46 73 } 47 74 … … 243 270 void *p_psAlloc(size_t size, const char *file, int lineno) 244 271 { 245 psMemBlock *ptr = malloc(sizeof(psMemBlock) + size + sizeof(void*)); 272 273 psMemBlock *ptr = NULL; 274 275 // memory is of the size I want to bother recycling? 276 if (size < P_PS_LARGE_BLOCK_SIZE) { 277 // find the bin we need. 278 int level = 0; 279 while (size > recycleBinSize[level]) { 280 level++; 281 } 282 // Are we in one of the bins 283 if (level<recycleBins) { 284 285 size = recycleBinSize[level]; // round-up size to next sized bin. 286 287 pthread_mutex_lock(&recycleMemBlockListMutex); 288 289 if (recycleMemBlockList[level] != NULL) { 290 ptr = recycleMemBlockList[level]; 291 recycleMemBlockList[level] = ptr->nextBlock; 292 if (recycleMemBlockList[level] != NULL) { 293 recycleMemBlockList[level]->previousBlock = NULL; 294 } 295 size = ptr->userMemorySize; 296 } 297 298 pthread_mutex_unlock(&recycleMemBlockListMutex); 299 } 300 } 246 301 247 302 if (ptr == NULL) { 248 ptr = memExhaustedCallback(size); 303 ptr = malloc(sizeof(psMemBlock) + size + sizeof(void*)); 304 249 305 if (ptr == NULL) { 250 psAbort(__func__, "Failed to allocate %u bytes at %s:%d", 251 size, file, lineno); 252 } 306 ptr = memExhaustedCallback(size); 307 if (ptr == NULL) { 308 psAbort(__func__, "Failed to allocate %u bytes at %s:%d", 309 size, file, lineno); 310 } 311 } 312 313 ptr->startblock = P_PS_MEMMAGIC; 314 ptr->endblock = P_PS_MEMMAGIC; 315 ptr->userMemorySize = size; 316 pthread_mutex_init(&ptr->refCounterMutex, NULL); 253 317 } 254 318 … … 261 325 ptr->freeFcn = NULL; 262 326 *(unsigned int*)&ptr->lineno = lineno; 263 ptr->startblock = P_PS_MEMMAGIC;264 ptr->endblock = P_PS_MEMMAGIC;265 ptr->userMemorySize = size;266 327 *(void**)((int8_t*)(ptr+1)+size) = P_PS_MEMMAGIC; 267 328 ptr->previousBlock = NULL; 268 329 269 pthread_mutex_init(&ptr->refCounterMutex, NULL);270 330 ptr->refCounter = 1; // one user so far 271 331 … … 475 535 } 476 536 477 #ifdef PS_MEM_DEBUG478 // for debug mode, we should keep the memBlock around and mark as not referenced.479 ptr->refCounter = 0;480 (void)p_psRealloc(vptr,0,file,lineno);481 #else482 483 537 pthread_mutex_lock(&memBlockListMutex); 484 538 … … 496 550 pthread_mutex_unlock(&memBlockListMutex); 497 551 552 553 // do we need to recycle? 554 if (ptr->userMemorySize < P_PS_LARGE_BLOCK_SIZE) { 555 556 int level = 1; 557 while (ptr->userMemorySize >= recycleBinSize[level]) { 558 level++; 559 } 560 level--; 561 562 ptr->refCounter = 0; 563 ptr->previousBlock = NULL; 564 565 pthread_mutex_lock(&recycleMemBlockListMutex); 566 ptr->nextBlock = recycleMemBlockList[level]; 567 if (recycleMemBlockList[level] != NULL) { 568 recycleMemBlockList[level]->previousBlock = ptr; 569 } 570 recycleMemBlockList[level] = ptr; 571 pthread_mutex_unlock(&recycleMemBlockListMutex); 572 573 } else { 574 // memory is larger than I want to recycle. 575 #ifdef PS_MEM_DEBUG 576 (void)p_psRealloc(vptr,0,file,lineno); 577 ptr->previousBlock = NULL; 578 ptr->nextBlock = deadBlockList; 579 if (deadBlockList != NULL) { 580 deadBlockList->previous = ptr; 581 } 582 deadBlockList = ptr; 583 #else 584 585 pthread_mutex_destroy(&ptr->refCounterMutex); 586 free(ptr); 587 #endif 588 589 } 590 591 498 592 pthread_mutex_destroy(&ptr->refCounterMutex); 499 593 500 free(ptr);501 #endif502 594 503 595 vptr = NULL; // since we freed it, make sure we return NULL. -
trunk/psLib/src/sysUtils/psMemory.c
r1233 r1240 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1.2 8$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-07-1 5 23:52:34$10 * @version $Revision: 1.29 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-07-19 20:45:53 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 28 28 #define P_PS_MEMMAGIC (void *)0xdeadbeef // Magic number in psMemBlock header 29 29 30 #define P_PS_LARGE_BLOCK_SIZE 65536 // size where under, we try to recycle 31 30 32 static int checkMemBlock(const psMemBlock *m, const char* funcName); 31 33 static psMemBlock *lastMemBlockAllocated = NULL; … … 33 35 static pthread_mutex_t memIdMutex = PTHREAD_MUTEX_INITIALIZER; 34 36 37 static pthread_mutex_t recycleMemBlockListMutex = PTHREAD_MUTEX_INITIALIZER; 38 39 static int recycleBins = 13; 40 static int recycleBinSize[14] = {8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,P_PS_LARGE_BLOCK_SIZE}; 41 // N.B. recycleBinSize should be terminated by P_PS_LARGE_BLOCK_SIZE (simplifies search loops) 42 static psMemBlock* recycleMemBlockList[13] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}; 43 44 #ifdef PS_MEM_DEBUG 45 static psMemBlock* deadBlockList; // a place to put dead memBlocks in debug mode. 46 #endif 35 47 /** 36 48 * Unique ID for allocated blocks … … 43 55 static void *memExhaustedCallbackDefault(size_t size) 44 56 { 45 return NULL; 57 void* ptr = NULL; 58 59 pthread_mutex_lock(&recycleMemBlockListMutex); 60 int level=recycleBins-1; 61 while (level >= 0 && ptr == NULL) { 62 while (recycleMemBlockList[level] != NULL && ptr == NULL) { 63 psMemBlock* old = recycleMemBlockList[level]; 64 recycleMemBlockList[level] = recycleMemBlockList[level]->nextBlock; 65 free(old); 66 ptr = malloc(size); 67 } 68 level--; 69 } 70 pthread_mutex_unlock(&recycleMemBlockListMutex); 71 72 return ptr; 46 73 } 47 74 … … 243 270 void *p_psAlloc(size_t size, const char *file, int lineno) 244 271 { 245 psMemBlock *ptr = malloc(sizeof(psMemBlock) + size + sizeof(void*)); 272 273 psMemBlock *ptr = NULL; 274 275 // memory is of the size I want to bother recycling? 276 if (size < P_PS_LARGE_BLOCK_SIZE) { 277 // find the bin we need. 278 int level = 0; 279 while (size > recycleBinSize[level]) { 280 level++; 281 } 282 // Are we in one of the bins 283 if (level<recycleBins) { 284 285 size = recycleBinSize[level]; // round-up size to next sized bin. 286 287 pthread_mutex_lock(&recycleMemBlockListMutex); 288 289 if (recycleMemBlockList[level] != NULL) { 290 ptr = recycleMemBlockList[level]; 291 recycleMemBlockList[level] = ptr->nextBlock; 292 if (recycleMemBlockList[level] != NULL) { 293 recycleMemBlockList[level]->previousBlock = NULL; 294 } 295 size = ptr->userMemorySize; 296 } 297 298 pthread_mutex_unlock(&recycleMemBlockListMutex); 299 } 300 } 246 301 247 302 if (ptr == NULL) { 248 ptr = memExhaustedCallback(size); 303 ptr = malloc(sizeof(psMemBlock) + size + sizeof(void*)); 304 249 305 if (ptr == NULL) { 250 psAbort(__func__, "Failed to allocate %u bytes at %s:%d", 251 size, file, lineno); 252 } 306 ptr = memExhaustedCallback(size); 307 if (ptr == NULL) { 308 psAbort(__func__, "Failed to allocate %u bytes at %s:%d", 309 size, file, lineno); 310 } 311 } 312 313 ptr->startblock = P_PS_MEMMAGIC; 314 ptr->endblock = P_PS_MEMMAGIC; 315 ptr->userMemorySize = size; 316 pthread_mutex_init(&ptr->refCounterMutex, NULL); 253 317 } 254 318 … … 261 325 ptr->freeFcn = NULL; 262 326 *(unsigned int*)&ptr->lineno = lineno; 263 ptr->startblock = P_PS_MEMMAGIC;264 ptr->endblock = P_PS_MEMMAGIC;265 ptr->userMemorySize = size;266 327 *(void**)((int8_t*)(ptr+1)+size) = P_PS_MEMMAGIC; 267 328 ptr->previousBlock = NULL; 268 329 269 pthread_mutex_init(&ptr->refCounterMutex, NULL);270 330 ptr->refCounter = 1; // one user so far 271 331 … … 475 535 } 476 536 477 #ifdef PS_MEM_DEBUG478 // for debug mode, we should keep the memBlock around and mark as not referenced.479 ptr->refCounter = 0;480 (void)p_psRealloc(vptr,0,file,lineno);481 #else482 483 537 pthread_mutex_lock(&memBlockListMutex); 484 538 … … 496 550 pthread_mutex_unlock(&memBlockListMutex); 497 551 552 553 // do we need to recycle? 554 if (ptr->userMemorySize < P_PS_LARGE_BLOCK_SIZE) { 555 556 int level = 1; 557 while (ptr->userMemorySize >= recycleBinSize[level]) { 558 level++; 559 } 560 level--; 561 562 ptr->refCounter = 0; 563 ptr->previousBlock = NULL; 564 565 pthread_mutex_lock(&recycleMemBlockListMutex); 566 ptr->nextBlock = recycleMemBlockList[level]; 567 if (recycleMemBlockList[level] != NULL) { 568 recycleMemBlockList[level]->previousBlock = ptr; 569 } 570 recycleMemBlockList[level] = ptr; 571 pthread_mutex_unlock(&recycleMemBlockListMutex); 572 573 } else { 574 // memory is larger than I want to recycle. 575 #ifdef PS_MEM_DEBUG 576 (void)p_psRealloc(vptr,0,file,lineno); 577 ptr->previousBlock = NULL; 578 ptr->nextBlock = deadBlockList; 579 if (deadBlockList != NULL) { 580 deadBlockList->previous = ptr; 581 } 582 deadBlockList = ptr; 583 #else 584 585 pthread_mutex_destroy(&ptr->refCounterMutex); 586 free(ptr); 587 #endif 588 589 } 590 591 498 592 pthread_mutex_destroy(&ptr->refCounterMutex); 499 593 500 free(ptr);501 #endif502 594 503 595 vptr = NULL; // since we freed it, make sure we return NULL. -
trunk/psLib/test/sysUtils/verified/tst_psMemory.stderr
r1204 r1240 92 92 <DATE> <TIME> |<HOST>|I|TPmemCorruption|psMemCheckCorruption should output an error message and memProblemCallback callback should be called. 93 93 <DATE> <TIME> |<HOST>|E|psMemCheckCorru|Memory Corruption: memory block 1 is corrupted (buffer underflow) 94 <DATE> <TIME> |<HOST>|I|memProblemCallb|memory callback called for id 1 (psMemCheckCorruption:2 27).94 <DATE> <TIME> |<HOST>|I|memProblemCallb|memory callback called for id 1 (psMemCheckCorruption:254). 95 95 96 96 ---> TESTPOINT PASSED (psMemory{psMemCorruption} | tst_psMemory.c)
Note:
See TracChangeset
for help on using the changeset viewer.
