Logo AND Algorithmique Numérique Distribuée

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