00001 00002 /* Allows for the storage and reuse of memory blocks for event messages and 00003 checkpointing. */ 00004 #include "charm++.h" 00005 #include "pose.h" 00006 #include "mempool.def.h" 00007 00008 CkGroupID MemPoolID; 00009 00010 // Returns number of blocks of size sz in pool 00011 int MemoryPool::CheckPool(int sz) 00012 { 00013 if (sz > MAX_RECYCLABLE) return -1; 00014 if (memPools) { 00015 if (lastLook && (lastLook->blockSize == sz)) 00016 return (lastLook->numBlocks); 00017 else { 00018 lastLook = memPools; 00019 while (lastLook && (lastLook->blockSize < sz)) 00020 lastLook = lastLook->next; 00021 if (lastLook && (lastLook->blockSize == sz)) 00022 return lastLook->numBlocks; 00023 return 0; 00024 } 00025 } 00026 else return 0; 00027 } 00028 00029 // Returns a block of size sz from pool 00030 void *MemoryPool::GetBlock(int sz) 00031 { 00032 if (lastLook && (lastLook->blockSize == sz)) { 00033 lastLook->numBlocks--; 00034 return lastLook->memPool[lastLook->numBlocks]; 00035 } 00036 else { 00037 lastLook = memPools; 00038 while (lastLook->blockSize != sz) 00039 lastLook = lastLook->next; 00040 lastLook->numBlocks--; 00041 return lastLook->memPool[lastLook->numBlocks]; 00042 } 00043 } 00044 00045 // Puts a block of size sz in pool 00046 void MemoryPool::PutBlock(int sz, void *blk) 00047 { 00048 if (lastLook && (lastLook->blockSize == sz)) { 00049 lastLook->memPool[lastLook->numBlocks] = blk; 00050 lastLook->numBlocks++; 00051 } 00052 else { 00053 lastLook = memPools; 00054 if (!lastLook) { 00055 Pool *np = new Pool(); 00056 np->blockSize = sz; 00057 np->numBlocks = 0; 00058 np->next = np->prev = NULL; 00059 np->memPool[np->numBlocks] = blk; 00060 np->numBlocks++; 00061 memPools = lastLook = np; 00062 } 00063 else if (lastLook->blockSize > sz) { 00064 Pool *np = new Pool(); 00065 np->blockSize = sz; 00066 np->numBlocks = 0; 00067 np->next = memPools; 00068 np->prev = NULL; 00069 lastLook->prev = np; 00070 np->memPool[np->numBlocks] = blk; 00071 np->numBlocks++; 00072 memPools = lastLook = np; 00073 } 00074 else if (lastLook->blockSize == sz) { 00075 lastLook->memPool[lastLook->numBlocks] = blk; 00076 lastLook->numBlocks++; 00077 } 00078 else { 00079 while (lastLook->next && (lastLook->next->blockSize < sz)) 00080 lastLook = lastLook->next; 00081 if (lastLook->next) { 00082 if (lastLook->next->blockSize == sz) { 00083 lastLook->next->memPool[lastLook->next->numBlocks] = blk; 00084 lastLook->next->numBlocks++; 00085 } 00086 else { 00087 Pool *np = new Pool(); 00088 np->blockSize = sz; 00089 np->numBlocks = 0; 00090 np->next = lastLook->next; 00091 np->prev = lastLook; 00092 lastLook->next->prev = np; 00093 lastLook->next = np; 00094 np->memPool[np->numBlocks] = blk; 00095 np->numBlocks++; 00096 lastLook = np; 00097 } 00098 } 00099 else { 00100 Pool *np = new Pool(); 00101 np->blockSize = sz; 00102 np->numBlocks = 0; 00103 np->next = NULL; 00104 np->prev = lastLook; 00105 lastLook->next = np; 00106 np->memPool[np->numBlocks] = blk; 00107 np->numBlocks++; 00108 lastLook = np; 00109 } 00110 } 00111 } 00112 }