Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Indent include and src using this command:
[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 void __mmalloc_free(struct mdesc *mdp, void *ptr)
19 {
20   int type;
21   size_t block;
22   register size_t i;
23   struct list *prev, *next;
24
25   block = BLOCK(ptr);
26
27   if ((char *) ptr < (char *) mdp->heapbase || block > mdp->heapsize) {
28     printf("Ouch, this pointer is not mine. I refuse to free it.\n");
29     return;
30   }
31
32
33   type = mdp->heapinfo[block].busy.type;
34   switch (type) {
35   case 0:
36     /* Get as many statistics as early as we can.  */
37     mdp->heapstats.chunks_used--;
38     mdp->heapstats.bytes_used -=
39         mdp->heapinfo[block].busy.info.size * BLOCKSIZE;
40     mdp->heapstats.bytes_free +=
41         mdp->heapinfo[block].busy.info.size * BLOCKSIZE;
42
43     /* Find the free cluster previous to this one in the free list.
44        Start searching at the last block referenced; this may benefit
45        programs with locality of allocation.  */
46     i = mdp->heapindex;
47     if (i > block) {
48       while (i > block) {
49         i = mdp->heapinfo[i].free.prev;
50       }
51     } else {
52       do {
53         i = mdp->heapinfo[i].free.next;
54       }
55       while ((i != 0) && (i < block));
56       i = mdp->heapinfo[i].free.prev;
57     }
58
59     /* Determine how to link this block into the free list.  */
60     if (block == i + mdp->heapinfo[i].free.size) {
61       /* Coalesce this block with its predecessor.  */
62       mdp->heapinfo[i].free.size += mdp->heapinfo[block].busy.info.size;
63       block = i;
64     } else {
65       /* Really link this block back into the free list.  */
66       mdp->heapinfo[block].free.size = mdp->heapinfo[block].busy.info.size;
67       mdp->heapinfo[block].free.next = mdp->heapinfo[i].free.next;
68       mdp->heapinfo[block].free.prev = i;
69       mdp->heapinfo[i].free.next = block;
70       mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev = block;
71       mdp->heapstats.chunks_free++;
72     }
73
74     /* Now that the block is linked in, see if we can coalesce it
75        with its successor (by deleting its successor from the list
76        and adding in its size).  */
77     if (block + mdp->heapinfo[block].free.size ==
78         mdp->heapinfo[block].free.next) {
79       mdp->heapinfo[block].free.size
80           += mdp->heapinfo[mdp->heapinfo[block].free.next].free.size;
81       mdp->heapinfo[block].free.next
82           = mdp->heapinfo[mdp->heapinfo[block].free.next].free.next;
83       mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev = block;
84       mdp->heapstats.chunks_free--;
85     }
86
87     /* Now see if we can return stuff to the system.  */
88     /*    blocks = mdp -> heapinfo[block].free.size;
89        if (blocks >= FINAL_FREE_BLOCKS && block + blocks == mdp -> heaplimit
90        && mdp -> morecore (mdp, 0) == ADDRESS (block + blocks))
91        {
92        register size_t bytes = blocks * BLOCKSIZE;
93        mdp -> heaplimit -= blocks;
94        mdp -> morecore (mdp, -bytes);
95        mdp -> heapinfo[mdp -> heapinfo[block].free.prev].free.next
96        = mdp -> heapinfo[block].free.next;
97        mdp -> heapinfo[mdp -> heapinfo[block].free.next].free.prev
98        = mdp -> heapinfo[block].free.prev;
99        block = mdp -> heapinfo[block].free.prev;
100        mdp -> heapstats.chunks_free--;
101        mdp -> heapstats.bytes_free -= bytes;
102        } */
103
104     /* Set the next search to begin at this block.  */
105     mdp->heapindex = block;
106     break;
107
108   default:
109     /* Do some of the statistics.  */
110     mdp->heapstats.chunks_used--;
111     mdp->heapstats.bytes_used -= 1 << type;
112     mdp->heapstats.chunks_free++;
113     mdp->heapstats.bytes_free += 1 << type;
114
115     /* Get the address of the first free fragment in this block.  */
116     prev = (struct list *)
117         ((char *) ADDRESS(block) +
118          (mdp->heapinfo[block].busy.info.frag.first << type));
119
120     if (mdp->heapinfo[block].busy.info.frag.nfree ==
121         (BLOCKSIZE >> type) - 1) {
122       /* If all fragments of this block are free, remove them
123          from the fragment list and free the whole block.  */
124       next = prev;
125       for (i = 1; i < (size_t) (BLOCKSIZE >> type); ++i) {
126         next = next->next;
127       }
128       prev->prev->next = next;
129       if (next != NULL) {
130         next->prev = prev->prev;
131       }
132       mdp->heapinfo[block].busy.type = 0;
133       mdp->heapinfo[block].busy.info.size = 1;
134
135       /* Keep the statistics accurate.  */
136       mdp->heapstats.chunks_used++;
137       mdp->heapstats.bytes_used += BLOCKSIZE;
138       mdp->heapstats.chunks_free -= BLOCKSIZE >> type;
139       mdp->heapstats.bytes_free -= BLOCKSIZE;
140
141       mfree((void *) mdp, (void *) ADDRESS(block));
142     } else if (mdp->heapinfo[block].busy.info.frag.nfree != 0) {
143       /* If some fragments of this block are free, link this
144          fragment into the fragment list after the first free
145          fragment of this block. */
146       next = (struct list *) ptr;
147       next->next = prev->next;
148       next->prev = prev;
149       prev->next = next;
150       if (next->next != NULL) {
151         next->next->prev = next;
152       }
153       ++mdp->heapinfo[block].busy.info.frag.nfree;
154     } else {
155       /* No fragments of this block are free, so link this
156          fragment into the fragment list and announce that
157          it is the first free fragment of this block. */
158       prev = (struct list *) ptr;
159       mdp->heapinfo[block].busy.info.frag.nfree = 1;
160       mdp->heapinfo[block].busy.info.frag.first =
161           RESIDUAL(ptr, BLOCKSIZE) >> type;
162       prev->next = mdp->fraghead[type].next;
163       prev->prev = &mdp->fraghead[type];
164       prev->prev->next = prev;
165       if (prev->next != NULL) {
166         prev->next->prev = prev;
167       }
168     }
169     break;
170   }
171 }
172
173 /* Return memory to the heap.  */
174
175 void mfree(void *md, void *ptr)
176 {
177   struct mdesc *mdp;
178   register struct alignlist *l;
179
180   if (ptr != NULL) {
181     mdp = MD_TO_MDP(md);
182     LOCK(mdp);
183     for (l = mdp->aligned_blocks; l != NULL; l = l->next) {
184       if (l->aligned == ptr) {
185         l->aligned = NULL;      /* Mark the slot in the list as free. */
186         ptr = l->exact;
187         break;
188       }
189     }
190     if (mdp->mfree_hook != NULL) {
191       (*mdp->mfree_hook) (mdp, ptr);
192     } else {
193       __mmalloc_free(mdp, ptr);
194     }
195     UNLOCK(mdp);
196   }
197 }