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 fprintf(stderr,"Ouch, this pointer is not mine. I refuse to free it. I refuse it to death!!\n");
38 type = mdp->heapinfo[block].type;
41 case -1: /* Already free */
42 fprintf(stderr,"Asked to free a fragment in a block that is already free. I'm puzzled\n");
47 /* Get as many statistics as early as we can. */
48 mdp -> heapstats.chunks_used--;
49 mdp -> heapstats.bytes_used -=
50 mdp -> heapinfo[block].busy_block.size * BLOCKSIZE;
51 mdp -> heapstats.bytes_free +=
52 mdp -> heapinfo[block].busy_block.size * BLOCKSIZE;
54 memset(ptr, 0, mdp -> heapinfo[block].busy_block.size * BLOCKSIZE);
56 /* Find the free cluster previous to this one in the free list.
57 Start searching at the last block referenced; this may benefit
58 programs with locality of allocation. */
62 i = mdp->heapinfo[i].free_block.prev;
66 i = mdp->heapinfo[i].free_block.next;
68 while ((i != 0) && (i < block));
69 i = mdp->heapinfo[i].free_block.prev;
72 /* Determine how to link this block into the free list. */
73 if (block == i + mdp->heapinfo[i].free_block.size) {
75 /* Coalesce this block with its predecessor. */
76 mdp->heapinfo[i].free_block.size += mdp->heapinfo[block].busy_block.size;
77 /* Mark all my ex-blocks as free */
78 for (it=0; it<mdp->heapinfo[block].busy_block.size; it++) {
79 if (mdp->heapinfo[block+it].type <0) {
80 fprintf(stderr,"Internal Error: Asked to free a block already marked as free (block=%lu it=%d type=%lu). Please report this bug.\n",
81 (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].type);
84 mdp->heapinfo[block+it].type = -1;
89 //fprintf(stderr,"Free block %d to %d (as a new chunck)\n",block,block+mdp->heapinfo[block].busy_block.size);
90 /* Really link this block back into the free list. */
91 mdp->heapinfo[block].free_block.size = mdp->heapinfo[block].busy_block.size;
92 mdp->heapinfo[block].free_block.next = mdp->heapinfo[i].free_block.next;
93 mdp->heapinfo[block].free_block.prev = i;
94 mdp->heapinfo[i].free_block.next = block;
95 mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
96 mdp -> heapstats.chunks_free++;
97 /* Mark all my ex-blocks as free */
98 for (it=0; it<mdp->heapinfo[block].free_block.size; it++) {
99 if (mdp->heapinfo[block+it].type <0) {
100 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",
101 (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].free_block.size,(unsigned long)mdp->heapinfo[block].type);
104 mdp->heapinfo[block+it].type = -1;
108 /* Now that the block is linked in, see if we can coalesce it
109 with its successor (by deleting its successor from the list
110 and adding in its size). */
111 if (block + mdp->heapinfo[block].free_block.size ==
112 mdp->heapinfo[block].free_block.next) {
113 mdp->heapinfo[block].free_block.size
114 += mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.size;
115 mdp->heapinfo[block].free_block.next
116 = mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.next;
117 mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
118 mdp -> heapstats.chunks_free--;
121 /* Now see if we can return stuff to the system. */
122 /* blocks = mdp -> heapinfo[block].free.size;
123 if (blocks >= FINAL_FREE_BLOCKS && block + blocks == mdp -> heaplimit
124 && mdp -> morecore (mdp, 0) == ADDRESS (block + blocks))
126 register size_t bytes = blocks * BLOCKSIZE;
127 mdp -> heaplimit -= blocks;
128 mdp -> morecore (mdp, -bytes);
129 mdp -> heapinfo[mdp -> heapinfo[block].free.prev].free.next
130 = mdp -> heapinfo[block].free.next;
131 mdp -> heapinfo[mdp -> heapinfo[block].free.next].free.prev
132 = mdp -> heapinfo[block].free.prev;
133 block = mdp -> heapinfo[block].free.prev;
134 mdp -> heapstats.chunks_free--;
135 mdp -> heapstats.bytes_free -= bytes;
138 /* Set the next search to begin at this block. */
139 mdp->heapindex = block;
143 /* Do some of the statistics. */
144 mdp -> heapstats.chunks_used--;
145 mdp -> heapstats.bytes_used -= 1 << type;
146 mdp -> heapstats.chunks_free++;
147 mdp -> heapstats.bytes_free += 1 << type;
149 memset(ptr, 0, 1 << type);
152 /* Get the address of the first free fragment in this block. */
153 prev = (struct list *)
154 ((char *) ADDRESS(block) +
155 (mdp->heapinfo[block].busy_frag.first << type));
157 if (mdp->heapinfo[block].busy_frag.nfree ==
158 (BLOCKSIZE >> type) - 1) {
159 /* If all fragments of this block are free, remove them
160 from the fragment list and free the whole block. */
162 for (i = 1; i < (size_t) (BLOCKSIZE >> type); ++i) {
165 prev->prev->next = next;
167 next->prev = prev->prev;
169 /* pretend that this block is used and free it so that it gets properly coalesced with adjacent free blocks */
170 mdp->heapinfo[block].type = 0;
171 mdp->heapinfo[block].busy_block.size = 1;
172 mdp->heapinfo[block].busy_block.busy_size = 0;
174 /* Keep the statistics accurate. */
175 mdp -> heapstats.chunks_used++;
176 mdp -> heapstats.bytes_used += BLOCKSIZE;
177 mdp -> heapstats.chunks_free -= BLOCKSIZE >> type;
178 mdp -> heapstats.bytes_free -= BLOCKSIZE;
180 mfree((void *) mdp, (void *) ADDRESS(block));
181 } else if (mdp->heapinfo[block].busy_frag.nfree != 0) {
182 /* If some fragments of this block are free, link this
183 fragment into the fragment list after the first free
184 fragment of this block. */
185 next = (struct list *) ptr;
186 next->next = prev->next;
189 if (next->next != NULL) {
190 next->next->prev = next;
192 ++mdp->heapinfo[block].busy_frag.nfree;
194 /* No fragments of this block were free before the one we just released,
195 * so link this fragment into the fragment list and announce that
196 it is the first free fragment of this block. */
197 prev = (struct list *) ptr;
198 mdp->heapinfo[block].busy_frag.nfree = 1;
199 mdp->heapinfo[block].busy_frag.first =
200 RESIDUAL(ptr, BLOCKSIZE) >> type;
201 prev->next = mdp->fraghead[type].next;
202 prev->prev = &mdp->fraghead[type];
203 prev->prev->next = prev;
204 if (prev->next != NULL) {
205 prev->next->prev = prev;