Changeset 8808
- Timestamp:
- Sep 13, 2006, 11:11:11 AM (20 years ago)
- Location:
- trunk/psLib/src
- Files:
-
- 2 edited
-
sys/psMemory.c (modified) (14 diffs)
-
types/psArguments.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psMemory.c
r8705 r8808 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1.8 2$ $Name: not supported by cvs2svn $11 * @date $Date: 2006-0 8-30 04:40:56$10 * @version $Revision: 1.83 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2006-09-13 21:11:11 $ 12 12 * 13 13 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 38 38 #define P_PS_MEMMAGIC (psPtr )0xdeadbeef // Magic number in psMemBlock header 39 39 40 #define P_PS_LARGE_BLOCK_SIZE 65536 // size where under, we try to recycle41 40 42 41 static psS32 checkMemBlock(const psMemBlock* m, const char *funcName); … … 51 50 static bool memory_is_persistent = false; 52 51 53 #ifdef PS_MEM_USE_RECYCLE // Only use recycling if this is set54 #define N_RECYCLE_BINS 14 // number of recycle bins55 #define MAX_RECYCLE 100 // Maximum number permitted in a recycle bin56 static pthread_mutex_t recycleMemBlockListMutex = PTHREAD_MUTEX_INITIALIZER; // Mutex for recycle bins57 static const psS32 recycleBinSize[N_RECYCLE_BINS] = // Size of each bin58 {59 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, P_PS_LARGE_BLOCK_SIZE60 };61 // N.B. recycleBinSize should be terminated by P_PS_LARGE_BLOCK_SIZE (simplifies search loops)62 static psS32 recycleBinNums[N_RECYCLE_BINS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Number in each bin63 static psMemBlock* recycleMemBlockList[N_RECYCLE_BINS] = // Contents of the bins64 { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };65 #endif // #ifdef PS_MEM_USE_RECYCLE66 67 52 #ifdef PS_MEM_DEBUG 68 53 static psMemBlock* deadBlockList; // a place to put dead memBlocks in debug mode. … … 73 58 */ 74 59 static psMemId memid = 0; 75 76 /**77 * Default memExhausted callback.78 */79 static psPtr memExhaustedCallbackDefault(size_t size)80 {81 #if PS_MEM_USE_RECYCLE82 psPtr ptr = NULL;83 if (safeThreads) {84 pthread_mutex_lock(&recycleMemBlockListMutex);85 }86 // Attempt to free up everything I can find so I can alloc my ptr87 int bin = N_RECYCLE_BINS - 1; // Recycle bin88 89 while (bin >= 0 && ptr == NULL) {90 while (recycleMemBlockList[bin] != NULL && ptr == NULL) {91 psMemBlock *old = recycleMemBlockList[bin];92 recycleMemBlockList[bin] = recycleMemBlockList[bin]->nextBlock;93 free(old);94 ptr = malloc(size);95 }96 bin--;97 }98 99 if (safeThreads) {100 pthread_mutex_unlock(&recycleMemBlockListMutex);101 }102 return ptr;103 #else // #ifdef PS_MEM_USE_RECYCLE104 105 return NULL;106 #endif // #ifdef PS_MEM_USE_RECYCLE107 }108 60 109 61 /* … … 150 102 const char *funcName) 151 103 { 152 // n.b. since this is called by psMemCheckCorruption while the memblock list is mutex locked, 153 // we shouldn't call such things as p_psAlloc/p_psFree here. 104 // n.b. since this is called by psMemCheckCorruption while the memblock 105 // list is mutex locked, we shouldn't call such things as 106 // p_psAlloc/p_psFree here. 154 107 155 108 if (m == NULL) { … … 189 142 static psMemFreeCallback memFreeCallback = memFreeCallbackDefault; 190 143 static psMemProblemCallback memProblemCallback = memProblemCallbackDefault; 191 static psMemExhaustedCallback memExhaustedCallback = memExhaustedCallbackDefault;192 193 psMemExhaustedCallback psMemExhaustedCallbackSet(psMemExhaustedCallback func)194 {195 psMemExhaustedCallback old = memExhaustedCallback;196 197 if (func != NULL) {198 memExhaustedCallback = func;199 } else {200 memExhaustedCallback = memExhaustedCallbackDefault;201 }202 203 return old;204 }205 144 206 145 psMemProblemCallback psMemProblemCallbackSet(psMemProblemCallback func) … … 277 216 278 217 if (safeThreads) { 218 // LOCK: id 279 219 pthread_mutex_lock(&memIdMutex); 280 220 } … … 283 223 284 224 if (safeThreads) { 225 // UNLOCK: id 285 226 pthread_mutex_unlock(&memIdMutex); 286 227 } … … 291 232 psMemId psMemGetLastId(void) 292 233 { 293 return memid; 234 psMemId id; 235 236 if (safeThreads) { 237 // LOCK: id 238 pthread_mutex_lock(&memIdMutex); 239 } 240 241 id = memid; 242 243 if (safeThreads) { 244 // UNLOCK: id 245 pthread_mutex_unlock(&memIdMutex); 246 } 247 248 return id; 294 249 } 295 250 … … 297 252 { 298 253 psS32 nbad = 0; // number of bad blocks 299 psBool failure = false; 300 301 // get exclusive access to the memBlock list to avoid it changing on us while we use it. 302 // pthread_mutex_lock(&memBlockListMutex); 254 255 // get exclusive access to the memBlock list to avoid it changing on us 256 // while we use it. 257 if (safeThreads) { 258 // LOCK: block list 259 pthread_mutex_unlock(&memBlockListMutex); 260 } 303 261 304 262 for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter = iter->nextBlock) { 305 if (safeThreads) { 306 pthread_mutex_unlock(&memBlockListMutex); 307 } 308 failure = checkMemBlock(iter, __func__); 309 if (safeThreads) { 310 pthread_mutex_lock(&memBlockListMutex); 311 } 312 if ( failure ) { 263 if (checkMemBlock(iter, __func__)) { 313 264 nbad++; 314 265 … … 316 267 317 268 if (abort_on_error) { 318 // release the lock on the memblock list319 269 if (safeThreads) { 270 // UNLOCK: block list 320 271 pthread_mutex_unlock(&memBlockListMutex); 321 272 } … … 326 277 } 327 278 328 // release the lock on the memblock list329 if (safeThreads) {279 if (safeThreads) { 280 // UNLOCK: block list 330 281 pthread_mutex_unlock(&memBlockListMutex); 331 282 } 283 332 284 return nbad; 333 285 } … … 427 379 psMemBlock *ptr = NULL; 428 380 429 #ifdef PS_MEM_USE_RECYCLE430 // Are we in one of the recycle bins?431 int bin = getRecycleBin(size);432 if (bin < N_RECYCLE_BINS) {433 size = recycleBinSize[bin]; // round-up size to next sized bin.434 ptr = recyclePop(bin); // grab out of the recycle bin435 }436 #endif // #ifdef PS_MEM_USE_RECYCLE437 438 381 if (ptr == NULL) { 439 382 ptr = malloc(sizeof(psMemBlock) + size + sizeof(psPtr )); 440 383 441 384 if (ptr == NULL) { 442 ptr = memExhaustedCallback(size); 443 if (ptr == NULL) { 444 psAbort(__func__, "Failed to allocate %zd bytes at %s:%d", size, file, lineno); 445 } 385 psAbort(__func__, "Failed to allocate %zd bytes at %s:%d", size, file, lineno); 446 386 } 447 387 … … 533 473 ptr = (psMemBlock *)realloc(ptr, sizeof(psMemBlock) + size + sizeof(psPtr)); 534 474 if (ptr == NULL) { 535 ptr = memExhaustedCallback(size); 536 if (ptr == NULL) { 537 psAbort(__func__, "Failed to reallocate %zd bytes at %s:%d", size, file, lineno); 538 } 475 psAbort(__func__, "Failed to reallocate %zd bytes at %s:%d", size, file, lineno); 539 476 } 540 477 -
trunk/psLib/src/types/psArguments.c
r8805 r8808 7 7 * @author David Robbins, MHPCC 8 8 * 9 * @version $Revision: 1.1 2$ $Name: not supported by cvs2svn $10 * @date $Date: 2006-09-13 02:20:15$9 * @version $Revision: 1.13 $ $Name: not supported by cvs2svn $ 10 * @date $Date: 2006-09-13 21:11:11 $ 11 11 * 12 12 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii 13 13 */ 14 14 15 #include<stdio.h> 16 #include<stdarg.h> 17 #include<string.h> 15 #include <stdio.h> 16 #include <stdarg.h> 17 #include <string.h> 18 #include <inttypes.h> 19 18 20 #include "psArguments.h" 19 21 #include "fitsio.h" … … 315 317 // Print the value 316 318 switch (item->type) { 317 PRINT_CASE(U8, "%u");318 PRINT_CASE(U16, "%u");319 PRINT_CASE(U32, "%u");320 PRINT_CASE(U64, "%lu");321 PRINT_CASE(S8, "%d");322 PRINT_CASE(S16, "%d");323 PRINT_CASE(S32, "%d");324 PRINT_CASE(S64, "%ld");325 PRINT_CASE(F32, "%.6e");326 PRINT_CASE(F64, "%.6e");319 PRINT_CASE(U8, "%u"); 320 PRINT_CASE(U16, "%u"); 321 PRINT_CASE(U32, "%u"); 322 PRINT_CASE(U64, "%" PRIu64); 323 PRINT_CASE(S8, "%d"); 324 PRINT_CASE(S16, "%d"); 325 PRINT_CASE(S32, "%d"); 326 PRINT_CASE(S64, "%" PRId64); 327 PRINT_CASE(F32, "%.6e"); 328 PRINT_CASE(F64, "%.6e"); 327 329 case PS_DATA_BOOL: 328 330 if (item->data.B) {
Note:
See TracChangeset
for help on using the changeset viewer.
