1 /* Free a block of memory allocated by `mmalloc'.
2 Copyright 1990, 1991, 1992 Free Software Foundation
4 Written May 1989 by Mike Haertel.
5 Heavily modified Mar 1992 by Fred Fish. (fnf@cygnus.com) */
7 /* Copyright (c) 2010. The SimGrid Team.
8 * All rights reserved. */
10 /* This program is free software; you can redistribute it and/or modify it
11 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include "mmprivate.h"
15 /* Return memory to the heap.
16 Like `mfree' but don't call a mfree_hook if there is one. */
18 /* Return memory to the heap. */
19 void mfree(struct mdesc *mdp, void *ptr)
24 struct list *prev, *next;
32 if ((char *) ptr < (char *) mdp->heapbase || block > mdp->heapsize) {
33 printf("Ouch, this pointer is not mine. I refuse to free it.\n");
38 type = mdp->heapinfo[block].type;
40 THROWF(arg_error,0,"Asked to free a fragment in a block that is already free. I'm puzzled");
44 /* Find the free cluster previous to this one in the free list.
45 Start searching at the last block referenced; this may benefit
46 programs with locality of allocation. */
50 i = mdp->heapinfo[i].free_block.prev;
54 i = mdp->heapinfo[i].free_block.next;
56 while ((i != 0) && (i < block));
57 i = mdp->heapinfo[i].free_block.prev;
60 /* Determine how to link this block into the free list. */
61 if (block == i + mdp->heapinfo[i].free_block.size) {
63 /* Coalesce this block with its predecessor. */
64 mdp->heapinfo[i].free_block.size += mdp->heapinfo[block].busy_block.size;
65 /* Mark all my ex-blocks as free */
66 for (it=0; it<mdp->heapinfo[block].busy_block.size; it++) {
67 if (mdp->heapinfo[block+it].type <0) {
68 fprintf(stderr,"Internal Error: Asked to free a block already marked as free (block=%lu it=%d type=%lu). Please report this bug.\n",
69 (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].type);
72 mdp->heapinfo[block+it].type = -1;
77 //fprintf(stderr,"Free block %d to %d (as a new chunck)\n",block,block+mdp->heapinfo[block].busy_block.size);
78 /* Really link this block back into the free list. */
79 mdp->heapinfo[block].free_block.size = mdp->heapinfo[block].busy_block.size;
80 mdp->heapinfo[block].free_block.next = mdp->heapinfo[i].free_block.next;
81 mdp->heapinfo[block].free_block.prev = i;
82 mdp->heapinfo[i].free_block.next = block;
83 mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
84 /* Mark all my ex-blocks as free */
85 for (it=0; it<mdp->heapinfo[block].free_block.size; it++) {
86 if (mdp->heapinfo[block+it].type <0) {
87 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",
88 (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].free_block.size,(unsigned long)mdp->heapinfo[block].type);
91 mdp->heapinfo[block+it].type = -1;
95 /* Now that the block is linked in, see if we can coalesce it
96 with its successor (by deleting its successor from the list
97 and adding in its size). */
98 if (block + mdp->heapinfo[block].free_block.size ==
99 mdp->heapinfo[block].free_block.next) {
100 mdp->heapinfo[block].free_block.size
101 += mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.size;
102 mdp->heapinfo[block].free_block.next
103 = mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.next;
104 mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
107 /* Now see if we can return stuff to the system. */
108 /* blocks = mdp -> heapinfo[block].free.size;
109 if (blocks >= FINAL_FREE_BLOCKS && block + blocks == mdp -> heaplimit
110 && mdp -> morecore (mdp, 0) == ADDRESS (block + blocks))
112 register size_t bytes = blocks * BLOCKSIZE;
113 mdp -> heaplimit -= blocks;
114 mdp -> morecore (mdp, -bytes);
115 mdp -> heapinfo[mdp -> heapinfo[block].free.prev].free.next
116 = mdp -> heapinfo[block].free.next;
117 mdp -> heapinfo[mdp -> heapinfo[block].free.next].free.prev
118 = mdp -> heapinfo[block].free.prev;
119 block = mdp -> heapinfo[block].free.prev;
120 mdp -> heapstats.chunks_free--;
121 mdp -> heapstats.bytes_free -= bytes;
124 /* Set the next search to begin at this block. */
125 mdp->heapindex = block;
129 /* Get the address of the first free fragment in this block. */
130 prev = (struct list *)
131 ((char *) ADDRESS(block) +
132 (mdp->heapinfo[block].busy_frag.first << type));
134 if (mdp->heapinfo[block].busy_frag.nfree ==
135 (BLOCKSIZE >> type) - 1) {
136 /* If all fragments of this block are free, remove them
137 from the fragment list and free the whole block. */
139 for (i = 1; i < (size_t) (BLOCKSIZE >> type); ++i) {
142 prev->prev->next = next;
144 next->prev = prev->prev;
146 /* pretend that this block is used and free it so that it gets properly coalesced with adjacent free blocks */
147 mdp->heapinfo[block].type = 0;
148 mdp->heapinfo[block].busy_block.size = 1;
149 mdp->heapinfo[block].busy_block.busy_size = 0;
151 mfree((void *) mdp, (void *) ADDRESS(block));
152 } else if (mdp->heapinfo[block].busy_frag.nfree != 0) {
153 /* If some fragments of this block are free, link this
154 fragment into the fragment list after the first free
155 fragment of this block. */
156 next = (struct list *) ptr;
157 next->next = prev->next;
160 if (next->next != NULL) {
161 next->next->prev = next;
163 ++mdp->heapinfo[block].busy_frag.nfree;
165 /* No fragments of this block were free before the one we just released,
166 * so link this fragment into the fragment list and announce that
167 it is the first free fragment of this block. */
168 prev = (struct list *) ptr;
169 mdp->heapinfo[block].busy_frag.nfree = 1;
170 mdp->heapinfo[block].busy_frag.first =
171 RESIDUAL(ptr, BLOCKSIZE) >> type;
172 prev->next = mdp->fraghead[type].next;
173 prev->prev = &mdp->fraghead[type];
174 prev->prev->next = prev;
175 if (prev->next != NULL) {
176 prev->next->prev = prev;