Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
59cac74b084274e7bf23bef3fd92648dcfef586e
[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     /* Find the free cluster previous to this one in the free list.
37        Start searching at the last block referenced; this may benefit
38        programs with locality of allocation.  */
39     i = mdp->heapindex;
40     if (i > block) {
41       while (i > block) {
42         i = mdp->heapinfo[i].free.prev;
43       }
44     } else {
45       do {
46         i = mdp->heapinfo[i].free.next;
47       }
48       while ((i != 0) && (i < block));
49       i = mdp->heapinfo[i].free.prev;
50     }
51
52     /* Determine how to link this block into the free list.  */
53     if (block == i + mdp->heapinfo[i].free.size) {
54       /* Coalesce this block with its predecessor.  */
55       mdp->heapinfo[i].free.size += mdp->heapinfo[block].busy.info.block.size;
56       block = i;
57     } else {
58       /* Really link this block back into the free list.  */
59       mdp->heapinfo[block].free.size = mdp->heapinfo[block].busy.info.block.size;
60       mdp->heapinfo[block].free.next = mdp->heapinfo[i].free.next;
61       mdp->heapinfo[block].free.prev = i;
62       mdp->heapinfo[i].free.next = block;
63       mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev = block;
64     }
65
66     /* Now that the block is linked in, see if we can coalesce it
67        with its successor (by deleting its successor from the list
68        and adding in its size).  */
69     if (block + mdp->heapinfo[block].free.size ==
70         mdp->heapinfo[block].free.next) {
71       mdp->heapinfo[block].free.size
72           += mdp->heapinfo[mdp->heapinfo[block].free.next].free.size;
73       mdp->heapinfo[block].free.next
74           = mdp->heapinfo[mdp->heapinfo[block].free.next].free.next;
75       mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev = block;
76     }
77
78     /* Now see if we can return stuff to the system.  */
79     /*    blocks = mdp -> heapinfo[block].free.size;
80        if (blocks >= FINAL_FREE_BLOCKS && block + blocks == mdp -> heaplimit
81        && mdp -> morecore (mdp, 0) == ADDRESS (block + blocks))
82        {
83        register size_t bytes = blocks * BLOCKSIZE;
84        mdp -> heaplimit -= blocks;
85        mdp -> morecore (mdp, -bytes);
86        mdp -> heapinfo[mdp -> heapinfo[block].free.prev].free.next
87        = mdp -> heapinfo[block].free.next;
88        mdp -> heapinfo[mdp -> heapinfo[block].free.next].free.prev
89        = mdp -> heapinfo[block].free.prev;
90        block = mdp -> heapinfo[block].free.prev;
91        mdp -> heapstats.chunks_free--;
92        mdp -> heapstats.bytes_free -= bytes;
93        } */
94
95     /* Set the next search to begin at this block.  */
96     mdp->heapindex = block;
97     break;
98
99   default:
100     /* Get the address of the first free fragment in this block.  */
101     prev = (struct list *)
102         ((char *) ADDRESS(block) +
103          (mdp->heapinfo[block].busy.info.frag.first << type));
104
105     if (mdp->heapinfo[block].busy.info.frag.nfree ==
106         (BLOCKSIZE >> type) - 1) {
107       /* If all fragments of this block are free, remove them
108          from the fragment list and free the whole block.  */
109       next = prev;
110       for (i = 1; i < (size_t) (BLOCKSIZE >> type); ++i) {
111         next = next->next;
112       }
113       prev->prev->next = next;
114       if (next != NULL) {
115         next->prev = prev->prev;
116       }
117       mdp->heapinfo[block].busy.type = 0;
118       mdp->heapinfo[block].busy.info.block.size = 1;
119       mdp->heapinfo[block].busy.info.block.busy_size = 0;
120
121       mfree((void *) mdp, (void *) ADDRESS(block));
122     } else if (mdp->heapinfo[block].busy.info.frag.nfree != 0) {
123       /* If some fragments of this block are free, link this
124          fragment into the fragment list after the first free
125          fragment of this block. */
126       next = (struct list *) ptr;
127       next->next = prev->next;
128       next->prev = prev;
129       prev->next = next;
130       if (next->next != NULL) {
131         next->next->prev = next;
132       }
133       ++mdp->heapinfo[block].busy.info.frag.nfree;
134     } else {
135       /* No fragments of this block are free, so link this
136          fragment into the fragment list and announce that
137          it is the first free fragment of this block. */
138       prev = (struct list *) ptr;
139       mdp->heapinfo[block].busy.info.frag.nfree = 1;
140       mdp->heapinfo[block].busy.info.frag.first =
141           RESIDUAL(ptr, BLOCKSIZE) >> type;
142       prev->next = mdp->fraghead[type].next;
143       prev->prev = &mdp->fraghead[type];
144       prev->prev->next = prev;
145       if (prev->next != NULL) {
146         prev->next->prev = prev;
147       }
148     }
149     break;
150   }
151 }
152
153 /* Return memory to the heap.  */
154
155 void mfree(xbt_mheap_t mdp, void *ptr)
156 {
157   if (ptr != NULL)
158     __mmalloc_free(mdp, ptr);
159 }