Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please codacy
[simgrid.git] / src / xbt / mmalloc / mfree.c
1 /* Free a block of memory allocated by `mmalloc'. */
2
3 /* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved.          */
4
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. */
7
8 /* Copyright 1990, 1991, 1992 Free Software Foundation
9
10    Written May 1989 by Mike Haertel.
11    Heavily modified Mar 1992 by Fred Fish.  (fnf@cygnus.com) */
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   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. Give me valid pointers, or give me death!!\n");
38     abort();
39   }
40
41   type = mdp->heapinfo[block].type;
42
43   switch (type) {
44   case MMALLOC_TYPE_HEAPINFO:
45     UNLOCK(mdp);
46     THROWF(system_error, 0, "Asked to free a fragment in a heapinfo block. I'm confused.\n");
47     break;
48
49   case MMALLOC_TYPE_FREE: /* Already free */
50     UNLOCK(mdp);
51     THROWF(system_error, 0, "Asked to free a fragment in a block that is already free. I'm puzzled.\n");
52     break;
53
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;
61
62     if(MC_is_active()){
63       if(mdp->heapinfo[block].busy_block.ignore > 0)
64         MC_remove_ignore_heap(ptr, mdp -> heapinfo[block].busy_block.busy_size);
65     }
66
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.  */
70     i = mdp->heapindex;
71     if (i > block) {
72       while (i > block) {
73         i = mdp->heapinfo[i].free_block.prev;
74       }
75     } else {
76       do {
77         i = mdp->heapinfo[i].free_block.next;
78       }
79       while ((i != 0) && (i < block));
80       i = mdp->heapinfo[i].free_block.prev;
81     }
82
83     /* Determine how to link this block into the free list.  */
84     if (block == i + mdp->heapinfo[i].free_block.size) {
85
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);
93           abort();
94         }
95         mdp->heapinfo[block+it].type = MMALLOC_TYPE_FREE;
96       }
97
98       block = i;
99     } else {
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);
113           abort();
114         }
115         mdp->heapinfo[block+it].type = MMALLOC_TYPE_FREE;
116       }
117     }
118
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--;
130     }
131
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))
136           {
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;
147           } */
148
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;
153     break;
154
155   default:
156     if (type < 0) {
157       fprintf(stderr, "Unkown mmalloc block type.\n");
158       abort();
159     }
160
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;
166
167     frag_nb = RESIDUAL(ptr, BLOCKSIZE) >> type;
168
169     if( mdp->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){
170       UNLOCK(mdp);
171       THROWF(system_error, 0, "Asked to free a fragment that is already free. I'm puzzled\n");
172     }
173
174     if(MC_is_active()){
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]);
177     }
178
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;
182
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]);
188
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;
193
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;
199
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;
204     } else {
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;
211
212       xbt_swag_insert(&mdp->heapinfo[block],&mdp->fraghead[type]);
213     }
214     break;
215   }
216 }