1 /* Free a block of memory allocated by `mmalloc'. */
3 /* Copyright (c) 2010-2022. The SimGrid Team. All rights reserved. */
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. */
8 /* Copyright 1990, 1991, 1992 Free Software Foundation
10 Written May 1989 by Mike Haertel.
11 Heavily modified Mar 1992 by Fred Fish. (fnf@cygnus.com) */
13 #include "mmprivate.h"
16 /* Return memory to the heap.
17 Like `mfree' but don't call a mfree_hook if there is one. */
19 /* Return memory to the heap. */
20 void mfree(struct mdesc *mdp, void *ptr)
29 size_t block = BLOCK(ptr);
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 */
37 fprintf(stderr,"Ouch, this pointer is not mine, I refuse to free it. Give me valid pointers, or give me death!!\n");
41 int type = mdp->heapinfo[block].type;
44 case MMALLOC_TYPE_HEAPINFO:
46 fprintf(stderr, "Asked to free a fragment in a heapinfo block. I'm confused.\n");
50 case MMALLOC_TYPE_FREE: /* Already free */
52 fprintf(stderr, "Asked to free a fragment in a block that is already free. I'm puzzled.\n");
56 case MMALLOC_TYPE_UNFRAGMENTED:
57 /* Get as many statistics as early as we can. */
58 mdp -> heapstats.chunks_used--;
59 mdp -> heapstats.bytes_used -=
60 mdp -> heapinfo[block].busy_block.size * BLOCKSIZE;
61 mdp -> heapstats.bytes_free +=
62 mdp -> heapinfo[block].busy_block.size * BLOCKSIZE;
64 if (MC_is_active() && mdp->heapinfo[block].busy_block.ignore > 0)
65 MC_unignore_heap(ptr, mdp->heapinfo[block].busy_block.busy_size);
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. */
73 i = mdp->heapinfo[i].free_block.prev;
77 i = mdp->heapinfo[i].free_block.next;
79 while ((i != 0) && (i < block));
80 i = mdp->heapinfo[i].free_block.prev;
83 /* Determine how to link this block into the free list. */
84 if (block == i + mdp->heapinfo[i].free_block.size) {
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) {
92 "Internal Error: Asked to free a block already marked as free (block=%zu it=%d type=%d). "
93 "Please report this bug.\n",
94 block, it, mdp->heapinfo[block].type);
97 mdp->heapinfo[block+it].type = MMALLOC_TYPE_FREE;
102 /* Really link this block back into the free list. */
103 mdp->heapinfo[block].free_block.size = mdp->heapinfo[block].busy_block.size;
104 mdp->heapinfo[block].free_block.next = mdp->heapinfo[i].free_block.next;
105 mdp->heapinfo[block].free_block.prev = i;
106 mdp->heapinfo[i].free_block.next = block;
107 mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
108 mdp -> heapstats.chunks_free++;
109 /* Mark all my ex-blocks as free */
110 for (it=0; it<mdp->heapinfo[block].free_block.size; it++) {
111 if (mdp->heapinfo[block+it].type <0) {
113 "Internal error: Asked to free a block already marked as free (block=%zu it=%d/%zu type=%d). "
114 "Please report this bug.\n",
115 block, it, mdp->heapinfo[block].free_block.size, mdp->heapinfo[block].type);
118 mdp->heapinfo[block+it].type = MMALLOC_TYPE_FREE;
122 /* Now that the block is linked in, see if we can coalesce it
123 with its successor (by deleting its successor from the list
124 and adding in its size). */
125 if (block + mdp->heapinfo[block].free_block.size ==
126 mdp->heapinfo[block].free_block.next) {
127 mdp->heapinfo[block].free_block.size
128 += mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.size;
129 mdp->heapinfo[block].free_block.next
130 = mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.next;
131 mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
132 mdp -> heapstats.chunks_free--;
135 /* Now see if we can return stuff to the system. */
137 blocks = mdp -> heapinfo[block].free.size;
138 if (blocks >= FINAL_FREE_BLOCKS && block + blocks == mdp -> heaplimit
139 && mdp -> morecore (mdp, 0) == ADDRESS (block + blocks))
141 register size_t bytes = blocks * BLOCKSIZE;
142 mdp -> heaplimit -= blocks;
143 mdp -> morecore (mdp, -bytes);
144 mdp -> heapinfo[mdp -> heapinfo[block].free.prev].free.next
145 = mdp -> heapinfo[block].free.next;
146 mdp -> heapinfo[mdp -> heapinfo[block].free.next].free.prev
147 = mdp -> heapinfo[block].free.prev;
148 block = mdp -> heapinfo[block].free.prev;
149 mdp -> heapstats.chunks_free--;
150 mdp -> heapstats.bytes_free -= bytes;
154 /* Set the next search to begin at this block.
155 This is probably important to the trick where realloc returns the block to
156 the system before reasking for the same block with a bigger size. */
157 mdp->heapindex = block;
162 fprintf(stderr, "Unknown mmalloc block type.\n");
166 /* Do some of the statistics. */
167 mdp -> heapstats.chunks_used--;
168 mdp -> heapstats.bytes_used -= 1 << type;
169 mdp -> heapstats.chunks_free++;
170 mdp -> heapstats.bytes_free += 1 << type;
172 frag_nb = RESIDUAL(ptr, BLOCKSIZE) >> type;
174 if( mdp->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){
176 fprintf(stderr, "Asked to free a fragment that is already free. I'm puzzled\n");
180 if (MC_is_active() && mdp->heapinfo[block].busy_frag.ignore[frag_nb] > 0)
181 MC_unignore_heap(ptr, mdp->heapinfo[block].busy_frag.frag_size[frag_nb]);
183 /* Set size used in the fragment to -1 */
184 mdp->heapinfo[block].busy_frag.frag_size[frag_nb] = -1;
185 mdp->heapinfo[block].busy_frag.ignore[frag_nb] = 0;
187 if (mdp->heapinfo[block].busy_frag.nfree ==
188 (BLOCKSIZE >> type) - 1) {
189 /* If all fragments of this block are free, remove this block from its swag and free the whole block. */
190 xbt_swag_remove(&mdp->heapinfo[block],&mdp->fraghead[type]);
192 /* pretend that this block is used and free it so that it gets properly coalesced with adjacent free blocks */
193 mdp->heapinfo[block].type = MMALLOC_TYPE_UNFRAGMENTED;
194 mdp->heapinfo[block].busy_block.size = 1;
195 mdp->heapinfo[block].busy_block.busy_size = 0;
197 /* Keep the statistics accurate. */
198 mdp -> heapstats.chunks_used++;
199 mdp -> heapstats.bytes_used += BLOCKSIZE;
200 mdp -> heapstats.chunks_free -= BLOCKSIZE >> type;
201 mdp -> heapstats.bytes_free -= BLOCKSIZE;
203 mfree(mdp, ADDRESS(block));
204 } else if (mdp->heapinfo[block].busy_frag.nfree != 0) {
205 /* If some fragments of this block are free, you know what? I'm already happy. */
206 ++mdp->heapinfo[block].busy_frag.nfree;
208 /* No fragments of this block were free before the one we just released,
209 * so add this block to the swag and announce that
210 it is the first free fragment of this block. */
211 mdp->heapinfo[block].busy_frag.nfree = 1;
212 mdp->heapinfo[block].freehook.prev = NULL;
213 mdp->heapinfo[block].freehook.next = NULL;
215 xbt_swag_insert(&mdp->heapinfo[block],&mdp->fraghead[type]);