Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / xbt / mmalloc / mmalloc.c
1 /* Memory allocator `malloc'.
2    Copyright 1990, 1991, 1992 Free Software Foundation
3
4    Written May 1989 by Mike Haertel.
5    Heavily modified Mar 1992 by Fred Fish for mmap'd version. */
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 <string.h>             /* Prototypes for memcpy, memmove, memset, etc */
14 #include <stdio.h>
15 #include "mmprivate.h"
16
17 /* Prototypes for local functions */
18
19 static int initialize(struct mdesc *mdp);
20 static void *morecore(struct mdesc *mdp, size_t size);
21 static void *align(struct mdesc *mdp, size_t size);
22
23 /* Aligned allocation.  */
24
25 static void *align(struct mdesc *mdp, size_t size)
26 {
27   void *result;
28   unsigned long int adj;
29
30   result = mdp->morecore(mdp, size);
31   adj = RESIDUAL(result, BLOCKSIZE);
32   if (adj != 0) {
33     adj = BLOCKSIZE - adj;
34     mdp->morecore(mdp, adj);
35     result = (char *) result + adj;
36   }
37   return (result);
38 }
39
40 /* Set everything up and remember that we have.  */
41
42 static int initialize(struct mdesc *mdp)
43 {
44   mdp->heapsize = HEAP / BLOCKSIZE;
45   mdp->heapinfo = (malloc_info *)
46       align(mdp, mdp->heapsize * sizeof(malloc_info));
47   if (mdp->heapinfo == NULL) {
48     return (0);
49   }
50   memset((void *) mdp->heapinfo, 0, mdp->heapsize * sizeof(malloc_info));
51   mdp->heapinfo[0].free.size = 0;
52   mdp->heapinfo[0].free.next = mdp->heapinfo[0].free.prev = 0;
53   mdp->heapindex = 0;
54   mdp->heapbase = (void *) mdp->heapinfo;
55   mdp->flags |= MMALLOC_INITIALIZED;
56   return (1);
57 }
58
59 /* Get neatly aligned memory, initializing or
60    growing the heap info table as necessary. */
61
62 static void *morecore(struct mdesc *mdp, size_t size)
63 {
64   void *result;
65   malloc_info *newinfo, *oldinfo;
66   size_t newsize;
67
68   result = align(mdp, size);
69   if (result == NULL) {
70     return (NULL);
71   }
72
73   /* Check if we need to grow the info table.  */
74   if ((size_t) BLOCK((char *) result + size) > mdp->heapsize) {
75     newsize = mdp->heapsize;
76     while ((size_t) BLOCK((char *) result + size) > newsize) {
77       newsize *= 2;
78     }
79     newinfo = (malloc_info *) align(mdp, newsize * sizeof(malloc_info));
80     if (newinfo == NULL) {
81       mdp->morecore(mdp, -size);
82       return (NULL);
83     }
84     memset((void *) newinfo, 0, newsize * sizeof(malloc_info));
85     memcpy((void *) newinfo, (void *) mdp->heapinfo,
86            mdp->heapsize * sizeof(malloc_info));
87     oldinfo = mdp->heapinfo;
88     newinfo[BLOCK(oldinfo)].busy.type = 0;
89     newinfo[BLOCK(oldinfo)].busy.info.block.size
90         = BLOCKIFY(mdp->heapsize * sizeof(malloc_info));
91     newinfo[BLOCK(oldinfo)].busy.info.block.busy_size = size;
92     mdp->heapinfo = newinfo;
93     __mmalloc_free(mdp, (void *) oldinfo);
94     mdp->heapsize = newsize;
95   }
96
97   mdp->heaplimit = BLOCK((char *) result + size);
98   return (result);
99 }
100
101 /* Allocate memory from the heap.  */
102
103 void *mmalloc(void *md, size_t size)
104 {
105   struct mdesc *mdp;
106   void *result;
107   size_t block, blocks, lastblocks, start;
108   register size_t i;
109   struct list *next;
110   register size_t log;
111
112   /* Work even if the user was stupid enough to ask a 0-byte block, ie return a valid block that can be realloced or freed
113    * glibc malloc does not use this trick but return a constant pointer, but my hack is quicker to implement ;)
114    */
115   if (size == 0)
116     size = 1;
117
118   mdp = MD_TO_MDP(md);
119 //  printf("(%s) Mallocing %d bytes on %p (default: %p)...",xbt_thread_self_name(),size,mdp,__mmalloc_default_mdp);fflush(stdout);
120
121   if (mdp->mmalloc_hook != NULL) {
122     return mdp->mmalloc_hook(md, size);
123   }
124
125   if (!(mdp->flags & MMALLOC_INITIALIZED)) {
126     if (!initialize(mdp)) {
127       return (NULL);
128     }
129   }
130
131   if (size < sizeof(struct list)) {
132     size = sizeof(struct list);
133   }
134
135   /* Determine the allocation policy based on the request size.  */
136   if (size <= BLOCKSIZE / 2) {
137     /* Small allocation to receive a fragment of a block.
138        Determine the logarithm to base two of the fragment size. */
139     log = 1;
140     --size;
141     while ((size /= 2) != 0) {
142       ++log;
143     }
144
145     /* Look in the fragment lists for a
146        free fragment of the desired size. */
147     next = mdp->fraghead[log].next;
148     if (next != NULL) {
149       /* There are free fragments of this size.
150          Pop a fragment out of the fragment list and return it.
151          Update the block's nfree and first counters. */
152       result = (void *) next;
153       next->prev->next = next->next;
154       if (next->next != NULL) {
155         next->next->prev = next->prev;
156       }
157       block = BLOCK(result);
158       if (--mdp->heapinfo[block].busy.info.frag.nfree != 0) {
159         mdp->heapinfo[block].busy.info.frag.first =
160             RESIDUAL(next->next, BLOCKSIZE) >> log;
161       }
162
163       /* Update the statistics.  */
164       mdp->heapstats.chunks_used++;
165       mdp->heapstats.bytes_used += 1 << log;
166       mdp->heapstats.chunks_free--;
167       mdp->heapstats.bytes_free -= 1 << log;
168     } else {
169       /* No free fragments of the desired size, so get a new block
170          and break it into fragments, returning the first.  */
171       //printf("(%s) No free fragment...",xbt_thread_self_name());
172       result = mmalloc(md, BLOCKSIZE);
173       //printf("(%s) Fragment: %p...",xbt_thread_self_name(),result);
174       if (result == NULL) {
175         return (NULL);
176       }
177
178       /* Link all fragments but the first into the free list.  */
179       for (i = 1; i < (size_t) (BLOCKSIZE >> log); ++i) {
180         next = (struct list *) ((char *) result + (i << log));
181         next->next = mdp->fraghead[log].next;
182         next->prev = &mdp->fraghead[log];
183         next->prev->next = next;
184         if (next->next != NULL) {
185           next->next->prev = next;
186         }
187       }
188
189       /* Initialize the nfree and first counters for this block.  */
190       block = BLOCK(result);
191       mdp->heapinfo[block].busy.type = log;
192       mdp->heapinfo[block].busy.info.frag.nfree = i - 1;
193       mdp->heapinfo[block].busy.info.frag.first = i - 1;
194
195       mdp->heapstats.chunks_free += (BLOCKSIZE >> log) - 1;
196       mdp->heapstats.bytes_free += BLOCKSIZE - (1 << log);
197       mdp->heapstats.bytes_used -= BLOCKSIZE - (1 << log);
198     }
199   } else {
200     /* Large allocation to receive one or more blocks.
201        Search the free list in a circle starting at the last place visited.
202        If we loop completely around without finding a large enough
203        space we will have to get more memory from the system.  */
204     blocks = BLOCKIFY(size);
205     start = block = MALLOC_SEARCH_START;
206     while (mdp->heapinfo[block].free.size < blocks) {
207       block = mdp->heapinfo[block].free.next;
208       if (block == start) {
209         /* Need to get more from the system.  Check to see if
210            the new core will be contiguous with the final free
211            block; if so we don't need to get as much.  */
212         block = mdp->heapinfo[0].free.prev;
213         lastblocks = mdp->heapinfo[block].free.size;
214         if (mdp->heaplimit != 0 &&
215             block + lastblocks == mdp->heaplimit &&
216             mdp->morecore(mdp, 0) == ADDRESS(block + lastblocks) &&
217             (morecore(mdp, (blocks - lastblocks) * BLOCKSIZE)) != NULL) {
218           /* Which block we are extending (the `final free
219              block' referred to above) might have changed, if
220              it got combined with a freed info table.  */
221           block = mdp->heapinfo[0].free.prev;
222
223           mdp->heapinfo[block].free.size += (blocks - lastblocks);
224           mdp->heapstats.bytes_free += (blocks - lastblocks) * BLOCKSIZE;
225           continue;
226         }
227         result = morecore(mdp, blocks * BLOCKSIZE);
228         if (result == NULL) {
229           return (NULL);
230         }
231         block = BLOCK(result);
232         mdp->heapinfo[block].busy.type = 0;
233         mdp->heapinfo[block].busy.info.block.size = blocks;
234         mdp->heapinfo[block].busy.info.block.busy_size = size;
235         mdp->heapstats.chunks_used++;
236         mdp->heapstats.bytes_used += blocks * BLOCKSIZE;
237         return (result);
238       }
239     }
240
241     /* At this point we have found a suitable free list entry.
242        Figure out how to remove what we need from the list. */
243     result = ADDRESS(block);
244     if (mdp->heapinfo[block].free.size > blocks) {
245       /* The block we found has a bit left over,
246          so relink the tail end back into the free list. */
247       mdp->heapinfo[block + blocks].free.size
248           = mdp->heapinfo[block].free.size - blocks;
249       mdp->heapinfo[block + blocks].free.next
250           = mdp->heapinfo[block].free.next;
251       mdp->heapinfo[block + blocks].free.prev
252           = mdp->heapinfo[block].free.prev;
253       mdp->heapinfo[mdp->heapinfo[block].free.prev].free.next
254           = mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev
255           = mdp->heapindex = block + blocks;
256     } else {
257       /* The block exactly matches our requirements,
258          so just remove it from the list. */
259       mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev
260           = mdp->heapinfo[block].free.prev;
261       mdp->heapinfo[mdp->heapinfo[block].free.prev].free.next
262           = mdp->heapindex = mdp->heapinfo[block].free.next;
263       mdp->heapstats.chunks_free--;
264     }
265
266     mdp->heapinfo[block].busy.type = 0;
267     mdp->heapinfo[block].busy.info.block.size = blocks;
268     mdp->heapinfo[block].busy.info.block.busy_size = size;
269     mdp->heapstats.chunks_used++;
270     mdp->heapstats.bytes_used += blocks * BLOCKSIZE;
271     mdp->heapstats.bytes_free -= blocks * BLOCKSIZE;
272   }
273   //printf("(%s) Done mallocing. Result is %p\n",xbt_thread_self_name(),result);fflush(stdout);
274   return (result);
275 }