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 /* Find the free cluster previous to this one in the free list.
37 Start searching at the last block referenced; this may benefit
38 programs with locality of allocation. */
42 i = mdp->heapinfo[i].free.prev;
46 i = mdp->heapinfo[i].free.next;
48 while ((i != 0) && (i < block));
49 i = mdp->heapinfo[i].free.prev;
52 /* Determine how to link this block into the free list. */
53 if (block == i + mdp->heapinfo[i].free.size) {
54 /* Coalesce this block with its predecessor. */
55 mdp->heapinfo[i].free.size += mdp->heapinfo[block].busy.info.block.size;
58 /* Really link this block back into the free list. */
59 mdp->heapinfo[block].free.size = mdp->heapinfo[block].busy.info.block.size;
60 mdp->heapinfo[block].free.next = mdp->heapinfo[i].free.next;
61 mdp->heapinfo[block].free.prev = i;
62 mdp->heapinfo[i].free.next = block;
63 mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev = block;
66 /* Now that the block is linked in, see if we can coalesce it
67 with its successor (by deleting its successor from the list
68 and adding in its size). */
69 if (block + mdp->heapinfo[block].free.size ==
70 mdp->heapinfo[block].free.next) {
71 mdp->heapinfo[block].free.size
72 += mdp->heapinfo[mdp->heapinfo[block].free.next].free.size;
73 mdp->heapinfo[block].free.next
74 = mdp->heapinfo[mdp->heapinfo[block].free.next].free.next;
75 mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev = block;
78 /* Now see if we can return stuff to the system. */
79 /* blocks = mdp -> heapinfo[block].free.size;
80 if (blocks >= FINAL_FREE_BLOCKS && block + blocks == mdp -> heaplimit
81 && mdp -> morecore (mdp, 0) == ADDRESS (block + blocks))
83 register size_t bytes = blocks * BLOCKSIZE;
84 mdp -> heaplimit -= blocks;
85 mdp -> morecore (mdp, -bytes);
86 mdp -> heapinfo[mdp -> heapinfo[block].free.prev].free.next
87 = mdp -> heapinfo[block].free.next;
88 mdp -> heapinfo[mdp -> heapinfo[block].free.next].free.prev
89 = mdp -> heapinfo[block].free.prev;
90 block = mdp -> heapinfo[block].free.prev;
91 mdp -> heapstats.chunks_free--;
92 mdp -> heapstats.bytes_free -= bytes;
95 /* Set the next search to begin at this block. */
96 mdp->heapindex = block;
100 /* Get the address of the first free fragment in this block. */
101 prev = (struct list *)
102 ((char *) ADDRESS(block) +
103 (mdp->heapinfo[block].busy.info.frag.first << type));
105 if (mdp->heapinfo[block].busy.info.frag.nfree ==
106 (BLOCKSIZE >> type) - 1) {
107 /* If all fragments of this block are free, remove them
108 from the fragment list and free the whole block. */
110 for (i = 1; i < (size_t) (BLOCKSIZE >> type); ++i) {
113 prev->prev->next = next;
115 next->prev = prev->prev;
117 mdp->heapinfo[block].busy.type = 0;
118 mdp->heapinfo[block].busy.info.block.size = 1;
119 mdp->heapinfo[block].busy.info.block.busy_size = 0;
121 mfree((void *) mdp, (void *) ADDRESS(block));
122 } else if (mdp->heapinfo[block].busy.info.frag.nfree != 0) {
123 /* If some fragments of this block are free, link this
124 fragment into the fragment list after the first free
125 fragment of this block. */
126 next = (struct list *) ptr;
127 next->next = prev->next;
130 if (next->next != NULL) {
131 next->next->prev = next;
133 ++mdp->heapinfo[block].busy.info.frag.nfree;
135 /* No fragments of this block are free, so link this
136 fragment into the fragment list and announce that
137 it is the first free fragment of this block. */
138 prev = (struct list *) ptr;
139 mdp->heapinfo[block].busy.info.frag.nfree = 1;
140 mdp->heapinfo[block].busy.info.frag.first =
141 RESIDUAL(ptr, BLOCKSIZE) >> type;
142 prev->next = mdp->fraghead[type].next;
143 prev->prev = &mdp->fraghead[type];
144 prev->prev->next = prev;
145 if (prev->next != NULL) {
146 prev->next->prev = prev;
153 /* Return memory to the heap. */
155 void mfree(xbt_mheap_t mdp, void *ptr)
158 __mmalloc_free(mdp, ptr);