Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : move function from mc_global in mm_module (compilation error without MC)
[simgrid.git] / src / xbt / mmalloc / mfree.c
1 /* Free a block of memory allocated by `mmalloc'.
2    Copyright 1990, 1991, 1992 Free Software Foundation
3
4    Written May 1989 by Mike Haertel.
5    Heavily modified Mar 1992 by Fred Fish.  (fnf@cygnus.com) */
6
7 /* Copyright (c) 2010. The SimGrid Team.
8  * All rights reserved.                                                     */
9
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. */
12
13 #include "mmprivate.h"
14 #include "xbt/ex.h"
15 #include "mc/mc.h"
16
17 /* Return memory to the heap.
18    Like `mfree' but don't call a mfree_hook if there is one.  */
19
20 /* Return memory to the heap.  */
21 void mfree(struct mdesc *mdp, void *ptr)
22 {
23   int type;
24   size_t block, frag_nb;
25   register size_t i;
26   int it;
27
28   mmalloc_paranoia(mdp);
29 //  fprintf(stderr,"free(%p)\n",ptr);
30
31   if (ptr == NULL)
32     return;
33
34   block = BLOCK(ptr);
35
36   if ((char *) ptr < (char *) mdp->heapbase || block > mdp->heapsize) {
37     fprintf(stderr,"Ouch, this pointer is not mine. I refuse to free it. I refuse it to death!!\n");
38     abort();
39   }
40
41   type = mdp->heapinfo[block].type;
42
43   switch (type) {
44   case -1: /* Already free */
45     UNLOCK(mdp);
46     THROWF(system_error, 0, "Asked to free a fragment in a block that is already free. I'm puzzled\n");
47     break;
48     
49   case 0:
50     /* Get as many statistics as early as we can.  */
51     mdp -> heapstats.chunks_used--;
52     mdp -> heapstats.bytes_used -=
53       mdp -> heapinfo[block].busy_block.size * BLOCKSIZE;
54     mdp -> heapstats.bytes_free +=
55       mdp -> heapinfo[block].busy_block.size * BLOCKSIZE;
56
57     if(mdp->heapinfo[block].busy_block.ignore == 1)
58       remove_ignore_heap(ptr, mdp -> heapinfo[block].busy_block.busy_size);
59
60     /* Find the free cluster previous to this one in the free list.
61        Start searching at the last block referenced; this may benefit
62        programs with locality of allocation.  */
63     i = mdp->heapindex;
64     if (i > block) {
65       while (i > block) {
66         i = mdp->heapinfo[i].free_block.prev;
67       }
68     } else {
69       do {
70         i = mdp->heapinfo[i].free_block.next;
71       }
72       while ((i != 0) && (i < block));
73       i = mdp->heapinfo[i].free_block.prev;
74     }
75
76     /* Determine how to link this block into the free list.  */
77     if (block == i + mdp->heapinfo[i].free_block.size) {
78
79       /* Coalesce this block with its predecessor.  */
80       mdp->heapinfo[i].free_block.size += mdp->heapinfo[block].busy_block.size;
81       /* Mark all my ex-blocks as free */
82       for (it=0; it<mdp->heapinfo[block].busy_block.size; it++) {
83         if (mdp->heapinfo[block+it].type <0) {
84           fprintf(stderr,"Internal Error: Asked to free a block already marked as free (block=%lu it=%d type=%lu). Please report this bug.\n",
85                   (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].type);
86           abort();
87         }
88         mdp->heapinfo[block+it].type = -1;
89         mdp->heapinfo[block+it].busy_block.ignore = 0;
90     
91       }
92
93       block = i;
94     } else {
95       //fprintf(stderr,"Free block %d to %d (as a new chunck)\n",block,block+mdp->heapinfo[block].busy_block.size);
96       /* Really link this block back into the free list.  */
97       mdp->heapinfo[block].free_block.size = mdp->heapinfo[block].busy_block.size;
98       mdp->heapinfo[block].free_block.next = mdp->heapinfo[i].free_block.next;
99       mdp->heapinfo[block].free_block.prev = i;
100       mdp->heapinfo[i].free_block.next = block;
101       mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
102       mdp -> heapstats.chunks_free++;
103       /* Mark all my ex-blocks as free */
104       for (it=0; it<mdp->heapinfo[block].free_block.size; it++) {
105         if (mdp->heapinfo[block+it].type <0) {
106           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",
107                   (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].free_block.size,(unsigned long)mdp->heapinfo[block].type);
108           abort();
109         }
110         mdp->heapinfo[block+it].type = -1;
111         mdp->heapinfo[block+it].busy_block.ignore = 0;
112       }
113     }
114
115     /* Now that the block is linked in, see if we can coalesce it
116        with its successor (by deleting its successor from the list
117        and adding in its size).  */
118     if (block + mdp->heapinfo[block].free_block.size ==
119         mdp->heapinfo[block].free_block.next) {
120       mdp->heapinfo[block].free_block.size
121         += mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.size;
122       mdp->heapinfo[block].free_block.next
123         = mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.next;
124       mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
125       mdp -> heapstats.chunks_free--;
126     }
127
128     /* Now see if we can return stuff to the system.  */
129     /*    blocks = mdp -> heapinfo[block].free.size;
130           if (blocks >= FINAL_FREE_BLOCKS && block + blocks == mdp -> heaplimit
131           && mdp -> morecore (mdp, 0) == ADDRESS (block + blocks))
132           {
133           register size_t bytes = blocks * BLOCKSIZE;
134           mdp -> heaplimit -= blocks;
135           mdp -> morecore (mdp, -bytes);
136           mdp -> heapinfo[mdp -> heapinfo[block].free.prev].free.next
137           = mdp -> heapinfo[block].free.next;
138           mdp -> heapinfo[mdp -> heapinfo[block].free.next].free.prev
139           = mdp -> heapinfo[block].free.prev;
140           block = mdp -> heapinfo[block].free.prev;
141           mdp -> heapstats.chunks_free--;
142           mdp -> heapstats.bytes_free -= bytes;
143           } */
144
145     /* Set the next search to begin at this block.  
146        This is probably important to the trick where realloc returns the block to 
147        the system before reasking for the same block with a bigger size.  */
148     mdp->heapindex = block;
149     break;
150
151   default:
152     /* Do some of the statistics.  */
153     mdp -> heapstats.chunks_used--;
154     mdp -> heapstats.bytes_used -= 1 << type;
155     mdp -> heapstats.chunks_free++;
156     mdp -> heapstats.bytes_free += 1 << type;
157
158     frag_nb = RESIDUAL(ptr, BLOCKSIZE) >> type;
159
160     if( mdp->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){
161       UNLOCK(mdp);
162       THROWF(system_error, 0, "Asked to free a fragment that is already free. I'm puzzled\n");
163     }
164
165     if(mdp->heapinfo[block].busy_frag.ignore[frag_nb] == 1)
166       remove_ignore_heap(ptr, mdp->heapinfo[block].busy_frag.frag_size[frag_nb]);
167
168     /* Set size used in the fragment to -1 */
169     mdp->heapinfo[block].busy_frag.frag_size[frag_nb] = -1;
170     mdp->heapinfo[block].busy_frag.ignore[frag_nb] = 0;
171     
172 //    fprintf(stderr,"nfree:%zu capa:%d\n", mdp->heapinfo[block].busy_frag.nfree,(BLOCKSIZE >> type));
173     if (mdp->heapinfo[block].busy_frag.nfree ==
174         (BLOCKSIZE >> type) - 1) {
175       /* If all fragments of this block are free, remove this block from its swag and free the whole block.  */
176       xbt_swag_remove(&mdp->heapinfo[block],&mdp->fraghead[type]);
177
178       /* pretend that this block is used and free it so that it gets properly coalesced with adjacent free blocks */
179       mdp->heapinfo[block].type = 0;
180       mdp->heapinfo[block].busy_block.size = 1;
181       mdp->heapinfo[block].busy_block.busy_size = 0;
182       mdp->heapinfo[block].busy_block.ignore = 0;
183             
184       /* Keep the statistics accurate.  */
185       mdp -> heapstats.chunks_used++;
186       mdp -> heapstats.bytes_used += BLOCKSIZE;
187       mdp -> heapstats.chunks_free -= BLOCKSIZE >> type;
188       mdp -> heapstats.bytes_free -= BLOCKSIZE;
189       
190       mfree((void *) mdp, (void *) ADDRESS(block));
191     } else if (mdp->heapinfo[block].busy_frag.nfree != 0) {
192       /* If some fragments of this block are free, you know what? I'm already happy. */
193       ++mdp->heapinfo[block].busy_frag.nfree;
194     } else {
195       /* No fragments of this block were free before the one we just released,
196        * so add this block to the swag and announce that
197        it is the first free fragment of this block. */
198       mdp->heapinfo[block].busy_frag.nfree = 1;
199       mdp->heapinfo[block].freehook.prev = NULL;
200       mdp->heapinfo[block].freehook.next = NULL;
201
202       xbt_swag_insert(&mdp->heapinfo[block],&mdp->fraghead[type]);
203     }
204     break;
205   }
206 }
207