Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
also enforce that the freed block were marked as used before doing so. Theses stats...
authorMartin Quinson <martin.quinson@loria.fr>
Thu, 2 Feb 2012 20:41:32 +0000 (21:41 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Thu, 2 Feb 2012 20:41:32 +0000 (21:41 +0100)
src/xbt/mmalloc/mfree.c
src/xbt/mmalloc/mmalloc.c
src/xbt/mmalloc/mrealloc.c

index bb7bd00..af92242 100644 (file)
@@ -63,8 +63,14 @@ void mfree(struct mdesc *mdp, void *ptr)
       /* Coalesce this block with its predecessor.  */
       mdp->heapinfo[i].free_block.size += mdp->heapinfo[block].busy_block.size;
       /* Mark all my ex-blocks as free */
-      for (it=0; it<mdp->heapinfo[block].busy_block.size; it++)
+      for (it=0; it<mdp->heapinfo[block].busy_block.size; it++) {
+         if (mdp->heapinfo[block+it].type <0) {
+               fprintf(stderr,"Internal Error: Asked to free a block already marked as free (block=%lu it=%d type=%lu). Please report this bug.\n",
+                               (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].type);
+               abort();
+         }
          mdp->heapinfo[block+it].type = -1;
+      }
 
       block = i;
     } else {
@@ -76,8 +82,14 @@ void mfree(struct mdesc *mdp, void *ptr)
       mdp->heapinfo[i].free_block.next = block;
       mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
       /* Mark all my ex-blocks as free */
-      for (it=0; it<mdp->heapinfo[block].free_block.size; it++)
+      for (it=0; it<mdp->heapinfo[block].free_block.size; it++) {
+         if (mdp->heapinfo[block+it].type <0) {
+               fprintf(stderr,"Internal error: Asked to free a block already marked as free (block=%lu it=%d/%lu type=%lu). Please report this bug.\n",
+                               (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].free_block.size,(unsigned long)mdp->heapinfo[block].type);
+               abort();
+         }
          mdp->heapinfo[block+it].type = -1;
+      }
     }
 
     /* Now that the block is linked in, see if we can coalesce it
@@ -131,7 +143,7 @@ void mfree(struct mdesc *mdp, void *ptr)
       if (next != NULL) {
         next->prev = prev->prev;
       }
-      /* pretend the block is used and free it so that it gets properly coalesced with adjacent free blocks */
+      /* pretend that this block is used and free it so that it gets properly coalesced with adjacent free blocks */
       mdp->heapinfo[block].type = 0;
       mdp->heapinfo[block].busy_block.size = 1;
       mdp->heapinfo[block].busy_block.busy_size = 0;
index 3e9bdf2..57fa34e 100644 (file)
@@ -79,6 +79,7 @@ static void *register_morecore(struct mdesc *mdp, size_t size)
 
   /* Check if we need to grow the info table (in a multiplicative manner)  */
   if ((size_t) BLOCK((char *) result + size) > mdp->heapsize) {
+       int it;
 
     newsize = mdp->heapsize;
     while ((size_t) BLOCK((char *) result + size) > newsize)
@@ -93,7 +94,9 @@ static void *register_morecore(struct mdesc *mdp, size_t size)
 
     /* mark the space previously occupied by the block info as free by first marking it
      * as occupied in the regular way, and then freing it */
-    newinfo[BLOCK(oldinfo)].type = 0;
+    for (it=0; it<BLOCKIFY(mdp->heapsize * sizeof(malloc_info)); it++)
+       newinfo[BLOCK(oldinfo)+it].type = 0;
+
     newinfo[BLOCK(oldinfo)].busy_block.size = BLOCKIFY(mdp->heapsize * sizeof(malloc_info));
     newinfo[BLOCK(oldinfo)].busy_block.busy_size = size;
     mfree(mdp, (void *) oldinfo);
@@ -113,6 +116,7 @@ void *mmalloc(xbt_mheap_t mdp, size_t size)
   register size_t i;
   struct list *next;
   register size_t log;
+  int it;
 
   /* Work even if the user was stupid enough to ask a 0-byte block, ie return a valid block that can be realloced or freed
    * glibc malloc does not use this trick but return a constant pointer, but my hack is quicker to implement ;)
@@ -224,7 +228,8 @@ void *mmalloc(xbt_mheap_t mdp, size_t size)
           return (NULL);
         }
         block = BLOCK(result);
-        mdp->heapinfo[block].type = 0;
+        for (it=0;it<blocks;it++)
+               mdp->heapinfo[block+it].type = 0;
         mdp->heapinfo[block].busy_block.size = blocks;
        mdp->heapinfo[block].busy_block.busy_size = size;
         return (result);
@@ -255,7 +260,8 @@ void *mmalloc(xbt_mheap_t mdp, size_t size)
           = mdp->heapindex = mdp->heapinfo[block].free_block.next;
     }
 
-    mdp->heapinfo[block].type = 0;
+    for (it=0;it<blocks;it++)
+       mdp->heapinfo[block+it].type = 0;
     mdp->heapinfo[block].busy_block.size = blocks;
     mdp->heapinfo[block].busy_block.busy_size = size;
   }
index 5928d13..82ab876 100644 (file)
@@ -28,7 +28,9 @@ void *mrealloc(xbt_mheap_t mdp, void *ptr, size_t size)
 
   /* Only keep real realloc and hidden malloc and free to the relevant functions */
   if (size == 0) {
+       fprintf(stderr,"free from realloc...");
     mfree(mdp, ptr);
+    fprintf(stderr,"done\n");
     return mmalloc(mdp, 0);
   } else if (ptr == NULL) {
     return mmalloc(mdp, size);
@@ -67,13 +69,16 @@ void *mrealloc(xbt_mheap_t mdp, void *ptr, size_t size)
        see if we can hold it in place. */
     blocks = BLOCKIFY(size);
     if (blocks < mdp->heapinfo[block].busy_block.size) {
+       int it;
       /* The new size is smaller; return excess memory to the free list. */
       //printf("(%s) return excess memory...",xbt_thread_self_name());
-      mdp->heapinfo[block + blocks].type = 0;
+     for (it= block+blocks; it< mdp->heapinfo[block].busy_block.size ; it++)
+        mdp->heapinfo[it].type = 0;
       mdp->heapinfo[block + blocks].busy_block.size
           = mdp->heapinfo[block].busy_block.size - blocks;
       mdp->heapinfo[block].busy_block.size = blocks;
       mdp->heapinfo[block].busy_block.busy_size = size;
+
       mfree(mdp, ADDRESS(block + blocks));
       result = ptr;
     } else if (blocks == mdp->heapinfo[block].busy_block.size) {