Changeset 15468


Ignore:
Timestamp:
05/01/13 10:52:21 (12 years ago)
Author:
tbretz
Message:
Cleaned up the file removing some old style memory allocation schemes
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/EventBuilder.c

    r15467 r15468  
    141141
    142142
    143 #define THOMAS_MALLOC
    144 
    145 #ifdef THOMAS_MALLOC
    146143#define MAX_HEAD_MEM (NBOARDS * sizeof(PEVNT_HEADER))
    147144#define MAX_TOT_MEM (sizeof(EVENT) + (NPIX+NTMARK)*1024*2 + MAX_HEAD_MEM)
     
    196193    tgb_inuse -= MAX_TOT_MEM;
    197194}
    198 #endif
    199 
    200 #ifdef ETIENNE_MALLOC
    201 //ETIENNE
    202 #define MAX_SLOTS_PER_CHUNK 100
    203 
    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 8096
    227 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 found
    251             //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 memory
    257     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 chunk
    304         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 ETIENNE
    319 #endif
    320 
    321195
    322196RUN_CTRL runCtrl[MAX_RUN];
     
    507381      //evtCtrl[i].mBuffer_idx = -1;
    508382      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   }
    522384
    523385   //actRun.FADhead = malloc (NBOARDS * sizeof (PEVNT_HEADER));
     
    739601//(and make sure multiple calls do no harm ....)
    740602
    741 //   int headmem = 0;
    742 //   size_t freemem = 0;
    743 
    744 #ifdef ETIENNE_MALLOC
    745    int evid;
    746    evid = mBuffer[i].evNum;
    747    ETI_Free(evid, i);
    748 #endif
    749 
    750 //   freemem = mBuffer[i].evtLen;
    751    //ETIENNE
    752 #ifdef THOMAS_MALLOC
    753603   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
    761605   evtCtrl[i].fEvent = NULL;
    762 
    763 //   free (mBuffer[i].FADhead);
    764606   evtCtrl[i].FADhead = NULL;
    765607
    766 //   free (mBuffer[i].buffer);
    767 //   mBuffer[i].buffer = NULL;
    768 //END ETIENNE
    769 //   headmem = NBOARDS * sizeof (PEVNT_HEADER);
    770608   evtCtrl[i].evNum = evtCtrl[i].nRoi = -1;
    771609   evtCtrl[i].runNum = 0;
    772610
    773 #ifdef ETIENNE_MALLOC
    774 //ETIENNE
    775 //   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 ETIENNE
    782 #endif
    783 
    784 #ifdef THOMAS_MALLOC
    785611   gj.usdMem = tgb_inuse;
    786 #endif
    787612
    788613   gj.bufTot--;
     
    1134959      gj.bufTot = gj.maxEvt = gj.xxxEvt = 0;
    1135960      gj.usdMem = gj.maxMem = gj.xxxMem = 0;
    1136 #ifdef THOMAS_MALLOC
    1137961      gj.totMem = tgb_memory;
    1138 #else
    1139       gj.totMem = g_maxMem;
    1140 #endif
    1141962      gj.bufNew = gj.bufEvt = 0;
    1142963      //gj.badRoiE = gj.badRoiR = gj.badRoiB = 0;
     
    16481469          gj.totBytes[ib] += gj.rateBytes[ib];
    16491470
    1650 #ifdef THOMAS_MALLOC
    16511471      gj.totMem = tgb_memory;
    1652 #else
    1653       gj.totMem = g_maxMem;
    1654 #endif
    16551472
    16561473      if (gj.maxMem > gj.xxxMem)
     
    18031620   factPrintf(kInfo, -1, "Exit read Process...");
    18041621
    1805 #ifdef THOMAS_MALLOC
    18061622   factPrintf(kInfo, -1, "%ld Bytes flaged as in-use.", tgb_inuse);
    1807 #endif
    18081623
    18091624   gi_runStat = -99;
Note: See TracChangeset for help on using the changeset viewer.