1 /* Free a block of memory allocated by `mmalloc'. */
3 /* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 /* Copyright 1990, 1991, 1992 Free Software Foundation
10 Written May 1989 by Mike Haertel.
11 Heavily modified Mar 1992 by Fred Fish. (fnf@cygnus.com) */
13 #include "mmprivate.h"
17 /* Return memory to the heap.
18 Like `mfree' but don't call a mfree_hook if there is one. */
20 /* Return memory to the heap. */
21 void mfree(struct mdesc *mdp, void *ptr)
27 mmalloc_paranoia(mdp);
28 // fprintf(stderr,"free(%p)\n",ptr);
33 size_t block = BLOCK(ptr);
35 if ((char *) ptr < (char *) mdp->heapbase || block > mdp->heapsize) {
36 fprintf(stderr,"Ouch, this pointer is not mine, I refuse to free it. Give me valid pointers, or give me death!!\n");
40 int type = mdp->heapinfo[block].type;
43 case MMALLOC_TYPE_HEAPINFO:
45 THROWF(system_error, 0, "Asked to free a fragment in a heapinfo block. I'm confused.\n");
48 case MMALLOC_TYPE_FREE: /* Already free */
50 THROWF(system_error, 0, "Asked to free a fragment in a block that is already free. I'm puzzled.\n");
53 case MMALLOC_TYPE_UNFRAGMENTED:
54 /* Get as many statistics as early as we can. */
55 mdp -> heapstats.chunks_used--;
56 mdp -> heapstats.bytes_used -=
57 mdp -> heapinfo[block].busy_block.size * BLOCKSIZE;
58 mdp -> heapstats.bytes_free +=
59 mdp -> heapinfo[block].busy_block.size * BLOCKSIZE;
61 if (MC_is_active() && mdp->heapinfo[block].busy_block.ignore > 0)
62 MC_unignore_heap(ptr, mdp->heapinfo[block].busy_block.busy_size);
64 /* Find the free cluster previous to this one in the free list.
65 Start searching at the last block referenced; this may benefit
66 programs with locality of allocation. */
70 i = mdp->heapinfo[i].free_block.prev;
74 i = mdp->heapinfo[i].free_block.next;
76 while ((i != 0) && (i < block));
77 i = mdp->heapinfo[i].free_block.prev;
80 /* Determine how to link this block into the free list. */
81 if (block == i + mdp->heapinfo[i].free_block.size) {
83 /* Coalesce this block with its predecessor. */
84 mdp->heapinfo[i].free_block.size += mdp->heapinfo[block].busy_block.size;
85 /* Mark all my ex-blocks as free */
86 for (it=0; it<mdp->heapinfo[block].busy_block.size; it++) {
87 if (mdp->heapinfo[block+it].type < 0) {
88 fprintf(stderr,"Internal Error: Asked to free a block already marked as free (block=%lu it=%d type=%lu). Please report this bug.\n",
89 (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].type);
92 mdp->heapinfo[block+it].type = MMALLOC_TYPE_FREE;
97 //fprintf(stderr,"Free block %d to %d (as a new chunck)\n",block,block+mdp->heapinfo[block].busy_block.size);
98 /* Really link this block back into the free list. */
99 mdp->heapinfo[block].free_block.size = mdp->heapinfo[block].busy_block.size;
100 mdp->heapinfo[block].free_block.next = mdp->heapinfo[i].free_block.next;
101 mdp->heapinfo[block].free_block.prev = i;
102 mdp->heapinfo[i].free_block.next = block;
103 mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
104 mdp -> heapstats.chunks_free++;
105 /* Mark all my ex-blocks as free */
106 for (it=0; it<mdp->heapinfo[block].free_block.size; it++) {
107 if (mdp->heapinfo[block+it].type <0) {
108 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",
109 (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].free_block.size,(unsigned long)mdp->heapinfo[block].type);
112 mdp->heapinfo[block+it].type = MMALLOC_TYPE_FREE;
116 /* Now that the block is linked in, see if we can coalesce it
117 with its successor (by deleting its successor from the list
118 and adding in its size). */
119 if (block + mdp->heapinfo[block].free_block.size ==
120 mdp->heapinfo[block].free_block.next) {
121 mdp->heapinfo[block].free_block.size
122 += mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.size;
123 mdp->heapinfo[block].free_block.next
124 = mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.next;
125 mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
126 mdp -> heapstats.chunks_free--;
129 /* Now see if we can return stuff to the system. */
130 /* blocks = mdp -> heapinfo[block].free.size;
131 if (blocks >= FINAL_FREE_BLOCKS && block + blocks == mdp -> heaplimit
132 && mdp -> morecore (mdp, 0) == ADDRESS (block + blocks))
134 register size_t bytes = blocks * BLOCKSIZE;
135 mdp -> heaplimit -= blocks;
136 mdp -> morecore (mdp, -bytes);
137 mdp -> heapinfo[mdp -> heapinfo[block].free.prev].free.next
138 = mdp -> heapinfo[block].free.next;
139 mdp -> heapinfo[mdp -> heapinfo[block].free.next].free.prev
140 = mdp -> heapinfo[block].free.prev;
141 block = mdp -> heapinfo[block].free.prev;
142 mdp -> heapstats.chunks_free--;
143 mdp -> heapstats.bytes_free -= bytes;
146 /* Set the next search to begin at this block.
147 This is probably important to the trick where realloc returns the block to
148 the system before reasking for the same block with a bigger size. */
149 mdp->heapindex = block;
154 fprintf(stderr, "Unkown mmalloc block type.\n");
158 /* Do some of the statistics. */
159 mdp -> heapstats.chunks_used--;
160 mdp -> heapstats.bytes_used -= 1 << type;
161 mdp -> heapstats.chunks_free++;
162 mdp -> heapstats.bytes_free += 1 << type;
164 frag_nb = RESIDUAL(ptr, BLOCKSIZE) >> type;
166 if( mdp->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){
168 THROWF(system_error, 0, "Asked to free a fragment that is already free. I'm puzzled\n");
171 if (MC_is_active() && mdp->heapinfo[block].busy_frag.ignore[frag_nb] > 0)
172 MC_unignore_heap(ptr, mdp->heapinfo[block].busy_frag.frag_size[frag_nb]);
174 /* Set size used in the fragment to -1 */
175 mdp->heapinfo[block].busy_frag.frag_size[frag_nb] = -1;
176 mdp->heapinfo[block].busy_frag.ignore[frag_nb] = 0;
178 // fprintf(stderr,"nfree:%zu capa:%d\n", mdp->heapinfo[block].busy_frag.nfree,(BLOCKSIZE >> type));
179 if (mdp->heapinfo[block].busy_frag.nfree ==
180 (BLOCKSIZE >> type) - 1) {
181 /* If all fragments of this block are free, remove this block from its swag and free the whole block. */
182 xbt_swag_remove(&mdp->heapinfo[block],&mdp->fraghead[type]);
184 /* pretend that this block is used and free it so that it gets properly coalesced with adjacent free blocks */
185 mdp->heapinfo[block].type = MMALLOC_TYPE_UNFRAGMENTED;
186 mdp->heapinfo[block].busy_block.size = 1;
187 mdp->heapinfo[block].busy_block.busy_size = 0;
189 /* Keep the statistics accurate. */
190 mdp -> heapstats.chunks_used++;
191 mdp -> heapstats.bytes_used += BLOCKSIZE;
192 mdp -> heapstats.chunks_free -= BLOCKSIZE >> type;
193 mdp -> heapstats.bytes_free -= BLOCKSIZE;
195 mfree((void *) mdp, (void *) ADDRESS(block));
196 } else if (mdp->heapinfo[block].busy_frag.nfree != 0) {
197 /* If some fragments of this block are free, you know what? I'm already happy. */
198 ++mdp->heapinfo[block].busy_frag.nfree;
200 /* No fragments of this block were free before the one we just released,
201 * so add this block to the swag and announce that
202 it is the first free fragment of this block. */
203 mdp->heapinfo[block].busy_frag.nfree = 1;
204 mdp->heapinfo[block].freehook.prev = NULL;
205 mdp->heapinfo[block].freehook.next = NULL;
207 xbt_swag_insert(&mdp->heapinfo[block],&mdp->fraghead[type]);