Changeset 10884 for branches/jch-memory/psLib/src/sys/psMemory.c
- Timestamp:
- Jan 2, 2007, 5:36:38 PM (19 years ago)
- File:
-
- 1 edited
-
branches/jch-memory/psLib/src/sys/psMemory.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/jch-memory/psLib/src/sys/psMemory.c
r10547 r10884 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1.88 $ $Name: not supported by cvs2svn $11 * @date $Date: 200 6-12-08 11:35:54$10 * @version $Revision: 1.88.2.1 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2007-01-03 03:36:38 $ 12 12 * 13 13 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 51 51 static bool memory_is_persistent = false; 52 52 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 53 #ifdef PS_MEM_DEBUG 68 54 static psMemBlock* deadBlockList; // a place to put dead memBlocks in debug mode. … … 79 65 static psPtr memExhaustedCallbackDefault(size_t size) 80 66 { 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 67 return NULL; 106 #endif // #ifdef PS_MEM_USE_RECYCLE107 68 } 108 69 … … 364 325 } 365 326 366 #ifdef PS_MEM_USE_RECYCLE367 // Return the appropriate recycle bin number368 static int getRecycleBin(size_t size)369 {370 if (size >= P_PS_LARGE_BLOCK_SIZE) {371 return N_RECYCLE_BINS;372 }373 int binNum = 0; // Recycle bin number374 while (size > recycleBinSize[binNum]) {375 binNum++;376 }377 return binNum;378 }379 380 // Push a pointer onto the recycle bin381 static void recyclePush(psMemBlock *mb, // Pointer to push382 int bin // Recycling bin383 )384 {385 assert(mb);386 assert(bin < N_RECYCLE_BINS);387 388 mb->previousBlock = NULL;389 390 if (safeThreads) {391 pthread_mutex_lock(&recycleMemBlockListMutex);392 }393 394 mb->nextBlock = recycleMemBlockList[bin];395 if (recycleMemBlockList[bin] != NULL) {396 recycleMemBlockList[bin]->previousBlock = mb;397 }398 recycleMemBlockList[bin] = mb;399 recycleBinNums[bin]++;400 401 if (safeThreads) {402 pthread_mutex_unlock(&recycleMemBlockListMutex);403 }404 405 return;406 }407 408 // Pop a pointer from the recycle bin409 static psMemBlock *recyclePop(int bin // Recycling bin410 )411 {412 if (bin >= N_RECYCLE_BINS) {413 return NULL;414 }415 416 if (safeThreads) {417 pthread_mutex_lock(&recycleMemBlockListMutex);418 }419 420 psMemBlock *mb = recycleMemBlockList[bin]; // Pointer popped from recycle bin421 422 if (mb) {423 // We pull it off the list424 recycleMemBlockList[bin] = mb->nextBlock;425 if (recycleMemBlockList[bin] != NULL) {426 recycleMemBlockList[bin]->previousBlock = NULL;427 }428 recycleBinNums[bin]--;429 }430 431 if (safeThreads) {432 pthread_mutex_unlock(&recycleMemBlockListMutex);433 }434 435 return mb;436 }437 #endif // #ifdef PS_MEM_USE_RECYCLE438 439 327 /* 440 328 * Actually allocate memory … … 446 334 447 335 psMemBlock *ptr = NULL; 448 449 #ifdef PS_MEM_USE_RECYCLE450 // Are we in one of the recycle bins?451 int bin = getRecycleBin(size);452 if (bin < N_RECYCLE_BINS) {453 size = recycleBinSize[bin]; // round-up size to next sized bin.454 ptr = recyclePop(bin); // grab out of the recycle bin455 }456 #endif // #ifdef PS_MEM_USE_RECYCLE457 336 458 337 if (ptr == NULL) { … … 531 410 } 532 411 533 #ifdef PS_MEM_USE_RECYCLE534 // Ensure the size matches that of the recycle bin535 int bin = getRecycleBin(size);536 if (bin < N_RECYCLE_BINS) {537 size = recycleBinSize[bin]; // round-up size to next sized bin.538 }539 #endif // #ifdef PS_MEM_USE_RECYCLE540 541 412 if (size == ptr->userMemorySize) { 542 413 // Nothing to do … … 831 702 if (safeThreads) { 832 703 pthread_mutex_unlock(&memBlockListMutex); 833 }834 835 #ifdef PS_MEM_USE_RECYCLE836 // do we recycle?837 int bin = getRecycleBin(ptr->userMemorySize);838 if (bin < N_RECYCLE_BINS && recycleBinNums[bin] < MAX_RECYCLE) {839 ptr->refCounter = 0;840 recyclePush(ptr, bin);841 } else842 #endif // #ifdef PS_MEM_USE_RECYCLE843 {844 // memory is larger than I want to recycle.845 #ifdef PS_MEM_DEBUG846 (void)p_psRealloc(vptr, 0, file, lineno);847 ptr->previousBlock = NULL;848 ptr->nextBlock = deadBlockList;849 if (deadBlockList != NULL) {850 deadBlockList->previous = ptr;851 }852 deadBlockList = ptr;853 #else // #ifdef PS_MEM_DEBUG854 855 if (safeThreads) {856 pthread_mutex_destroy(&ptr->refCounterMutex);857 }858 free(ptr);859 #endif // #else - #ifdef PS_MEM_DEBUG860 861 704 } 862 705 … … 1221 1064 size_t psMemStats(const bool print, // print details as they're found? 1222 1065 size_t *allocated, // memory that's currently allocated (but not persistent) 1223 size_t *persistent, // persistent memory that's currently allocated 1224 size_t *freelist) // memory that's waiting to be recycled 1066 size_t *persistent) // persistent memory that's currently allocated 1225 1067 { 1226 1068 const size_t overhead = sizeof(psMemBlock) + sizeof(psPtr); // overhead on each allocation … … 1264 1106 printf("Persistent %6s %10zd %8zd\n", "", persist, npersist); 1265 1107 } 1266 /*1267 * Count the memory on the free list1268 */1269 size_t freelist_s;1270 if (freelist == NULL) {1271 freelist = &freelist_s;1272 }1273 1274 *freelist = 0;1275 int nblock_tot = 0;1276 #ifdef PS_MEM_USE_RECYCLE1277 1278 for (int i = 0; recycleBinSize[i] < P_PS_LARGE_BLOCK_SIZE; i++) {1279 size_t bin_contents = 0;1280 size_t nblock = 0;1281 for (const psMemBlock *ptr = recycleMemBlockList[i]; ptr != NULL; ptr = ptr->nextBlock) {1282 nblock++;1283 bin_contents += ptr->userMemorySize + overhead;1284 }1285 *freelist += bin_contents;1286 nblock_tot += nblock;1287 1288 if (print) {1289 printf("Freelist %6d %10d %8d\n", recycleBinSize[i], bin_contents, nblock);1290 }1291 }1292 #endif // #ifdef PS_MEM_USE_RECYCLE1293 1294 if (print) {1295 printf("Freelist %6s %10zd %8d\n", "", *freelist, nblock_tot);1296 }1297 1298 1108 1299 1109 if (safeThreads) { … … 1301 1111 } 1302 1112 1303 return * freelist + *allocated + *persistent;1304 } 1113 return *allocated + *persistent; 1114 }
Note:
See TracChangeset
for help on using the changeset viewer.
