Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
I don't need an extra file just to define calloc when it can be inlined that easily
[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           if (mdp->heapinfo[block+it].type <0) {
68                 fprintf(stderr,"Internal Error: Asked to free a block already marked as free (block=%lu it=%d type=%lu). Please report this bug.\n",
69                                 (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].type);
70                 abort();
71           }
72           mdp->heapinfo[block+it].type = -1;
73       }
74
75       block = i;
76     } else {
77       //fprintf(stderr,"Free block %d to %d (as a new chunck)\n",block,block+mdp->heapinfo[block].busy_block.size);
78       /* Really link this block back into the free list.  */
79       mdp->heapinfo[block].free_block.size = mdp->heapinfo[block].busy_block.size;
80       mdp->heapinfo[block].free_block.next = mdp->heapinfo[i].free_block.next;
81       mdp->heapinfo[block].free_block.prev = i;
82       mdp->heapinfo[i].free_block.next = block;
83       mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
84       /* Mark all my ex-blocks as free */
85       for (it=0; it<mdp->heapinfo[block].free_block.size; it++) {
86           if (mdp->heapinfo[block+it].type <0) {
87                 fprintf(stderr,"Internal error: Asked to free a block already marked as free (block=%lu it=%d/%lu type=%lu). Please report this bug.\n",
88                                 (unsigned long)block,it,(unsigned long)mdp->heapinfo[block].free_block.size,(unsigned long)mdp->heapinfo[block].type);
89                 abort();
90           }
91           mdp->heapinfo[block+it].type = -1;
92       }
93     }
94
95     /* Now that the block is linked in, see if we can coalesce it
96        with its successor (by deleting its successor from the list
97        and adding in its size).  */
98     if (block + mdp->heapinfo[block].free_block.size ==
99         mdp->heapinfo[block].free_block.next) {
100       mdp->heapinfo[block].free_block.size
101           += mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.size;
102       mdp->heapinfo[block].free_block.next
103           = mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.next;
104       mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev = block;
105     }
106
107     /* Now see if we can return stuff to the system.  */
108     /*    blocks = mdp -> heapinfo[block].free.size;
109        if (blocks >= FINAL_FREE_BLOCKS && block + blocks == mdp -> heaplimit
110        && mdp -> morecore (mdp, 0) == ADDRESS (block + blocks))
111        {
112        register size_t bytes = blocks * BLOCKSIZE;
113        mdp -> heaplimit -= blocks;
114        mdp -> morecore (mdp, -bytes);
115        mdp -> heapinfo[mdp -> heapinfo[block].free.prev].free.next
116        = mdp -> heapinfo[block].free.next;
117        mdp -> heapinfo[mdp -> heapinfo[block].free.next].free.prev
118        = mdp -> heapinfo[block].free.prev;
119        block = mdp -> heapinfo[block].free.prev;
120        mdp -> heapstats.chunks_free--;
121        mdp -> heapstats.bytes_free -= bytes;
122        } */
123
124     /* Set the next search to begin at this block.  */
125     mdp->heapindex = block;
126     break;
127
128   default:
129     /* Get the address of the first free fragment in this block.  */
130     prev = (struct list *)
131         ((char *) ADDRESS(block) +
132          (mdp->heapinfo[block].busy_frag.first << type));
133
134     if (mdp->heapinfo[block].busy_frag.nfree ==
135         (BLOCKSIZE >> type) - 1) {
136       /* If all fragments of this block are free, remove them
137          from the fragment list and free the whole block.  */
138       next = prev;
139       for (i = 1; i < (size_t) (BLOCKSIZE >> type); ++i) {
140         next = next->next;
141       }
142       prev->prev->next = next;
143       if (next != NULL) {
144         next->prev = prev->prev;
145       }
146       /* pretend that this block is used and free it so that it gets properly coalesced with adjacent free blocks */
147       mdp->heapinfo[block].type = 0;
148       mdp->heapinfo[block].busy_block.size = 1;
149       mdp->heapinfo[block].busy_block.busy_size = 0;
150
151       mfree((void *) mdp, (void *) ADDRESS(block));
152     } else if (mdp->heapinfo[block].busy_frag.nfree != 0) {
153       /* If some fragments of this block are free, link this
154          fragment into the fragment list after the first free
155          fragment of this block. */
156       next = (struct list *) ptr;
157       next->next = prev->next;
158       next->prev = prev;
159       prev->next = next;
160       if (next->next != NULL) {
161         next->next->prev = next;
162       }
163       ++mdp->heapinfo[block].busy_frag.nfree;
164     } else {
165       /* No fragments of this block were free before the one we just released,
166        * so link this fragment into the fragment list and announce that
167          it is the first free fragment of this block. */
168       prev = (struct list *) ptr;
169       mdp->heapinfo[block].busy_frag.nfree = 1;
170       mdp->heapinfo[block].busy_frag.first =
171           RESIDUAL(ptr, BLOCKSIZE) >> type;
172       prev->next = mdp->fraghead[type].next;
173       prev->prev = &mdp->fraghead[type];
174       prev->prev->next = prev;
175       if (prev->next != NULL) {
176         prev->next->prev = prev;
177       }
178     }
179     break;
180   }
181 }