Logo AND Algorithmique Numérique Distribuée

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