Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #200 from Takishipp/clear_fct
[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   size_t frag_nb;
24   size_t i;
25   int it;
26
27   mmalloc_paranoia(mdp);
28 //  fprintf(stderr,"free(%p)\n",ptr);
29
30   if (ptr == NULL)
31     return;
32
33   size_t block = BLOCK(ptr);
34
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");
37     abort();
38   }
39
40   int type = mdp->heapinfo[block].type;
41
42   switch (type) {
43   case MMALLOC_TYPE_HEAPINFO:
44     UNLOCK(mdp);
45     THROWF(system_error, 0, "Asked to free a fragment in a heapinfo block. I'm confused.\n");
46     break;
47
48   case MMALLOC_TYPE_FREE: /* Already free */
49     UNLOCK(mdp);
50     THROWF(system_error, 0, "Asked to free a fragment in a block that is already free. I'm puzzled.\n");
51     break;
52
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;
60
61     if (MC_is_active() && mdp->heapinfo[block].busy_block.ignore > 0)
62       MC_unignore_heap(ptr, mdp->heapinfo[block].busy_block.busy_size);
63
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.  */
67     i = mdp->heapindex;
68     if (i > block) {
69       while (i > block) {
70         i = mdp->heapinfo[i].free_block.prev;
71       }
72     } else {
73       do {
74         i = mdp->heapinfo[i].free_block.next;
75       }
76       while ((i != 0) && (i < block));
77       i = mdp->heapinfo[i].free_block.prev;
78     }
79
80     /* Determine how to link this block into the free list.  */
81     if (block == i + mdp->heapinfo[i].free_block.size) {
82
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);
90           abort();
91         }
92         mdp->heapinfo[block+it].type = MMALLOC_TYPE_FREE;
93       }
94
95       block = i;
96     } else {
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);
110           abort();
111         }
112         mdp->heapinfo[block+it].type = MMALLOC_TYPE_FREE;
113       }
114     }
115
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--;
127     }
128
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))
133           {
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;
144           } */
145
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;
150     break;
151
152   default:
153     if (type < 0) {
154       fprintf(stderr, "Unkown mmalloc block type.\n");
155       abort();
156     }
157
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;
163
164     frag_nb = RESIDUAL(ptr, BLOCKSIZE) >> type;
165
166     if( mdp->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){
167       UNLOCK(mdp);
168       THROWF(system_error, 0, "Asked to free a fragment that is already free. I'm puzzled\n");
169     }
170
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]);
173
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;
177
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]);
183
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;
188
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;
194
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;
199     } else {
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;
206
207       xbt_swag_insert(&mdp->heapinfo[block],&mdp->fraghead[type]);
208     }
209     break;
210   }
211 }