Changeset 15126 for trunk/FACT++/src


Ignore:
Timestamp:
03/20/13 13:50:05 (12 years ago)
Author:
lyard
Message:
fixed mem alloc and changed sleep time with Thomas
File:
1 edited

Legend:

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

    r14734 r15126  
    166166                    EtiMemoryChunks[i].events[j] = evtId;
    167167                    EtiMemoryChunks[i].nFreeSlots--;
     168                    if (EtiMemoryChunks[i].nFreeSlots < 0)
     169                    {
     170                        snprintf(str, MXSTR, "Number of free slot in chunk %d went below zero (%d) slot: %d", i, EtiMemoryChunks[i].nFreeSlots, j);
     171                        factOut(kError, 000, str);
     172                        return NULL;
     173                    }
    168174                    mBufferMapping[evtIndex].eventNumber = evtId;
    169175                    mBufferMapping[evtIndex].chunk = i;
     
    174180            //If I reach this point then we have a problem because it should have found
    175181            //a free spot just above.
     182            snprintf(str, MXSTR, "Could not find a free slot in a chunk that's supposed to have some. chunk=%d", i);
     183            factOut(kError, 000, str);
     184            return NULL;
    176185        }
    177186    }
    178187    //If we reach this point this means that we should allocate more memory
    179     int32_t numNewSlots = 0;
    180     if ((numAllocatedChunks + 1)*MAX_CHUNK_SIZE < g_maxMem)
    181         numNewSlots = MAX_SLOTS_PER_CHUNK;
    182     else
    183         numNewSlots = (g_maxMem - numAllocatedChunks*MAX_CHUNK_SIZE)/MAX_SLOT_SIZE;
    184 
    185     if (numNewSlots == 0)//cannot allocate more without exceeding the max mem limit
    186     {
     188    int32_t numNewSlots = MAX_SLOTS_PER_CHUNK;
     189    if ((numAllocatedChunks + 1)*MAX_CHUNK_SIZE >= g_maxMem)
    187190        return NULL;
    188     }
    189 
    190     EtiMemoryChunks[numAllocatedChunks].pointers[0] = malloc(MAX_SLOT_SIZE*numNewSlots);
     191
     192    EtiMemoryChunks[numAllocatedChunks].pointers[0] = malloc(MAX_SLOT_SIZE*MAX_SLOTS_PER_CHUNK);
    191193    if (EtiMemoryChunks[numAllocatedChunks].pointers[0] == NULL)
    192194    {
     
    205207    for (int i=1;i<numNewSlots;i++)
    206208    {
    207         EtiMemoryChunks[numAllocatedChunks].pointers[i] = &(((char*)(EtiMemoryChunks[numAllocatedChunks].pointers[i-1]))[MAX_SLOT_SIZE]);
     209        EtiMemoryChunks[numAllocatedChunks].pointers[i] = EtiMemoryChunks[numAllocatedChunks].pointers[0] + i*MAX_SLOT_SIZE;// &(((char*)(EtiMemoryChunks[numAllocatedChunks].pointers[i-1]))[MAX_SLOT_SIZE]);
    208210        EtiMemoryChunks[numAllocatedChunks].events[i] = -1;
    209211    }
     
    239241        free(EtiMemoryChunks[chunkIndex].pointers[0]);
    240242        EtiMemoryChunks[chunkIndex].pointers[0] = NULL;
     243        EtiMemoryChunks[chunkIndex].nSlots = 0;
    241244        numAllocatedChunks--;
    242245        chunkIndex--;
     
    680683                     evID, sk);
    681684           factOut (kError, 882, str);
    682         } else {
     685        }
     686#ifdef EVTDEBUG
     687       else
     688       {
    683689           snprintf (str, MXSTR, "No memory left to keep event %6d sock %3d",
    684690                     evID, sk);
    685691           factOut (kDebug, 882, str);
    686692        }
     693#endif
    687694       return -11;
    688695   }
     
    767774
    768775
     776#ifdef EVTDEBUG
    769777   snprintf (str, MXSTR,
    770778             "%5d %8d start new evt  %8d %8d sock %3d len %5d t %10d", evID,
    771779             runID, i, evtCtrl.lastPtr, sk, fadlen, mBuffer[i].pcTime);
    772780   factOut (kDebug, -11, str);
     781#endif
    773782   evtCtrl.lastPtr++;
    774783   if (evtCtrl.lastPtr == MAX_EVT * MAX_RUN)
     
    11351144            } else {
    11361145               jrd = 0;         //did read nothing as requested
     1146#ifdef EVTDEBUG
    11371147               snprintf (str, MXSTR, "do not read from socket %d  %d", i,
    11381148                         rd[i].bufLen);
    11391149               factOut (kDebug, 301, str);
    1140             }
     1150#endif
     1151               }
    11411152
    11421153            gi.gotByte[b] += jrd;
     
    14101421                        }
    14111422                     }
     1423#ifdef EVTDEBUG
    14121424                     snprintf (str, MXSTR,
    14131425                               "%5d complete event roi %4d roiTM %d cpy %8d %5d",
     
    14151427                               qncpy, qnrun);
    14161428                     factOut (kDebug, -1, str);
    1417 
     1429#endif
    14181430                     //complete event read ---> flag for next processing
    14191431                     evtCtrl.evtStat[iDx] = 99;
     
    15981610
    15991611               int id = evtCtrl.evtBuf[k0];
     1612#ifdef EVTDEBUG
    16001613               snprintf (str, MXSTR, "%5d free event buffer, nb=%3d",
    16011614                         mBuffer[id].evNum, mBuffer[id].nBoard);
    16021615               factOut (kDebug, -1, str);
     1616#endif
    16031617               mBufFree (id);   //event written--> free memory
    16041618               evtCtrl.evtStat[k0] = -1;
     
    16431657         } else {
    16441658            xwait.tv_sec = 0;
    1645             xwait.tv_nsec = 2000000;    // sleep for ~2 msec
     1659            xwait.tv_nsec = 1000;    // sleep for ~1 usec
    16461660         }
    16471661         nanosleep (&xwait, NULL);
Note: See TracChangeset for help on using the changeset viewer.