*/
typedef struct mdesc *xbt_mheap_t;
-/* Allocate SIZE bytes of memory. */
+/* Allocate SIZE bytes of memory (and memset it to 0). */
extern void *mmalloc(xbt_mheap_t md, size_t size);
+/* Allocate SIZE bytes of memory (and don't mess with it) */
+void *mmalloc_no_memset(xbt_mheap_t mdp, size_t size);
+
/* Re-allocate the previously allocated block in void*, making the new block
SIZE bytes long. */
extern void *mrealloc(xbt_mheap_t md, void *ptr, size_t size);
}
/* Allocate memory from the heap. */
-
-void *mmalloc(xbt_mheap_t mdp, size_t size)
+void *mmalloc(xbt_mheap_t mdp, size_t size) {
+ void *res= mmalloc_no_memset(mdp,size);
+ memset(res,0,size);
+ return res;
+}
+/* Spliting mmalloc this way is mandated by a trick in mrealloc, that gives
+ back the memory of big blocks to the system before reallocating them: we don't
+ want to loose the beginning of the area when this happens */
+void *mmalloc_no_memset(xbt_mheap_t mdp, size_t size)
{
void *result;
size_t block, blocks, lastblocks, start;
mdp -> heapstats.chunks_free--;
mdp -> heapstats.bytes_free -= 1 << log;
- memset(result, 0, requested_size);
-
} else {
/* No free fragments of the desired size, so get a new block
and break it into fragments, returning the first. */
//printf("(%s) No free fragment...",xbt_thread_self_name());
result = mmalloc(mdp, BLOCKSIZE); // does not return NULL
- memset(result, 0, requested_size);
/* Link all fragments but the first into the free list, and mark their requested size to 0. */
block = BLOCK(result);
continue;
}
result = register_morecore(mdp, blocks * BLOCKSIZE);
- memset(result, 0, requested_size);
block = BLOCK(result);
for (it=0;it<blocks;it++){
mdp -> heapstats.bytes_used += blocks * BLOCKSIZE;
mdp -> heapstats.bytes_free -= blocks * BLOCKSIZE;
- memset(result, 0, requested_size);
}
//printf("(%s) Done mallocing. Result is %p\n",xbt_thread_self_name(),result);fflush(stdout);
return (result);
} else {
/* Won't fit, so allocate a new region that will.
- Free the old region first in case there is sufficient
- adjacent free space to grow without moving. */
+ Free the old region first in case there is sufficient adjacent free space to grow without moving.
+ This trick mandates using a specific version of mmalloc that does not memset the memory to 0 after
+ action for obvious reasons. */
blocks = mdp->heapinfo[block].busy_block.size;
/* Prevent free from actually returning memory to the system. */
oldlimit = mdp->heaplimit;
mfree(mdp, ptr);
mdp->heaplimit = oldlimit;
- result = mmalloc(mdp, requested_size);
+ result = mmalloc_no_memset(mdp, requested_size);
if (ptr != result)
memmove(result, ptr, blocks * BLOCKSIZE);
+ /* FIXME: we should memset the end of the recently area */
}
break;