Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5d544122bad41bc77ff844a07964ca3fe8fd8d63
[simgrid.git] / src / xbt / mmalloc / mfree.c
1 /* Free a block of memory allocated by `mmalloc'.
2    Copyright 1990, 1991, 1992 Free Software Foundation
3
4    Written May 1989 by Mike Haertel.
5    Heavily modified Mar 1992 by Fred Fish.  (fnf@cygnus.com) */
6
7 /* Copyright (c) 2010. The SimGrid Team.
8  * All rights reserved.                                                     */
9
10 /* This program is free software; you can redistribute it and/or modify it
11  * under the terms of the license (GNU LGPL) which comes with this package. */
12
13 #include "mmprivate.h"
14
15 /* Return memory to the heap.
16    Like `mfree' but don't call a mfree_hook if there is one.  */
17
18 /* Return memory to the heap.  */
19 void mfree(struct mdesc *mdp, void *ptr)
20 {
21   int type;
22   size_t block;
23   register size_t i;
24   struct list *prev, *next;
25   int it;
26
27   if (ptr == NULL)
28           return;
29
30   block = BLOCK(ptr);
31
32   if ((char *) ptr < (char *) mdp->heapbase || block > mdp->heapsize) {
33     fprintf(stderr,"Ouch, this pointer is not mine. I refuse to free it. I refuse it to death!!\n");
34     abort();
35   }
36
37
38   type = mdp->heapinfo[block].type;
39
40   switch (type) {
41   case -1: /* Already free */
42     fprintf(stderr,"Asked to free a fragment in a block that is already free. I'm puzzled\n");
43     abort();
44     break;
45
46   case 0:
47     /* Find the free cluster previous to this one in the free list.
48        Start searching at the last block referenced; this may benefit
49        programs with locality of allocation.  */
50     i = mdp->heapindex;
51     if (i > block) {
52       while (i > block) {
53         i = mdp->heapinfo[i].free_block.prev;
54       }
55     } else {
56       do {
57         i = mdp->heapinfo[i].free_block.next;
58       }
59       while ((i != 0) && (i < block));
60       i = mdp->heapinfo[i].free_block.prev;
61     }
62
63     /* Determine how to link this block into the free list.  */
64     if (block == i + mdp->heapinfo[i].free_block.size) {
65
66       /* Coalesce this block with its predecessor.  */
67       mdp->heapinfo[i].free_block.size += mdp->heapinfo[block].busy_block.size;
68       /* Mark all my ex-blocks as free */
69       for (it=0; it<mdp->heapinfo[block].busy_block.size; it++) {
70           if (mdp->heapinfo[block+it].type <0) {
71                 fprintf(stderr,"Internal Error: Asked to free a block already marked as free (block=%lu it=%d type=%lu). Please report this bug.\n",
72                                 (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].type);
73                 abort();
74           }
75           mdp->heapinfo[block+it].type = -1;
76       }
77
78       block = i;
79     } else {
80       //fprintf(stderr,"Free block %d to %d (as a new chunck)\n",block,block+mdp->heapinfo[block].busy_block.size);
81       /* Really link this block back into the free list.  */
82       mdp->heapinfo[block].free_block.size = mdp->heapinfo[block].busy_block.size;
83       mdp->heapinfo[block].free_block.next = mdp->heapinfo[i].free_block.next;
84       mdp->heapinfo[block].free_block.prev = i;
85       mdp->heapinfo[i].free_block.next = block;
86       mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
87       /* Mark all my ex-blocks as free */
88       for (it=0; it<mdp->heapinfo[block].free_block.size; it++) {
89           if (mdp->heapinfo[block+it].type <0) {
90                 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",
91                                 (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].free_block.size,(unsigned long)mdp->heapinfo[block].type);
92                 abort();
93           }
94           mdp->heapinfo[block+it].type = -1;
95       }
96     }
97
98     /* Now that the block is linked in, see if we can coalesce it
99        with its successor (by deleting its successor from the list
100        and adding in its size).  */
101     if (block + mdp->heapinfo[block].free_block.size ==
102         mdp->heapinfo[block].free_block.next) {
103       mdp->heapinfo[block].free_block.size
104           += mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.size;
105       mdp->heapinfo[block].free_block.next
106           = mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.next;
107       mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
108     }
109
110     /* Now see if we can return stuff to the system.  */
111     /*    blocks = mdp -> heapinfo[block].free.size;
112        if (blocks >= FINAL_FREE_BLOCKS && block + blocks == mdp -> heaplimit
113        && mdp -> morecore (mdp, 0) == ADDRESS (block + blocks))
114        {
115        register size_t bytes = blocks * BLOCKSIZE;
116        mdp -> heaplimit -= blocks;
117        mdp -> morecore (mdp, -bytes);
118        mdp -> heapinfo[mdp -> heapinfo[block].free.prev].free.next
119        = mdp -> heapinfo[block].free.next;
120        mdp -> heapinfo[mdp -> heapinfo[block].free.next].free.prev
121        = mdp -> heapinfo[block].free.prev;
122        block = mdp -> heapinfo[block].free.prev;
123        mdp -> heapstats.chunks_free--;
124        mdp -> heapstats.bytes_free -= bytes;
125        } */
126
127     /* Set the next search to begin at this block.  */
128     mdp->heapindex = block;
129     break;
130
131   default:
132     /* Get the address of the first free fragment in this block.  */
133     prev = (struct list *)
134         ((char *) ADDRESS(block) +
135          (mdp->heapinfo[block].busy_frag.first << type));
136
137     if (mdp->heapinfo[block].busy_frag.nfree ==
138         (BLOCKSIZE >> type) - 1) {
139       /* If all fragments of this block are free, remove them
140          from the fragment list and free the whole block.  */
141       next = prev;
142       for (i = 1; i < (size_t) (BLOCKSIZE >> type); ++i) {
143         next = next->next;
144       }
145       prev->prev->next = next;
146       if (next != NULL) {
147         next->prev = prev->prev;
148       }
149       /* pretend that this block is used and free it so that it gets properly coalesced with adjacent free blocks */
150       mdp->heapinfo[block].type = 0;
151       mdp->heapinfo[block].busy_block.size = 1;
152       mdp->heapinfo[block].busy_block.busy_size = 0;
153
154       mfree((void *) mdp, (void *) ADDRESS(block));
155     } else if (mdp->heapinfo[block].busy_frag.nfree != 0) {
156       /* If some fragments of this block are free, link this
157          fragment into the fragment list after the first free
158          fragment of this block. */
159       next = (struct list *) ptr;
160       next->next = prev->next;
161       next->prev = prev;
162       prev->next = next;
163       if (next->next != NULL) {
164         next->next->prev = next;
165       }
166       ++mdp->heapinfo[block].busy_frag.nfree;
167     } else {
168       /* No fragments of this block were free before the one we just released,
169        * so link this fragment into the fragment list and announce that
170          it is the first free fragment of this block. */
171       prev = (struct list *) ptr;
172       mdp->heapinfo[block].busy_frag.nfree = 1;
173       mdp->heapinfo[block].busy_frag.first =
174           RESIDUAL(ptr, BLOCKSIZE) >> type;
175       prev->next = mdp->fraghead[type].next;
176       prev->prev = &mdp->fraghead[type];
177       prev->prev->next = prev;
178       if (prev->next != NULL) {
179         prev->next->prev = prev;
180       }
181     }
182     break;
183   }
184 }