Logo AND Algorithmique Numérique Distribuée

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