/* 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 {
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
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;
/* 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)
/* 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);
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 ;)
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);
= 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;
}
/* 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);
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) {