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 void __mmalloc_free(struct mdesc *mdp, void *ptr)
23 struct list *prev, *next;
27 if ((char *) ptr < (char *) mdp->heapbase || block > mdp->heapsize) {
28 printf("Ouch, this pointer is not mine. I refuse to free it.\n");
33 type = mdp->heapinfo[block].busy.type;
36 /* Get as many statistics as early as we can. */
37 mdp->heapstats.chunks_used--;
38 mdp->heapstats.bytes_used -=
39 mdp->heapinfo[block].busy.info.block.size * BLOCKSIZE;
40 mdp->heapstats.bytes_free +=
41 mdp->heapinfo[block].busy.info.block.size * BLOCKSIZE;
43 /* Find the free cluster previous to this one in the free list.
44 Start searching at the last block referenced; this may benefit
45 programs with locality of allocation. */
49 i = mdp->heapinfo[i].free.prev;
53 i = mdp->heapinfo[i].free.next;
55 while ((i != 0) && (i < block));
56 i = mdp->heapinfo[i].free.prev;
59 /* Determine how to link this block into the free list. */
60 if (block == i + mdp->heapinfo[i].free.size) {
61 /* Coalesce this block with its predecessor. */
62 mdp->heapinfo[i].free.size += mdp->heapinfo[block].busy.info.block.size;
65 /* Really link this block back into the free list. */
66 mdp->heapinfo[block].free.size = mdp->heapinfo[block].busy.info.block.size;
67 mdp->heapinfo[block].free.next = mdp->heapinfo[i].free.next;
68 mdp->heapinfo[block].free.prev = i;
69 mdp->heapinfo[i].free.next = block;
70 mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev = block;
71 mdp->heapstats.chunks_free++;
74 /* Now that the block is linked in, see if we can coalesce it
75 with its successor (by deleting its successor from the list
76 and adding in its size). */
77 if (block + mdp->heapinfo[block].free.size ==
78 mdp->heapinfo[block].free.next) {
79 mdp->heapinfo[block].free.size
80 += mdp->heapinfo[mdp->heapinfo[block].free.next].free.size;
81 mdp->heapinfo[block].free.next
82 = mdp->heapinfo[mdp->heapinfo[block].free.next].free.next;
83 mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev = block;
84 mdp->heapstats.chunks_free--;
87 /* Now see if we can return stuff to the system. */
88 /* blocks = mdp -> heapinfo[block].free.size;
89 if (blocks >= FINAL_FREE_BLOCKS && block + blocks == mdp -> heaplimit
90 && mdp -> morecore (mdp, 0) == ADDRESS (block + blocks))
92 register size_t bytes = blocks * BLOCKSIZE;
93 mdp -> heaplimit -= blocks;
94 mdp -> morecore (mdp, -bytes);
95 mdp -> heapinfo[mdp -> heapinfo[block].free.prev].free.next
96 = mdp -> heapinfo[block].free.next;
97 mdp -> heapinfo[mdp -> heapinfo[block].free.next].free.prev
98 = mdp -> heapinfo[block].free.prev;
99 block = mdp -> heapinfo[block].free.prev;
100 mdp -> heapstats.chunks_free--;
101 mdp -> heapstats.bytes_free -= bytes;
104 /* Set the next search to begin at this block. */
105 mdp->heapindex = block;
109 /* Do some of the statistics. */
110 mdp->heapstats.chunks_used--;
111 mdp->heapstats.bytes_used -= 1 << type;
112 mdp->heapstats.chunks_free++;
113 mdp->heapstats.bytes_free += 1 << type;
115 /* Get the address of the first free fragment in this block. */
116 prev = (struct list *)
117 ((char *) ADDRESS(block) +
118 (mdp->heapinfo[block].busy.info.frag.first << type));
120 if (mdp->heapinfo[block].busy.info.frag.nfree ==
121 (BLOCKSIZE >> type) - 1) {
122 /* If all fragments of this block are free, remove them
123 from the fragment list and free the whole block. */
125 for (i = 1; i < (size_t) (BLOCKSIZE >> type); ++i) {
128 prev->prev->next = next;
130 next->prev = prev->prev;
132 mdp->heapinfo[block].busy.type = 0;
133 mdp->heapinfo[block].busy.info.block.size = 1;
134 mdp->heapinfo[block].busy.info.block.busy_size = 0;
136 /* Keep the statistics accurate. */
137 mdp->heapstats.chunks_used++;
138 mdp->heapstats.bytes_used += BLOCKSIZE;
139 mdp->heapstats.chunks_free -= BLOCKSIZE >> type;
140 mdp->heapstats.bytes_free -= BLOCKSIZE;
142 mfree((void *) mdp, (void *) ADDRESS(block));
143 } else if (mdp->heapinfo[block].busy.info.frag.nfree != 0) {
144 /* If some fragments of this block are free, link this
145 fragment into the fragment list after the first free
146 fragment of this block. */
147 next = (struct list *) ptr;
148 next->next = prev->next;
151 if (next->next != NULL) {
152 next->next->prev = next;
154 ++mdp->heapinfo[block].busy.info.frag.nfree;
156 /* No fragments of this block are free, so link this
157 fragment into the fragment list and announce that
158 it is the first free fragment of this block. */
159 prev = (struct list *) ptr;
160 mdp->heapinfo[block].busy.info.frag.nfree = 1;
161 mdp->heapinfo[block].busy.info.frag.first =
162 RESIDUAL(ptr, BLOCKSIZE) >> type;
163 prev->next = mdp->fraghead[type].next;
164 prev->prev = &mdp->fraghead[type];
165 prev->prev->next = prev;
166 if (prev->next != NULL) {
167 prev->next->prev = prev;
174 /* Return memory to the heap. */
176 void mfree(void *md, void *ptr)
179 register struct alignlist *l;
183 for (l = mdp->aligned_blocks; l != NULL; l = l->next) {
184 if (l->aligned == ptr) {
185 l->aligned = NULL; /* Mark the slot in the list as free. */
190 if (mdp->mfree_hook != NULL) {
191 mdp->mfree_hook(mdp, ptr);
193 __mmalloc_free(mdp, ptr);