more bug fixes
[IRC.git] / Robust / src / Runtime / memPool.h
index 0662e5fcca5949df2ca016f0910c46edd8b9e566..49612497a7db104f3ed0807b43eba5227dd4e8f9 100644 (file)
@@ -28,7 +28,6 @@
 #include <string.h>
 static INTPTR pageSize;
 #endif
-
 #include "runtime.h"
 #include "mem.h"
 #include "mlp_lock.h"
@@ -39,7 +38,7 @@ static INTPTR pageSize;
 
 
 typedef struct MemPoolItem_t {
-  void* next;
+  struct MemPoolItem_t* next;
 } MemPoolItem;
 
 
@@ -133,42 +132,23 @@ static inline void poolfreeinto( MemPool* p, void* ptr ) {
       printf( "mprotect failed, errno=%d.\n", errno );
     } 
 
+    printf( "itemSize is 0x%x, allocSize is 0x%x, protectSize is 0x%x.\n", (INTPTR)p->itemSize, (INTPTR)p->allocSize, (INTPTR)p->protectSize );
+    printf( "Intended to protect 0x%x to 0x%x,\n\n", (INTPTR)ptr, (INTPTR)ptr + (INTPTR)(p->protectSize) );
+
     exit( -1 );
   }
 }
 
 #else
 
+
 // normal version
 static inline void poolfreeinto( MemPool* p, void* ptr ) {
-
-  MemPoolItem* tailCurrent;
-  MemPoolItem* tailActual;
-  
-  // set up the now unneeded record to as the tail of the
-  // free list by treating its first bytes as next pointer,
   MemPoolItem* tailNew = (MemPoolItem*) ptr;
   tailNew->next = NULL;
-
-  while( 1 ) {
-    // make sure the null happens before the insertion,
-    // also makes sure that we reload tailCurrent, etc..
-    BARRIER();
-
-    tailCurrent = p->tail;
-    tailActual = (MemPoolItem*)
-      CAS( &(p->tail),         // ptr to set
-           (INTPTR) tailCurrent, // current tail's next should be NULL
-           (INTPTR) tailNew      // try set to our new tail
-           );   
-    if( tailActual == tailCurrent ) {
-      // success, update tail
-      tailCurrent->next = tailNew;
-      return;
-    }
-
-    // if CAS failed, retry entire operation
-  }
+  CFENCE;
+  MemPoolItem *tailCurrent=(MemPoolItem *) LOCKXCHG((INTPTR *) &p->tail, (INTPTR) tailNew);
+  tailCurrent->next=tailNew;
 }
 #endif
 
@@ -180,12 +160,13 @@ static inline void* poolalloc( MemPool* p ) {
   // put the memory we intend to expose to client
   // on a page-aligned boundary, always return
   // new memory
+
   INTPTR nonAligned = (INTPTR) RUNMALLOC( p->allocSize );
 
   void* newRec = (void*)((nonAligned + pageSize-1) & ~(pageSize-1));
 
   //printf( "PageSize is %d or 0x%x.\n", (INTPTR)pageSize, (INTPTR)pageSize );
-  //printf( "itemSize is 0x%x and allocSize is 0x%x.\n", (INTPTR)p->itemSize, (INTPTR)p->allocSize );
+  //printf( "itemSize is 0x%x, allocSize is 0x%x, protectSize is 0x%x.\n", (INTPTR)p->itemSize, (INTPTR)p->allocSize, (INTPTR)p->protectSize );
   //printf( "Allocation returned 0x%x to 0x%x,\n",   (INTPTR)nonAligned, (INTPTR)nonAligned + (INTPTR)(p->allocSize) );
   //printf( "Intend to use       0x%x to 0x%x,\n\n", (INTPTR)newRec,     (INTPTR)newRec     + (INTPTR)(p->itemSize)  );
   
@@ -195,8 +176,6 @@ static inline void* poolalloc( MemPool* p ) {
   topOfRec += p->protectSize - 1;
   ((char*)topOfRec)[0] = 0x1;
 
-
-
   if( p->initFreshlyAllocated != NULL ) {
     p->initFreshlyAllocated( newRec );
   }
@@ -217,14 +196,14 @@ static inline void* poolalloc( MemPool* p ) {
   MemPoolItem* headCurrent = p->head;
   MemPoolItem* next=headCurrent->next;
   int i;
+
+
   if(next == NULL) {
     // only one item, so don't take from pool
-    void* newRec = RUNMALLOC( p->itemSize );
-
+    void *newRec=RUNMALLOC(p->itemSize);
     if( p->initFreshlyAllocated != NULL ) {
       p->initFreshlyAllocated( newRec );
     }
-
     return newRec;
   }
  
@@ -258,13 +237,3 @@ static void pooldestroy( MemPool* p ) {
 
 
 #endif // ___MEMPOOL_H__
-
-
-
-
-
-
-
-
-
-