1 /* Memory allocator `malloc'.
2 Copyright 1990, 1991, 1992 Free Software Foundation
4 Written May 1989 by Mike Haertel.
5 Heavily modified Mar 1992 by Fred Fish for mmap'd version. */
7 /* Copyright (c) 2010. The SimGrid Team.
8 * All rights reserved. */
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. */
13 #include <string.h> /* Prototypes for memcpy, memmove, memset, etc */
15 #include "mmprivate.h"
17 /* Prototypes for local functions */
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);
23 /* Aligned allocation. */
25 static void *align(struct mdesc *mdp, size_t size)
28 unsigned long int adj;
30 result = mdp->morecore(mdp, size);
31 adj = RESIDUAL(result, BLOCKSIZE);
33 adj = BLOCKSIZE - adj;
34 mdp->morecore(mdp, adj);
35 result = (char *) result + adj;
40 /* Set everything up and remember that we have. */
42 static int initialize(struct mdesc *mdp)
44 mdp->heapsize = HEAP / BLOCKSIZE;
45 mdp->heapinfo = (malloc_info *)
46 align(mdp, mdp->heapsize * sizeof(malloc_info));
47 if (mdp->heapinfo == NULL) {
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;
54 mdp->heapbase = (void *) mdp->heapinfo;
55 mdp->flags |= MMALLOC_INITIALIZED;
59 /* Get neatly aligned memory, initializing or
60 growing the heap info table as necessary. */
62 static void *morecore(struct mdesc *mdp, size_t size)
65 malloc_info *newinfo, *oldinfo;
68 result = align(mdp, size);
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) {
79 newinfo = (malloc_info *) align(mdp, newsize * sizeof(malloc_info));
80 if (newinfo == NULL) {
81 mdp->morecore(mdp, -size);
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;
96 mdp->heaplimit = BLOCK((char *) result + size);
100 /* Allocate memory from the heap. */
102 void *mmalloc(void *md, size_t size)
106 size_t block, blocks, lastblocks, start;
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 ;)
118 // printf("(%s) Mallocing %d bytes on %p (default: %p)...",xbt_thread_self_name(),size,mdp,__mmalloc_default_mdp);fflush(stdout);
120 if (mdp->mmalloc_hook != NULL) {
121 return (*mdp->mmalloc_hook) (md, size);
124 if (!(mdp->flags & MMALLOC_INITIALIZED)) {
125 if (!initialize(mdp)) {
130 if (size < sizeof(struct list)) {
131 size = sizeof(struct list);
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. */
140 while ((size /= 2) != 0) {
144 /* Look in the fragment lists for a
145 free fragment of the desired size. */
146 next = mdp->fraghead[log].next;
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;
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;
162 /* Update the statistics. */
163 mdp->heapstats.chunks_used++;
164 mdp->heapstats.bytes_used += 1 << log;
165 mdp->heapstats.chunks_free--;
166 mdp->heapstats.bytes_free -= 1 << log;
168 /* No free fragments of the desired size, so get a new block
169 and break it into fragments, returning the first. */
170 //printf("(%s) No free fragment...",xbt_thread_self_name());
171 result = mmalloc(md, BLOCKSIZE);
172 //printf("(%s) Fragment: %p...",xbt_thread_self_name(),result);
173 if (result == NULL) {
177 /* Link all fragments but the first into the free list. */
178 for (i = 1; i < (size_t) (BLOCKSIZE >> log); ++i) {
179 next = (struct list *) ((char *) result + (i << log));
180 next->next = mdp->fraghead[log].next;
181 next->prev = &mdp->fraghead[log];
182 next->prev->next = next;
183 if (next->next != NULL) {
184 next->next->prev = next;
188 /* Initialize the nfree and first counters for this block. */
189 block = BLOCK(result);
190 mdp->heapinfo[block].busy.type = log;
191 mdp->heapinfo[block].busy.info.frag.nfree = i - 1;
192 mdp->heapinfo[block].busy.info.frag.first = i - 1;
194 mdp->heapstats.chunks_free += (BLOCKSIZE >> log) - 1;
195 mdp->heapstats.bytes_free += BLOCKSIZE - (1 << log);
196 mdp->heapstats.bytes_used -= BLOCKSIZE - (1 << log);
199 /* Large allocation to receive one or more blocks.
200 Search the free list in a circle starting at the last place visited.
201 If we loop completely around without finding a large enough
202 space we will have to get more memory from the system. */
203 blocks = BLOCKIFY(size);
204 start = block = MALLOC_SEARCH_START;
205 while (mdp->heapinfo[block].free.size < blocks) {
206 block = mdp->heapinfo[block].free.next;
207 if (block == start) {
208 /* Need to get more from the system. Check to see if
209 the new core will be contiguous with the final free
210 block; if so we don't need to get as much. */
211 block = mdp->heapinfo[0].free.prev;
212 lastblocks = mdp->heapinfo[block].free.size;
213 if (mdp->heaplimit != 0 &&
214 block + lastblocks == mdp->heaplimit &&
215 mdp->morecore(mdp, 0) == ADDRESS(block + lastblocks) &&
216 (morecore(mdp, (blocks - lastblocks) * BLOCKSIZE)) != NULL) {
217 /* Which block we are extending (the `final free
218 block' referred to above) might have changed, if
219 it got combined with a freed info table. */
220 block = mdp->heapinfo[0].free.prev;
222 mdp->heapinfo[block].free.size += (blocks - lastblocks);
223 mdp->heapstats.bytes_free += (blocks - lastblocks) * BLOCKSIZE;
226 result = morecore(mdp, blocks * BLOCKSIZE);
227 if (result == NULL) {
230 block = BLOCK(result);
231 mdp->heapinfo[block].busy.type = 0;
232 mdp->heapinfo[block].busy.info.size = blocks;
233 mdp->heapstats.chunks_used++;
234 mdp->heapstats.bytes_used += blocks * BLOCKSIZE;
239 /* At this point we have found a suitable free list entry.
240 Figure out how to remove what we need from the list. */
241 result = ADDRESS(block);
242 if (mdp->heapinfo[block].free.size > blocks) {
243 /* The block we found has a bit left over,
244 so relink the tail end back into the free list. */
245 mdp->heapinfo[block + blocks].free.size
246 = mdp->heapinfo[block].free.size - blocks;
247 mdp->heapinfo[block + blocks].free.next
248 = mdp->heapinfo[block].free.next;
249 mdp->heapinfo[block + blocks].free.prev
250 = mdp->heapinfo[block].free.prev;
251 mdp->heapinfo[mdp->heapinfo[block].free.prev].free.next
252 = mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev
253 = mdp->heapindex = block + blocks;
255 /* The block exactly matches our requirements,
256 so just remove it from the list. */
257 mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev
258 = mdp->heapinfo[block].free.prev;
259 mdp->heapinfo[mdp->heapinfo[block].free.prev].free.next
260 = mdp->heapindex = mdp->heapinfo[block].free.next;
261 mdp->heapstats.chunks_free--;
264 mdp->heapinfo[block].busy.type = 0;
265 mdp->heapinfo[block].busy.info.size = blocks;
266 mdp->heapstats.chunks_used++;
267 mdp->heapstats.bytes_used += blocks * BLOCKSIZE;
268 mdp->heapstats.bytes_free -= blocks * BLOCKSIZE;
270 //printf("(%s) Done mallocing. Result is %p\n",xbt_thread_self_name(),result);fflush(stdout);