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 /* Find the free cluster previous to this one in the free list.
48 Start searching at the last block referenced; this may benefit
49 programs with locality of allocation. */
53 i = mdp->heapinfo[i].free_block.prev;
57 i = mdp->heapinfo[i].free_block.next;
59 while ((i != 0) && (i < block));
60 i = mdp->heapinfo[i].free_block.prev;
63 /* Determine how to link this block into the free list. */
64 if (block == i + mdp->heapinfo[i].free_block.size) {
66 /* Coalesce this block with its predecessor. */
67 mdp->heapinfo[i].free_block.size += mdp->heapinfo[block].busy_block.size;
68 /* Mark all my ex-blocks as free */
69 for (it=0; it<mdp->heapinfo[block].busy_block.size; it++) {
70 if (mdp->heapinfo[block+it].type <0) {
71 fprintf(stderr,"Internal Error: Asked to free a block already marked as free (block=%lu it=%d type=%lu). Please report this bug.\n",
72 (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].type);
75 mdp->heapinfo[block+it].type = -1;
80 //fprintf(stderr,"Free block %d to %d (as a new chunck)\n",block,block+mdp->heapinfo[block].busy_block.size);
81 /* Really link this block back into the free list. */
82 mdp->heapinfo[block].free_block.size = mdp->heapinfo[block].busy_block.size;
83 mdp->heapinfo[block].free_block.next = mdp->heapinfo[i].free_block.next;
84 mdp->heapinfo[block].free_block.prev = i;
85 mdp->heapinfo[i].free_block.next = block;
86 mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
87 /* Mark all my ex-blocks as free */
88 for (it=0; it<mdp->heapinfo[block].free_block.size; it++) {
89 if (mdp->heapinfo[block+it].type <0) {
90 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",
91 (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].free_block.size,(unsigned long)mdp->heapinfo[block].type);
94 mdp->heapinfo[block+it].type = -1;
98 /* Now that the block is linked in, see if we can coalesce it
99 with its successor (by deleting its successor from the list
100 and adding in its size). */
101 if (block + mdp->heapinfo[block].free_block.size ==
102 mdp->heapinfo[block].free_block.next) {
103 mdp->heapinfo[block].free_block.size
104 += mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.size;
105 mdp->heapinfo[block].free_block.next
106 = mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.next;
107 mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
110 /* Now see if we can return stuff to the system. */
111 /* blocks = mdp -> heapinfo[block].free.size;
112 if (blocks >= FINAL_FREE_BLOCKS && block + blocks == mdp -> heaplimit
113 && mdp -> morecore (mdp, 0) == ADDRESS (block + blocks))
115 register size_t bytes = blocks * BLOCKSIZE;
116 mdp -> heaplimit -= blocks;
117 mdp -> morecore (mdp, -bytes);
118 mdp -> heapinfo[mdp -> heapinfo[block].free.prev].free.next
119 = mdp -> heapinfo[block].free.next;
120 mdp -> heapinfo[mdp -> heapinfo[block].free.next].free.prev
121 = mdp -> heapinfo[block].free.prev;
122 block = mdp -> heapinfo[block].free.prev;
123 mdp -> heapstats.chunks_free--;
124 mdp -> heapstats.bytes_free -= bytes;
127 /* Set the next search to begin at this block. */
128 mdp->heapindex = block;
132 /* Get the address of the first free fragment in this block. */
133 prev = (struct list *)
134 ((char *) ADDRESS(block) +
135 (mdp->heapinfo[block].busy_frag.first << type));
137 if (mdp->heapinfo[block].busy_frag.nfree ==
138 (BLOCKSIZE >> type) - 1) {
139 /* If all fragments of this block are free, remove them
140 from the fragment list and free the whole block. */
142 for (i = 1; i < (size_t) (BLOCKSIZE >> type); ++i) {
145 prev->prev->next = next;
147 next->prev = prev->prev;
149 /* pretend that this block is used and free it so that it gets properly coalesced with adjacent free blocks */
150 mdp->heapinfo[block].type = 0;
151 mdp->heapinfo[block].busy_block.size = 1;
152 mdp->heapinfo[block].busy_block.busy_size = 0;
154 mfree((void *) mdp, (void *) ADDRESS(block));
155 } else if (mdp->heapinfo[block].busy_frag.nfree != 0) {
156 /* If some fragments of this block are free, link this
157 fragment into the fragment list after the first free
158 fragment of this block. */
159 next = (struct list *) ptr;
160 next->next = prev->next;
163 if (next->next != NULL) {
164 next->next->prev = next;
166 ++mdp->heapinfo[block].busy_frag.nfree;
168 /* No fragments of this block were free before the one we just released,
169 * so link this fragment into the fragment list and announce that
170 it is the first free fragment of this block. */
171 prev = (struct list *) ptr;
172 mdp->heapinfo[block].busy_frag.nfree = 1;
173 mdp->heapinfo[block].busy_frag.first =
174 RESIDUAL(ptr, BLOCKSIZE) >> type;
175 prev->next = mdp->fraghead[type].next;
176 prev->prev = &mdp->fraghead[type];
177 prev->prev->next = prev;
178 if (prev->next != NULL) {
179 prev->next->prev = prev;