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)
24 size_t block, frag_nb;
28 mmalloc_paranoia(mdp);
29 // fprintf(stderr,"free(%p)\n",ptr);
36 if ((char *) ptr < (char *) mdp->heapbase || block > mdp->heapsize) {
37 fprintf(stderr,"Ouch, this pointer is not mine, I refuse to free it. Give me valid pointers, or give me death!!\n");
41 type = mdp->heapinfo[block].type;
44 case MMALLOC_TYPE_HEAPINFO:
46 THROWF(system_error, 0, "Asked to free a fragment in a heapinfo block. I'm confused.\n");
49 case MMALLOC_TYPE_FREE: /* Already free */
51 THROWF(system_error, 0, "Asked to free a fragment in a block that is already free. I'm puzzled.\n");
54 case MMALLOC_TYPE_UNFRAGMENTED:
55 /* Get as many statistics as early as we can. */
56 mdp -> heapstats.chunks_used--;
57 mdp -> heapstats.bytes_used -=
58 mdp -> heapinfo[block].busy_block.size * BLOCKSIZE;
59 mdp -> heapstats.bytes_free +=
60 mdp -> heapinfo[block].busy_block.size * BLOCKSIZE;
63 if(mdp->heapinfo[block].busy_block.ignore > 0)
64 MC_remove_ignore_heap(ptr, mdp -> heapinfo[block].busy_block.busy_size);
67 /* Find the free cluster previous to this one in the free list.
68 Start searching at the last block referenced; this may benefit
69 programs with locality of allocation. */
73 i = mdp->heapinfo[i].free_block.prev;
77 i = mdp->heapinfo[i].free_block.next;
79 while ((i != 0) && (i < block));
80 i = mdp->heapinfo[i].free_block.prev;
83 /* Determine how to link this block into the free list. */
84 if (block == i + mdp->heapinfo[i].free_block.size) {
86 /* Coalesce this block with its predecessor. */
87 mdp->heapinfo[i].free_block.size += mdp->heapinfo[block].busy_block.size;
88 /* Mark all my ex-blocks as free */
89 for (it=0; it<mdp->heapinfo[block].busy_block.size; it++) {
90 if (mdp->heapinfo[block+it].type < 0) {
91 fprintf(stderr,"Internal Error: Asked to free a block already marked as free (block=%lu it=%d type=%lu). Please report this bug.\n",
92 (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].type);
95 mdp->heapinfo[block+it].type = MMALLOC_TYPE_FREE;
100 //fprintf(stderr,"Free block %d to %d (as a new chunck)\n",block,block+mdp->heapinfo[block].busy_block.size);
101 /* Really link this block back into the free list. */
102 mdp->heapinfo[block].free_block.size = mdp->heapinfo[block].busy_block.size;
103 mdp->heapinfo[block].free_block.next = mdp->heapinfo[i].free_block.next;
104 mdp->heapinfo[block].free_block.prev = i;
105 mdp->heapinfo[i].free_block.next = block;
106 mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
107 mdp -> heapstats.chunks_free++;
108 /* Mark all my ex-blocks as free */
109 for (it=0; it<mdp->heapinfo[block].free_block.size; it++) {
110 if (mdp->heapinfo[block+it].type <0) {
111 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",
112 (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].free_block.size,(unsigned long)mdp->heapinfo[block].type);
115 mdp->heapinfo[block+it].type = MMALLOC_TYPE_FREE;
119 /* Now that the block is linked in, see if we can coalesce it
120 with its successor (by deleting its successor from the list
121 and adding in its size). */
122 if (block + mdp->heapinfo[block].free_block.size ==
123 mdp->heapinfo[block].free_block.next) {
124 mdp->heapinfo[block].free_block.size
125 += mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.size;
126 mdp->heapinfo[block].free_block.next
127 = mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.next;
128 mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
129 mdp -> heapstats.chunks_free--;
132 /* Now see if we can return stuff to the system. */
133 /* blocks = mdp -> heapinfo[block].free.size;
134 if (blocks >= FINAL_FREE_BLOCKS && block + blocks == mdp -> heaplimit
135 && mdp -> morecore (mdp, 0) == ADDRESS (block + blocks))
137 register size_t bytes = blocks * BLOCKSIZE;
138 mdp -> heaplimit -= blocks;
139 mdp -> morecore (mdp, -bytes);
140 mdp -> heapinfo[mdp -> heapinfo[block].free.prev].free.next
141 = mdp -> heapinfo[block].free.next;
142 mdp -> heapinfo[mdp -> heapinfo[block].free.next].free.prev
143 = mdp -> heapinfo[block].free.prev;
144 block = mdp -> heapinfo[block].free.prev;
145 mdp -> heapstats.chunks_free--;
146 mdp -> heapstats.bytes_free -= bytes;
149 /* Set the next search to begin at this block.
150 This is probably important to the trick where realloc returns the block to
151 the system before reasking for the same block with a bigger size. */
152 mdp->heapindex = block;
157 fprintf(stderr, "Unkown mmalloc block type.\n");
161 /* Do some of the statistics. */
162 mdp -> heapstats.chunks_used--;
163 mdp -> heapstats.bytes_used -= 1 << type;
164 mdp -> heapstats.chunks_free++;
165 mdp -> heapstats.bytes_free += 1 << type;
167 frag_nb = RESIDUAL(ptr, BLOCKSIZE) >> type;
169 if( mdp->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){
171 THROWF(system_error, 0, "Asked to free a fragment that is already free. I'm puzzled\n");
175 if(mdp->heapinfo[block].busy_frag.ignore[frag_nb] > 0)
176 MC_remove_ignore_heap(ptr, mdp->heapinfo[block].busy_frag.frag_size[frag_nb]);
179 /* Set size used in the fragment to -1 */
180 mdp->heapinfo[block].busy_frag.frag_size[frag_nb] = -1;
181 mdp->heapinfo[block].busy_frag.ignore[frag_nb] = 0;
183 // fprintf(stderr,"nfree:%zu capa:%d\n", mdp->heapinfo[block].busy_frag.nfree,(BLOCKSIZE >> type));
184 if (mdp->heapinfo[block].busy_frag.nfree ==
185 (BLOCKSIZE >> type) - 1) {
186 /* If all fragments of this block are free, remove this block from its swag and free the whole block. */
187 xbt_swag_remove(&mdp->heapinfo[block],&mdp->fraghead[type]);
189 /* pretend that this block is used and free it so that it gets properly coalesced with adjacent free blocks */
190 mdp->heapinfo[block].type = MMALLOC_TYPE_UNFRAGMENTED;
191 mdp->heapinfo[block].busy_block.size = 1;
192 mdp->heapinfo[block].busy_block.busy_size = 0;
194 /* Keep the statistics accurate. */
195 mdp -> heapstats.chunks_used++;
196 mdp -> heapstats.bytes_used += BLOCKSIZE;
197 mdp -> heapstats.chunks_free -= BLOCKSIZE >> type;
198 mdp -> heapstats.bytes_free -= BLOCKSIZE;
200 mfree((void *) mdp, (void *) ADDRESS(block));
201 } else if (mdp->heapinfo[block].busy_frag.nfree != 0) {
202 /* If some fragments of this block are free, you know what? I'm already happy. */
203 ++mdp->heapinfo[block].busy_frag.nfree;
205 /* No fragments of this block were free before the one we just released,
206 * so add this block to the swag and announce that
207 it is the first free fragment of this block. */
208 mdp->heapinfo[block].busy_frag.nfree = 1;
209 mdp->heapinfo[block].freehook.prev = NULL;
210 mdp->heapinfo[block].freehook.next = NULL;
212 xbt_swag_insert(&mdp->heapinfo[block],&mdp->fraghead[type]);