Logo AND Algorithmique Numérique Distribuée

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