1 /* Free a block of memory allocated by `mmalloc'. */
3 /* Copyright (c) 2010-2023. 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"
14 #include "src/mc/mc.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:
45 fprintf(stderr, "Asked to free a fragment in a heapinfo block. I'm confused.\n");
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");
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;
62 if (MC_is_active() && mdp->heapinfo[block].busy_block.ignore > 0)
63 MC_unignore_heap(ptr, mdp->heapinfo[block].busy_block.busy_size);
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. */
71 i = mdp->heapinfo[i].free_block.prev;
75 i = mdp->heapinfo[i].free_block.next;
77 while ((i != 0) && (i < block));
78 i = mdp->heapinfo[i].free_block.prev;
81 /* Determine how to link this block into the free list. */
82 if (block == i + mdp->heapinfo[i].free_block.size) {
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) {
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);
95 mdp->heapinfo[block+it].type = MMALLOC_TYPE_FREE;
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) {
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);
116 mdp->heapinfo[block+it].type = MMALLOC_TYPE_FREE;
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--;
133 /* Now see if we can return stuff to the system. */
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))
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;
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;
160 fprintf(stderr, "Unknown mmalloc block type.\n");
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;
170 frag_nb = RESIDUAL(ptr, BLOCKSIZE) >> type;
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");
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]);
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;
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]);
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;
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;
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;
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;
212 xbt_swag_insert(&mdp->heapinfo[block],&mdp->fraghead[type]);