Index: trunk/FACT++/src/EventBuilder.c
===================================================================
--- trunk/FACT++/src/EventBuilder.c	(revision 15122)
+++ trunk/FACT++/src/EventBuilder.c	(revision 15126)
@@ -166,4 +166,10 @@
                     EtiMemoryChunks[i].events[j] = evtId;
                     EtiMemoryChunks[i].nFreeSlots--;
+                    if (EtiMemoryChunks[i].nFreeSlots < 0)
+                    {
+                        snprintf(str, MXSTR, "Number of free slot in chunk %d went below zero (%d) slot: %d", i, EtiMemoryChunks[i].nFreeSlots, j);
+                        factOut(kError, 000, str);
+                        return NULL;
+                    }
                     mBufferMapping[evtIndex].eventNumber = evtId;
                     mBufferMapping[evtIndex].chunk = i;
@@ -174,19 +180,15 @@
             //If I reach this point then we have a problem because it should have found
             //a free spot just above.
+            snprintf(str, MXSTR, "Could not find a free slot in a chunk that's supposed to have some. chunk=%d", i);
+            factOut(kError, 000, str);
+            return NULL;
         }
     }
     //If we reach this point this means that we should allocate more memory
-    int32_t numNewSlots = 0;
-    if ((numAllocatedChunks + 1)*MAX_CHUNK_SIZE < g_maxMem)
-        numNewSlots = MAX_SLOTS_PER_CHUNK;
-    else
-        numNewSlots = (g_maxMem - numAllocatedChunks*MAX_CHUNK_SIZE)/MAX_SLOT_SIZE;
-
-    if (numNewSlots == 0)//cannot allocate more without exceeding the max mem limit
-    {
+    int32_t numNewSlots = MAX_SLOTS_PER_CHUNK;
+    if ((numAllocatedChunks + 1)*MAX_CHUNK_SIZE >= g_maxMem)
         return NULL;
-    }
-
-    EtiMemoryChunks[numAllocatedChunks].pointers[0] = malloc(MAX_SLOT_SIZE*numNewSlots);
+
+    EtiMemoryChunks[numAllocatedChunks].pointers[0] = malloc(MAX_SLOT_SIZE*MAX_SLOTS_PER_CHUNK);
     if (EtiMemoryChunks[numAllocatedChunks].pointers[0] == NULL)
     {
@@ -205,5 +207,5 @@
     for (int i=1;i<numNewSlots;i++)
     {
-        EtiMemoryChunks[numAllocatedChunks].pointers[i] = &(((char*)(EtiMemoryChunks[numAllocatedChunks].pointers[i-1]))[MAX_SLOT_SIZE]);
+        EtiMemoryChunks[numAllocatedChunks].pointers[i] = EtiMemoryChunks[numAllocatedChunks].pointers[0] + i*MAX_SLOT_SIZE;// &(((char*)(EtiMemoryChunks[numAllocatedChunks].pointers[i-1]))[MAX_SLOT_SIZE]);
         EtiMemoryChunks[numAllocatedChunks].events[i] = -1;
     }
@@ -239,4 +241,5 @@
         free(EtiMemoryChunks[chunkIndex].pointers[0]);
         EtiMemoryChunks[chunkIndex].pointers[0] = NULL;
+        EtiMemoryChunks[chunkIndex].nSlots = 0;
         numAllocatedChunks--;
         chunkIndex--;
@@ -680,9 +683,13 @@
                      evID, sk);
            factOut (kError, 882, str);
-        } else {
+        }
+#ifdef EVTDEBUG
+       else
+       {
            snprintf (str, MXSTR, "No memory left to keep event %6d sock %3d",
                      evID, sk);
            factOut (kDebug, 882, str);
         }
+#endif
        return -11;
    }
@@ -767,8 +774,10 @@
 
 
+#ifdef EVTDEBUG
    snprintf (str, MXSTR,
              "%5d %8d start new evt  %8d %8d sock %3d len %5d t %10d", evID,
              runID, i, evtCtrl.lastPtr, sk, fadlen, mBuffer[i].pcTime);
    factOut (kDebug, -11, str);
+#endif
    evtCtrl.lastPtr++;
    if (evtCtrl.lastPtr == MAX_EVT * MAX_RUN)
@@ -1135,8 +1144,10 @@
             } else {
                jrd = 0;         //did read nothing as requested
+#ifdef EVTDEBUG
                snprintf (str, MXSTR, "do not read from socket %d  %d", i,
                          rd[i].bufLen);
                factOut (kDebug, 301, str);
-            }
+#endif
+               }
 
             gi.gotByte[b] += jrd;
@@ -1410,4 +1421,5 @@
                         }
                      }
+#ifdef EVTDEBUG
                      snprintf (str, MXSTR,
                                "%5d complete event roi %4d roiTM %d cpy %8d %5d",
@@ -1415,5 +1427,5 @@
                                qncpy, qnrun);
                      factOut (kDebug, -1, str);
-
+#endif
                      //complete event read ---> flag for next processing
                      evtCtrl.evtStat[iDx] = 99;
@@ -1598,7 +1610,9 @@
 
                int id = evtCtrl.evtBuf[k0];
+#ifdef EVTDEBUG
                snprintf (str, MXSTR, "%5d free event buffer, nb=%3d",
                          mBuffer[id].evNum, mBuffer[id].nBoard);
                factOut (kDebug, -1, str);
+#endif
                mBufFree (id);   //event written--> free memory
                evtCtrl.evtStat[k0] = -1;
@@ -1643,5 +1657,5 @@
          } else {
             xwait.tv_sec = 0;
-            xwait.tv_nsec = 2000000;    // sleep for ~2 msec
+            xwait.tv_nsec = 1000;    // sleep for ~1 usec
          }
          nanosleep (&xwait, NULL);
