Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
various cosmetics and comments improvements
[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(xbt_mheap_t mdp);
20 static void *register_morecore(xbt_mheap_t mdp, size_t size);
21 static void *align(xbt_mheap_t mdp, size_t size);
22
23 /* Allocation aligned on block boundary */
24 static void *align(struct mdesc *mdp, size_t size)
25 {
26   void *result;
27   unsigned long int adj;
28
29   result = mmorecore(mdp, size);
30
31   /* if this reservation does not fill up the last block of our resa,
32    * complete the reservation by also asking for the full lastest block.
33    *
34    * Also, the returned block is aligned to the end of block (but I've
35    * no fucking idea of why, actually -- http://abstrusegoose.com/432).
36    */
37   adj = RESIDUAL(result, BLOCKSIZE);
38   if (adj != 0) {
39     adj = BLOCKSIZE - adj;
40     mmorecore(mdp, adj);
41     result = (char *) result + adj;
42   }
43   return (result);
44 }
45
46 /* Finish the initialization of the mheap. If we want to inline it
47  * properly, we need to make the align function publicly visible, too  */
48 static int initialize(xbt_mheap_t mdp)
49 {
50   mdp->heapsize = HEAP / BLOCKSIZE;
51   mdp->heapinfo = (malloc_info *)
52       align(mdp, mdp->heapsize * sizeof(malloc_info));
53   if (mdp->heapinfo == NULL) {
54     return (0);
55   }
56   memset((void *) mdp->heapinfo, 0, mdp->heapsize * sizeof(malloc_info));
57   mdp->heapinfo[0].free.size = 0;
58   mdp->heapinfo[0].free.next = mdp->heapinfo[0].free.prev = 0;
59   mdp->heapindex = 0;
60   mdp->heapbase = (void *) mdp->heapinfo;
61   mdp->flags |= MMALLOC_INITIALIZED;
62   return (1);
63 }
64
65 /* Get neatly aligned memory from the low level layers, and register it
66  * into the heap info table as necessary. */
67 static void *register_morecore(struct mdesc *mdp, size_t size)
68 {
69   void *result;
70   malloc_info *newinfo, *oldinfo;
71   size_t newsize;
72
73   result = align(mdp, size);
74   if (result == NULL) {
75     return (NULL);
76   }
77
78   /* Check if we need to grow the info table.  */
79   if ((size_t) BLOCK((char *) result + size) > mdp->heapsize) {
80     newsize = mdp->heapsize;
81     while ((size_t) BLOCK((char *) result + size) > newsize) {
82       newsize *= 2;
83     }
84     newinfo = (malloc_info *) align(mdp, newsize * sizeof(malloc_info));
85     if (newinfo == NULL) {
86       mmorecore(mdp, -size);
87       return (NULL);
88     }
89     memset((void *) newinfo, 0, newsize * sizeof(malloc_info));
90     memcpy((void *) newinfo, (void *) mdp->heapinfo,
91            mdp->heapsize * sizeof(malloc_info));
92     oldinfo = mdp->heapinfo;
93     newinfo[BLOCK(oldinfo)].busy.type = 0;
94     newinfo[BLOCK(oldinfo)].busy.info.block.size
95         = BLOCKIFY(mdp->heapsize * sizeof(malloc_info));
96     newinfo[BLOCK(oldinfo)].busy.info.block.busy_size = size;
97     mdp->heapinfo = newinfo;
98     __mmalloc_free(mdp, (void *) oldinfo);
99     mdp->heapsize = newsize;
100   }
101
102   mdp->heaplimit = BLOCK((char *) result + size);
103   return (result);
104 }
105
106 /* Allocate memory from the heap.  */
107
108 void *mmalloc(xbt_mheap_t mdp, size_t size)
109 {
110   void *result;
111   size_t block, blocks, lastblocks, start;
112   register size_t i;
113   struct list *next;
114   register size_t log;
115
116   /* 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
117    * glibc malloc does not use this trick but return a constant pointer, but my hack is quicker to implement ;)
118    */
119   if (size == 0)
120     size = 1;
121
122 //  printf("(%s) Mallocing %d bytes on %p (default: %p)...",xbt_thread_self_name(),size,mdp,__mmalloc_default_mdp);fflush(stdout);
123
124   if (!(mdp->flags & MMALLOC_INITIALIZED)) {
125     if (!initialize(mdp)) {
126       return (NULL);
127     }
128   }
129
130   if (size < sizeof(struct list)) {
131     size = sizeof(struct list);
132   }
133
134   /* Determine the allocation policy based on the request size.  */
135   if (size <= BLOCKSIZE / 2) {
136     /* Small allocation to receive a fragment of a block.
137        Determine the logarithm to base two of the fragment size. */
138     log = 1;
139     --size;
140     while ((size /= 2) != 0) {
141       ++log;
142     }
143
144     /* Look in the fragment lists for a
145        free fragment of the desired size. */
146     next = mdp->fraghead[log].next;
147     if (next != NULL) {
148       /* There are free fragments of this size.
149          Pop a fragment out of the fragment list and return it.
150          Update the block's nfree and first counters. */
151       result = (void *) next;
152       next->prev->next = next->next;
153       if (next->next != NULL) {
154         next->next->prev = next->prev;
155       }
156       block = BLOCK(result);
157       if (--mdp->heapinfo[block].busy.info.frag.nfree != 0) {
158         mdp->heapinfo[block].busy.info.frag.first =
159             RESIDUAL(next->next, BLOCKSIZE) >> log;
160       }
161
162     } else {
163       /* No free fragments of the desired size, so get a new block
164          and break it into fragments, returning the first.  */
165       //printf("(%s) No free fragment...",xbt_thread_self_name());
166       result = mmalloc(mdp, BLOCKSIZE);
167       //printf("(%s) Fragment: %p...",xbt_thread_self_name(),result);
168       if (result == NULL) {
169         return (NULL);
170       }
171
172       /* Link all fragments but the first into the free list.  */
173       for (i = 1; i < (size_t) (BLOCKSIZE >> log); ++i) {
174         next = (struct list *) ((char *) result + (i << log));
175         next->next = mdp->fraghead[log].next;
176         next->prev = &mdp->fraghead[log];
177         next->prev->next = next;
178         if (next->next != NULL) {
179           next->next->prev = next;
180         }
181       }
182
183       /* Initialize the nfree and first counters for this block.  */
184       block = BLOCK(result);
185       mdp->heapinfo[block].busy.type = log;
186       mdp->heapinfo[block].busy.info.frag.nfree = i - 1;
187       mdp->heapinfo[block].busy.info.frag.first = i - 1;
188     }
189   } else {
190     /* Large allocation to receive one or more blocks.
191        Search the free list in a circle starting at the last place visited.
192        If we loop completely around without finding a large enough
193        space we will have to get more memory from the system.  */
194     blocks = BLOCKIFY(size);
195     start = block = MALLOC_SEARCH_START;
196     while (mdp->heapinfo[block].free.size < blocks) {
197       block = mdp->heapinfo[block].free.next;
198       if (block == start) {
199         /* Need to get more from the system.  Check to see if
200            the new core will be contiguous with the final free
201            block; if so we don't need to get as much.  */
202         block = mdp->heapinfo[0].free.prev;
203         lastblocks = mdp->heapinfo[block].free.size;
204         if (mdp->heaplimit != 0 &&
205             block + lastblocks == mdp->heaplimit &&
206             mmorecore(mdp, 0) == ADDRESS(block + lastblocks) &&
207             (register_morecore(mdp, (blocks - lastblocks) * BLOCKSIZE)) != NULL) {
208           /* Which block we are extending (the `final free
209              block' referred to above) might have changed, if
210              it got combined with a freed info table.  */
211           block = mdp->heapinfo[0].free.prev;
212
213           mdp->heapinfo[block].free.size += (blocks - lastblocks);
214           continue;
215         }
216         result = register_morecore(mdp, blocks * BLOCKSIZE);
217         if (result == NULL) {
218           return (NULL);
219         }
220         block = BLOCK(result);
221         mdp->heapinfo[block].busy.type = 0;
222         mdp->heapinfo[block].busy.info.block.size = blocks;
223         mdp->heapinfo[block].busy.info.block.busy_size = size;
224         return (result);
225       }
226     }
227
228     /* At this point we have found a suitable free list entry.
229        Figure out how to remove what we need from the list. */
230     result = ADDRESS(block);
231     if (mdp->heapinfo[block].free.size > blocks) {
232       /* The block we found has a bit left over,
233          so relink the tail end back into the free list. */
234       mdp->heapinfo[block + blocks].free.size
235           = mdp->heapinfo[block].free.size - blocks;
236       mdp->heapinfo[block + blocks].free.next
237           = mdp->heapinfo[block].free.next;
238       mdp->heapinfo[block + blocks].free.prev
239           = mdp->heapinfo[block].free.prev;
240       mdp->heapinfo[mdp->heapinfo[block].free.prev].free.next
241           = mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev
242           = mdp->heapindex = block + blocks;
243     } else {
244       /* The block exactly matches our requirements,
245          so just remove it from the list. */
246       mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev
247           = mdp->heapinfo[block].free.prev;
248       mdp->heapinfo[mdp->heapinfo[block].free.prev].free.next
249           = mdp->heapindex = mdp->heapinfo[block].free.next;
250     }
251
252     mdp->heapinfo[block].busy.type = 0;
253     mdp->heapinfo[block].busy.info.block.size = blocks;
254     mdp->heapinfo[block].busy.info.block.busy_size = size;
255   }
256   //printf("(%s) Done mallocing. Result is %p\n",xbt_thread_self_name(),result);fflush(stdout);
257   return (result);
258 }