Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Indent include and src using this command:
[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.size
90         = BLOCKIFY(mdp->heapsize * sizeof(malloc_info));
91     mdp->heapinfo = newinfo;
92     __mmalloc_free(mdp, (void *) oldinfo);
93     mdp->heapsize = newsize;
94   }
95
96   mdp->heaplimit = BLOCK((char *) result + size);
97   return (result);
98 }
99
100 /* Allocate memory from the heap.  */
101
102 void *mmalloc(void *md, size_t size)
103 {
104   struct mdesc *mdp;
105   void *result;
106   size_t block, blocks, lastblocks, start;
107   register size_t i;
108   struct list *next;
109   register size_t log;
110
111   /* 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
112    * glibc malloc does not use this trick but return a constant pointer, but my hack is quicker to implement ;)
113    */
114   if (size == 0)
115     size = 1;
116
117   mdp = MD_TO_MDP(md);
118   LOCK(mdp);
119 //  printf("(%s) Mallocing %d bytes on %p (default: %p)...",xbt_thread_self_name(),size,mdp,__mmalloc_default_mdp);fflush(stdout);
120
121
122   if (mdp->mmalloc_hook != NULL) {
123     void *res = ((*mdp->mmalloc_hook) (md, size));
124     UNLOCK(mdp);
125     return res;
126   }
127
128   if (!(mdp->flags & MMALLOC_INITIALIZED)) {
129     if (!initialize(mdp)) {
130       UNLOCK(mdp);
131       return (NULL);
132     }
133   }
134
135   if (size < sizeof(struct list)) {
136     size = sizeof(struct list);
137   }
138
139   /* Determine the allocation policy based on the request size.  */
140   if (size <= BLOCKSIZE / 2) {
141     /* Small allocation to receive a fragment of a block.
142        Determine the logarithm to base two of the fragment size. */
143     log = 1;
144     --size;
145     while ((size /= 2) != 0) {
146       ++log;
147     }
148
149     /* Look in the fragment lists for a
150        free fragment of the desired size. */
151     next = mdp->fraghead[log].next;
152     if (next != NULL) {
153       /* There are free fragments of this size.
154          Pop a fragment out of the fragment list and return it.
155          Update the block's nfree and first counters. */
156       result = (void *) next;
157       next->prev->next = next->next;
158       if (next->next != NULL) {
159         next->next->prev = next->prev;
160       }
161       block = BLOCK(result);
162       if (--mdp->heapinfo[block].busy.info.frag.nfree != 0) {
163         mdp->heapinfo[block].busy.info.frag.first =
164             RESIDUAL(next->next, BLOCKSIZE) >> log;
165       }
166
167       /* Update the statistics.  */
168       mdp->heapstats.chunks_used++;
169       mdp->heapstats.bytes_used += 1 << log;
170       mdp->heapstats.chunks_free--;
171       mdp->heapstats.bytes_free -= 1 << log;
172     } else {
173       /* No free fragments of the desired size, so get a new block
174          and break it into fragments, returning the first.  */
175       UNLOCK(mdp);
176       //printf("(%s) No free fragment...",xbt_thread_self_name());
177       result = mmalloc(md, BLOCKSIZE);
178       //printf("(%s) Fragment: %p...",xbt_thread_self_name(),result);
179       LOCK(mdp);
180       if (result == NULL) {
181         UNLOCK(mdp);
182         return (NULL);
183       }
184
185       /* Link all fragments but the first into the free list.  */
186       for (i = 1; i < (size_t) (BLOCKSIZE >> log); ++i) {
187         next = (struct list *) ((char *) result + (i << log));
188         next->next = mdp->fraghead[log].next;
189         next->prev = &mdp->fraghead[log];
190         next->prev->next = next;
191         if (next->next != NULL) {
192           next->next->prev = next;
193         }
194       }
195
196       /* Initialize the nfree and first counters for this block.  */
197       block = BLOCK(result);
198       mdp->heapinfo[block].busy.type = log;
199       mdp->heapinfo[block].busy.info.frag.nfree = i - 1;
200       mdp->heapinfo[block].busy.info.frag.first = i - 1;
201
202       mdp->heapstats.chunks_free += (BLOCKSIZE >> log) - 1;
203       mdp->heapstats.bytes_free += BLOCKSIZE - (1 << log);
204       mdp->heapstats.bytes_used -= BLOCKSIZE - (1 << log);
205     }
206   } else {
207     /* Large allocation to receive one or more blocks.
208        Search the free list in a circle starting at the last place visited.
209        If we loop completely around without finding a large enough
210        space we will have to get more memory from the system.  */
211     blocks = BLOCKIFY(size);
212     start = block = MALLOC_SEARCH_START;
213     while (mdp->heapinfo[block].free.size < blocks) {
214       block = mdp->heapinfo[block].free.next;
215       if (block == start) {
216         /* Need to get more from the system.  Check to see if
217            the new core will be contiguous with the final free
218            block; if so we don't need to get as much.  */
219         block = mdp->heapinfo[0].free.prev;
220         lastblocks = mdp->heapinfo[block].free.size;
221         if (mdp->heaplimit != 0 &&
222             block + lastblocks == mdp->heaplimit &&
223             mdp->morecore(mdp, 0) == ADDRESS(block + lastblocks) &&
224             (morecore(mdp, (blocks - lastblocks) * BLOCKSIZE)) != NULL) {
225           /* Which block we are extending (the `final free
226              block' referred to above) might have changed, if
227              it got combined with a freed info table.  */
228           block = mdp->heapinfo[0].free.prev;
229
230           mdp->heapinfo[block].free.size += (blocks - lastblocks);
231           mdp->heapstats.bytes_free += (blocks - lastblocks) * BLOCKSIZE;
232           continue;
233         }
234         result = morecore(mdp, blocks * BLOCKSIZE);
235         if (result == NULL) {
236           UNLOCK(mdp);
237           return (NULL);
238         }
239         block = BLOCK(result);
240         mdp->heapinfo[block].busy.type = 0;
241         mdp->heapinfo[block].busy.info.size = blocks;
242         mdp->heapstats.chunks_used++;
243         mdp->heapstats.bytes_used += blocks * BLOCKSIZE;
244         UNLOCK(mdp);
245         return (result);
246       }
247     }
248
249     /* At this point we have found a suitable free list entry.
250        Figure out how to remove what we need from the list. */
251     result = ADDRESS(block);
252     if (mdp->heapinfo[block].free.size > blocks) {
253       /* The block we found has a bit left over,
254          so relink the tail end back into the free list. */
255       mdp->heapinfo[block + blocks].free.size
256           = mdp->heapinfo[block].free.size - blocks;
257       mdp->heapinfo[block + blocks].free.next
258           = mdp->heapinfo[block].free.next;
259       mdp->heapinfo[block + blocks].free.prev
260           = mdp->heapinfo[block].free.prev;
261       mdp->heapinfo[mdp->heapinfo[block].free.prev].free.next
262           = mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev
263           = mdp->heapindex = block + blocks;
264     } else {
265       /* The block exactly matches our requirements,
266          so just remove it from the list. */
267       mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev
268           = mdp->heapinfo[block].free.prev;
269       mdp->heapinfo[mdp->heapinfo[block].free.prev].free.next
270           = mdp->heapindex = mdp->heapinfo[block].free.next;
271       mdp->heapstats.chunks_free--;
272     }
273
274     mdp->heapinfo[block].busy.type = 0;
275     mdp->heapinfo[block].busy.info.size = blocks;
276     mdp->heapstats.chunks_used++;
277     mdp->heapstats.bytes_used += blocks * BLOCKSIZE;
278     mdp->heapstats.bytes_free -= blocks * BLOCKSIZE;
279   }
280   //printf("(%s) Done mallocing. Result is %p\n",xbt_thread_self_name(),result);fflush(stdout);
281   UNLOCK(mdp);
282   return (result);
283 }