Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
423d78a5e283fc75899e496543ecdee093f47ffa
[simgrid.git] / src / xbt / mmalloc / mfree.c
1 /* Free a block of memory allocated by `mmalloc'. */
2
3 /* Copyright (c) 2010-2023. 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 "mc/mc.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   size_t frag_nb;
23   size_t i;
24   size_t it;
25
26   if (ptr == NULL)
27     return;
28
29   size_t block = BLOCK(ptr);
30
31   if ((char *) ptr < (char *) mdp->heapbase || block > mdp->heapsize) {
32     if ((char*)ptr <= (char*)mmalloc_preinit_buffer + mmalloc_preinit_buffer_size &&
33         (char*)ptr >= (char*)mmalloc_preinit_buffer)
34       /* This points to the static buffer for fake mallocs done by dlsym before mmalloc initialization, ignore it */
35       return;
36
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   int type = mdp->heapinfo[block].type;
42
43   switch (type) {
44   case MMALLOC_TYPE_HEAPINFO:
45     fprintf(stderr, "Asked to free a fragment in a heapinfo block. I'm confused.\n");
46     abort();
47     break;
48
49   case MMALLOC_TYPE_FREE: /* Already free */
50     fprintf(stderr, "Asked to free a fragment in a block that is already free. I'm puzzled.\n");
51     abort();
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() && mdp->heapinfo[block].busy_block.ignore > 0)
63       MC_unignore_heap(ptr, mdp->heapinfo[block].busy_block.busy_size);
64
65     /* Find the free cluster previous to this one in the free list.
66        Start searching at the last block referenced; this may benefit
67        programs with locality of allocation.  */
68     i = mdp->heapindex;
69     if (i > block) {
70       while (i > block) {
71         i = mdp->heapinfo[i].free_block.prev;
72       }
73     } else {
74       do {
75         i = mdp->heapinfo[i].free_block.next;
76       }
77       while ((i != 0) && (i < block));
78       i = mdp->heapinfo[i].free_block.prev;
79     }
80
81     /* Determine how to link this block into the free list.  */
82     if (block == i + mdp->heapinfo[i].free_block.size) {
83
84       /* Coalesce this block with its predecessor.  */
85       mdp->heapinfo[i].free_block.size += mdp->heapinfo[block].busy_block.size;
86       /* Mark all my ex-blocks as free */
87       for (it=0; it<mdp->heapinfo[block].busy_block.size; it++) {
88         if (mdp->heapinfo[block+it].type < 0) {
89           fprintf(stderr,
90                   "Internal Error: Asked to free a block already marked as free (block=%zu it=%zu type=%d). "
91                   "Please report this bug.\n",
92                   block, it, mdp->heapinfo[block].type);
93           abort();
94         }
95         mdp->heapinfo[block+it].type = MMALLOC_TYPE_FREE;
96       }
97
98       block = i;
99     } else {
100       /* Really link this block back into the free list.  */
101       mdp->heapinfo[block].free_block.size = mdp->heapinfo[block].busy_block.size;
102       mdp->heapinfo[block].free_block.next = mdp->heapinfo[i].free_block.next;
103       mdp->heapinfo[block].free_block.prev = i;
104       mdp->heapinfo[i].free_block.next = block;
105       mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
106       mdp -> heapstats.chunks_free++;
107       /* Mark all my ex-blocks as free */
108       for (it=0; it<mdp->heapinfo[block].free_block.size; it++) {
109         if (mdp->heapinfo[block+it].type <0) {
110           fprintf(stderr,
111                   "Internal error: Asked to free a block already marked as free (block=%zu it=%zu/%zu type=%d). "
112                   "Please report this bug.\n",
113                   block, it, mdp->heapinfo[block].free_block.size, mdp->heapinfo[block].type);
114           abort();
115         }
116         mdp->heapinfo[block+it].type = MMALLOC_TYPE_FREE;
117       }
118     }
119
120     /* Now that the block is linked in, see if we can coalesce it
121        with its successor (by deleting its successor from the list
122        and adding in its size).  */
123     if (block + mdp->heapinfo[block].free_block.size ==
124         mdp->heapinfo[block].free_block.next) {
125       mdp->heapinfo[block].free_block.size
126         += mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.size;
127       mdp->heapinfo[block].free_block.next
128         = mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.next;
129       mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
130       mdp -> heapstats.chunks_free--;
131     }
132
133     /* Now see if we can return stuff to the system.  */
134 #if 0
135           blocks = mdp -> heapinfo[block].free.size;
136           if (blocks >= FINAL_FREE_BLOCKS && block + blocks == mdp -> heaplimit
137           && mdp -> morecore (mdp, 0) == ADDRESS (block + blocks))
138           {
139           register size_t bytes = blocks * BLOCKSIZE;
140           mdp -> heaplimit -= blocks;
141           mdp -> morecore (mdp, -bytes);
142           mdp -> heapinfo[mdp -> heapinfo[block].free.prev].free.next
143           = mdp -> heapinfo[block].free.next;
144           mdp -> heapinfo[mdp -> heapinfo[block].free.next].free.prev
145           = mdp -> heapinfo[block].free.prev;
146           block = mdp -> heapinfo[block].free.prev;
147           mdp -> heapstats.chunks_free--;
148           mdp -> heapstats.bytes_free -= bytes;
149           }
150 #endif
151
152     /* Set the next search to begin at this block.
153        This is probably important to the trick where realloc returns the block to
154        the system before reasking for the same block with a bigger size.  */
155     mdp->heapindex = block;
156     break;
157
158   default:
159     if (type < 0) {
160       fprintf(stderr, "Unknown mmalloc block type.\n");
161       abort();
162     }
163
164     /* Do some of the statistics.  */
165     mdp -> heapstats.chunks_used--;
166     mdp -> heapstats.bytes_used -= 1 << type;
167     mdp -> heapstats.chunks_free++;
168     mdp -> heapstats.bytes_free += 1 << type;
169
170     frag_nb = RESIDUAL(ptr, BLOCKSIZE) >> type;
171
172     if( mdp->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){
173       fprintf(stderr, "Asked to free a fragment that is already free. I'm puzzled\n");
174       abort();
175     }
176
177     if (MC_is_active() && mdp->heapinfo[block].busy_frag.ignore[frag_nb] > 0)
178       MC_unignore_heap(ptr, mdp->heapinfo[block].busy_frag.frag_size[frag_nb]);
179
180     /* Set size used in the fragment to -1 */
181     mdp->heapinfo[block].busy_frag.frag_size[frag_nb] = -1;
182     mdp->heapinfo[block].busy_frag.ignore[frag_nb] = 0;
183
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(mdp, 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 }