Changeset 15468
- Timestamp:
- 05/01/13 10:52:21 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/EventBuilder.c
r15467 r15468 141 141 142 142 143 #define THOMAS_MALLOC144 145 #ifdef THOMAS_MALLOC146 143 #define MAX_HEAD_MEM (NBOARDS * sizeof(PEVNT_HEADER)) 147 144 #define MAX_TOT_MEM (sizeof(EVENT) + (NPIX+NTMARK)*1024*2 + MAX_HEAD_MEM) … … 196 193 tgb_inuse -= MAX_TOT_MEM; 197 194 } 198 #endif199 200 #ifdef ETIENNE_MALLOC201 //ETIENNE202 #define MAX_SLOTS_PER_CHUNK 100203 204 typedef struct {205 int32_t eventNumber;206 int32_t chunk;207 int32_t slot;208 } CHUNK_MAPPING;209 210 CHUNK_MAPPING mBufferMapping[MAX_EVT * MAX_RUN];211 212 #define MAX_EVT_MEM (sizeof(EVENT) + NPIX*1024*2 + NTMARK*1024*2)213 #define MAX_HEAD_MEM (NBOARDS * sizeof(PEVNT_HEADER))214 #define MAX_SLOT_SIZE (MAX_EVT_MEM + MAX_HEAD_MEM)215 #define MAX_CHUNK_SIZE (MAX_SLOT_SIZE*MAX_SLOTS_PER_CHUNK)216 217 typedef struct {218 void * pointers[MAX_SLOTS_PER_CHUNK];219 int32_t events[MAX_SLOTS_PER_CHUNK];220 int32_t nFreeSlots;221 int32_t nSlots;222 } ETI_CHUNK;223 224 int32_t numAllocatedChunks = 0;225 226 #define MAX_CHUNKS 8096227 ETI_CHUNK EtiMemoryChunks[MAX_CHUNKS];228 229 void* ETI_Malloc(int evtId, int evtIndex)230 {231 for (int i=0;i<numAllocatedChunks;i++) {232 if (EtiMemoryChunks[i].nFreeSlots > 0) {233 for (int j=0;j<EtiMemoryChunks[i].nSlots;j++)234 {235 if (EtiMemoryChunks[i].events[j] == -1)236 {237 EtiMemoryChunks[i].events[j] = evtId;238 EtiMemoryChunks[i].nFreeSlots--;239 if (EtiMemoryChunks[i].nFreeSlots < 0)240 {241 factPrintf(kError, 0, "Number of free slot in chunk %d went below zero (%d) slot: %d", i, EtiMemoryChunks[i].nFreeSlots, j);242 return NULL;243 }244 mBufferMapping[evtIndex].eventNumber = evtId;245 mBufferMapping[evtIndex].chunk = i;246 mBufferMapping[evtIndex].slot = j;247 return EtiMemoryChunks[i].pointers[j];248 }249 }250 //If I reach this point then we have a problem because it should have found251 //a free spot just above.252 factPrintf(kError, 0, "Could not find a free slot in a chunk that's supposed to have some. chunk=%d", i);253 return NULL;254 }255 }256 //If we reach this point this means that we should allocate more memory257 int32_t numNewSlots = MAX_SLOTS_PER_CHUNK;258 if ((numAllocatedChunks + 1)*MAX_CHUNK_SIZE >= g_maxMem)259 return NULL;260 261 EtiMemoryChunks[numAllocatedChunks].pointers[0] = malloc(MAX_SLOT_SIZE*MAX_SLOTS_PER_CHUNK);262 if (EtiMemoryChunks[numAllocatedChunks].pointers[0] == NULL)263 {264 factPrintf(kError, 0, "Allocation of %lu bytes failed. %d chunks are currently allocated (max allowed %lu bytes)", MAX_CHUNK_SIZE, numAllocatedChunks, g_maxMem);265 return NULL;266 }267 268 EtiMemoryChunks[numAllocatedChunks].nSlots = numNewSlots;269 EtiMemoryChunks[numAllocatedChunks].events[0] = evtId;270 EtiMemoryChunks[numAllocatedChunks].nFreeSlots = numNewSlots-1;271 mBufferMapping[evtIndex].eventNumber = evtId;272 mBufferMapping[evtIndex].chunk = numAllocatedChunks;273 mBufferMapping[evtIndex].slot = 0;274 275 for (int i=1;i<numNewSlots;i++)276 {277 EtiMemoryChunks[numAllocatedChunks].pointers[i] = (char*)EtiMemoryChunks[numAllocatedChunks].pointers[0] + i*MAX_SLOT_SIZE;// &(((char*)(EtiMemoryChunks[numAllocatedChunks].pointers[i-1]))[MAX_SLOT_SIZE]);278 EtiMemoryChunks[numAllocatedChunks].events[i] = -1;279 }280 numAllocatedChunks++;281 282 return EtiMemoryChunks[numAllocatedChunks-1].pointers[0];283 }284 285 void ETI_Free(int evtId, int evtIndex)286 {287 ETI_CHUNK* currentChunk = &EtiMemoryChunks[mBufferMapping[evtIndex].chunk];288 if (currentChunk->events[mBufferMapping[evtIndex].slot] != evtId)289 {290 factPrintf(kError, 0, "Mismatch in chunk mapping table. Expected evtId %d. Got %d. No memory was freed.", evtId, currentChunk->events[mBufferMapping[evtIndex].slot]);291 return;292 }293 currentChunk->events[mBufferMapping[evtIndex].slot] = -1;294 currentChunk->nFreeSlots++;295 296 return; /* TEST */297 298 int chunkIndex = mBufferMapping[evtIndex].chunk;299 if (chunkIndex != numAllocatedChunks-1)300 return;301 302 while (EtiMemoryChunks[chunkIndex].nFreeSlots == EtiMemoryChunks[chunkIndex].nSlots)303 {//free this chunk304 if (EtiMemoryChunks[chunkIndex].pointers[0] == NULL)305 {306 factPrintf(kError, 0, "Chunk %d not allocated as it ought to be. Skipping memory release.", chunkIndex);307 return;308 }309 free(EtiMemoryChunks[chunkIndex].pointers[0]);310 EtiMemoryChunks[chunkIndex].pointers[0] = NULL;311 EtiMemoryChunks[chunkIndex].nSlots = 0;312 numAllocatedChunks--;313 chunkIndex--;314 if (numAllocatedChunks == 0)315 break;316 }317 }318 //END ETIENNE319 #endif320 321 195 322 196 RUN_CTRL runCtrl[MAX_RUN]; … … 507 381 //evtCtrl[i].mBuffer_idx = -1; 508 382 evtCtrl[i].evtStat = -1; 509 510 #ifdef ETIENNE_MALLOC 511 //ETIENNE 512 mBufferMapping[i].chunk = -1; 513 mBufferMapping[i].eventNumber = -1; 514 mBufferMapping[i].slot = -1; 515 //END ETIENNE 516 #endif 517 } 518 #ifdef ETIENNE_MALLOC 519 for (int j=0;j<MAX_CHUNKS;j++) 520 EtiMemoryChunks[j].pointers[0] = NULL; 521 #endif 383 } 522 384 523 385 //actRun.FADhead = malloc (NBOARDS * sizeof (PEVNT_HEADER)); … … 739 601 //(and make sure multiple calls do no harm ....) 740 602 741 // int headmem = 0;742 // size_t freemem = 0;743 744 #ifdef ETIENNE_MALLOC745 int evid;746 evid = mBuffer[i].evNum;747 ETI_Free(evid, i);748 #endif749 750 // freemem = mBuffer[i].evtLen;751 //ETIENNE752 #ifdef THOMAS_MALLOC753 603 TGB_free(evtCtrl[i].FADhead); 754 #endif 755 756 #ifdef STANDARD_MALLOC 757 free (mBuffer[i].FADhead); 758 #endif 759 760 // free (mBuffer[i].fEvent); 604 761 605 evtCtrl[i].fEvent = NULL; 762 763 // free (mBuffer[i].FADhead);764 606 evtCtrl[i].FADhead = NULL; 765 607 766 // free (mBuffer[i].buffer);767 // mBuffer[i].buffer = NULL;768 //END ETIENNE769 // headmem = NBOARDS * sizeof (PEVNT_HEADER);770 608 evtCtrl[i].evNum = evtCtrl[i].nRoi = -1; 771 609 evtCtrl[i].runNum = 0; 772 610 773 #ifdef ETIENNE_MALLOC774 //ETIENNE775 // gj.usdMem = gj.usdMem - freemem - headmem - gi_maxSize;776 gj.usdMem = 0;777 for (int k=0;k<numAllocatedChunks;k++)778 {779 gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE;780 }781 //END ETIENNE782 #endif783 784 #ifdef THOMAS_MALLOC785 611 gj.usdMem = tgb_inuse; 786 #endif787 612 788 613 gj.bufTot--; … … 1134 959 gj.bufTot = gj.maxEvt = gj.xxxEvt = 0; 1135 960 gj.usdMem = gj.maxMem = gj.xxxMem = 0; 1136 #ifdef THOMAS_MALLOC1137 961 gj.totMem = tgb_memory; 1138 #else1139 gj.totMem = g_maxMem;1140 #endif1141 962 gj.bufNew = gj.bufEvt = 0; 1142 963 //gj.badRoiE = gj.badRoiR = gj.badRoiB = 0; … … 1648 1469 gj.totBytes[ib] += gj.rateBytes[ib]; 1649 1470 1650 #ifdef THOMAS_MALLOC1651 1471 gj.totMem = tgb_memory; 1652 #else1653 gj.totMem = g_maxMem;1654 #endif1655 1472 1656 1473 if (gj.maxMem > gj.xxxMem) … … 1803 1620 factPrintf(kInfo, -1, "Exit read Process..."); 1804 1621 1805 #ifdef THOMAS_MALLOC1806 1622 factPrintf(kInfo, -1, "%ld Bytes flaged as in-use.", tgb_inuse); 1807 #endif1808 1623 1809 1624 gi_runStat = -99;
Note:
See TracChangeset
for help on using the changeset viewer.