incorporating modifications to mempool done by Gengbin
[charm.git] / src / arch / util / mempool.c
index 631825966f2765ac1d75a8e7cbc4c0c6b77da16f..d88111e954f65ba5476875ebeb585f46027ec12b 100644 (file)
@@ -24,10 +24,10 @@ Generalized by Gengbin Zheng  10/5/2011
 
 #include "mempool.h"
 
 
 #include "mempool.h"
 
-#if USE_MEMPOOL_ISOMALLOC
-static      size_t     expand_mem = 1024*16;
-#else
+#if CMK_CONVERSE_GEMINI_UGNI
 static      size_t     expand_mem = 1024ll*1024*16;
 static      size_t     expand_mem = 1024ll*1024*16;
+#else
+static      size_t     expand_mem = 1024*16;
 #endif 
 
 mempool_type *mempool_init(size_t pool_size, mempool_newblockfn allocfn, mempool_freeblock freefn)
 #endif 
 
 mempool_type *mempool_init(size_t pool_size, mempool_newblockfn allocfn, mempool_freeblock freefn)
@@ -145,6 +145,7 @@ void*  mempool_malloc(mempool_type *mptr, int size, int expand)
         printf("[%d] No memory has such free empty chunck of %d. expanding %p with new size %d\n", CmiMyPe(), size, expand_pool->mempool_ptr, expand_size);
 #endif
         // FIXME: go to the end of link list
         printf("[%d] No memory has such free empty chunck of %d. expanding %p with new size %d\n", CmiMyPe(), size, expand_pool->mempool_ptr, expand_size);
 #endif
         // FIXME: go to the end of link list
+        //while (mempools_head->next != NULL && mempools_head < expand_pool) mempools_head = mempools_head->next;
         while (mempools_head->next != NULL) mempools_head = mempools_head->next;
         mempools_head->next = expand_pool;
 
         while (mempools_head->next != NULL) mempools_head = mempools_head->next;
         mempools_head->next = expand_pool;
 
@@ -154,13 +155,13 @@ void*  mempool_malloc(mempool_type *mptr, int size, int expand)
         bestfit->next_free = 0;
         bestfit_size = expand_size-sizeof(mempool_block);
 #if 0
         bestfit->next_free = 0;
         bestfit_size = expand_size-sizeof(mempool_block);
 #if 0
-        current = freelist_head;
+        current = freelist_head_ptr;
         while(current!= NULL && current < bestfit )
         {
           previous = current;
         while(current!= NULL && current < bestfit )
         {
           previous = current;
-          current = current->next;
+          current = current->next_free?(mempool_header*)((char *)mptr + current->next_free):NULL;
         }
         }
-#else
+#elif 0
         CmiAssert(bestfit > previous);
 #endif
         bestfit_previous = previous;
         CmiAssert(bestfit > previous);
 #endif
         bestfit_previous = previous;
@@ -230,7 +231,23 @@ void mempool_free(mempool_type *mptr, void *ptr_free)
     printf("[%d] INSIDE FREE ptr=%p, size=%d freehead=%p mutex: %p\n", CmiMyPe(), to_free, free_size, freelist_head, mptr->mutex);
 #endif
     
     printf("[%d] INSIDE FREE ptr=%p, size=%d freehead=%p mutex: %p\n", CmiMyPe(), to_free, free_size, freelist_head, mptr->mutex);
 #endif
     
-    while(current!= NULL && current < to_free )
+    /*while(current!= NULL && current < to_free )
+    {
+#if  MEMPOOL_DEBUG
+        CmiPrintf("[%d] previous=%p, current=%p size:%d %p\n", CmiMyPe(), previous, current, current->size, (char*)current+current->size);
+#endif
+        previous = current;
+        current = current->next_free?(mempool_header*)((char*)mptr + current->next_free):NULL;
+    }*/
+    while(current!= NULL && memcmp(&current->mem_hndl, &to_free->mem_hndl, sizeof(gni_mem_handle_t))!=0)
+    {
+#if  MEMPOOL_DEBUG
+        CmiPrintf("[%d] previous=%p, current=%p size:%d %p\n", CmiMyPe(), previous, current, current->size, (char*)current+current->size);
+#endif
+        previous = current;
+        current = current->next_free?(mempool_header*)((char*)mptr + current->next_free):NULL;
+    }
+    while(current!= NULL && current < to_free && memcmp(&current->mem_hndl, &to_free->mem_hndl, sizeof(gni_mem_handle_t))==0)
     {
 #if  MEMPOOL_DEBUG
         CmiPrintf("[%d] previous=%p, current=%p size:%d %p\n", CmiMyPe(), previous, current, current->size, (char*)current+current->size);
     {
 #if  MEMPOOL_DEBUG
         CmiPrintf("[%d] previous=%p, current=%p size:%d %p\n", CmiMyPe(), previous, current, current->size, (char*)current+current->size);
@@ -242,7 +259,7 @@ void mempool_free(mempool_type *mptr, void *ptr_free)
     if (current) CmiPrintf("[%d] previous=%p, current=%p size:%d %p\n", CmiMyPe(), previous, current, current->size, free_lastbytes_pos);
 #endif
     //continuos with previous free space 
     if (current) CmiPrintf("[%d] previous=%p, current=%p size:%d %p\n", CmiMyPe(), previous, current, current->size, free_lastbytes_pos);
 #endif
     //continuos with previous free space 
-    if(previous!= NULL && (char*)previous+previous->size == to_free &&  memcmp(&previous->mem_hndl, &to_free->mem_hndl, sizeof(gni_mem_handle_t))==0 )
+    if(previous!= NULL && (char*)previous+previous->size == (char*)to_free &&  memcmp(&previous->mem_hndl, &to_free->mem_hndl, sizeof(gni_mem_handle_t))==0 )
     {
         previous->size +=  free_size;
         merged = 1;
     {
         previous->size +=  free_size;
         merged = 1;