- /* Look in the fragment lists for a
- free fragment of the desired size. */
- next = mdp->fraghead[log].next;
- if (next != NULL) {
- /* There are free fragments of this size.
- Pop a fragment out of the fragment list and return it.
- Update the block's nfree and first counters. */
- int frag_nb;
- result = (void *) next;
- block = BLOCK(result);
-
- frag_nb = RESIDUAL(result, BLOCKSIZE) >> log;
- mdp->heapinfo[block].busy_frag.frag_size[frag_nb] = requested_size;
- xbt_backtrace_no_malloc(mdp->heapinfo[block].busy_frag.bt[frag_nb],XBT_BACKTRACE_SIZE);
-
- next->prev->next = next->next;
- if (next->next != NULL) {
- next->next->prev = next->prev;
- }
- if (--mdp->heapinfo[block].busy_frag.nfree != 0) {
- mdp->heapinfo[block].busy_frag.first =
- RESIDUAL(next->next, BLOCKSIZE) >> log;
+ /* Look in the fragment lists for a free fragment of the desired size. */
+ if (xbt_swag_size(&mdp->fraghead[log])>0) {
+ /* There are free fragments of this size; Get one of them and prepare to return it.
+ Update the block's nfree and if no other free fragment, get out of the swag. */
+
+ /* search a fragment that I could return as a result */
+ malloc_info *candidate_info = xbt_swag_getFirst(&mdp->fraghead[log]);
+ size_t candidate_block = (candidate_info - &(mdp->heapinfo[0]));
+ size_t candidate_frag;
+ for (candidate_frag=0;candidate_frag<(size_t) (BLOCKSIZE >> log);candidate_frag++)
+ if (candidate_info->busy_frag.frag_size[candidate_frag] == -1)
+ break;
+ xbt_assert(candidate_frag < (size_t) (BLOCKSIZE >> log),
+ "Block %zu was registered as containing free fragments of type %zu, but I can't find any",candidate_block,log);
+
+ result = (void*) (((char*)ADDRESS(candidate_block)) + (candidate_frag << log));
+
+ /* Remove this fragment from the list of free guys */
+ candidate_info->busy_frag.nfree--;
+ if (candidate_info->busy_frag.nfree == 0) {
+ xbt_swag_remove(candidate_info,&mdp->fraghead[log]);