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(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);
23 /* Allocation aligned on block boundary */
24 static void *align(struct mdesc *mdp, size_t size)
27 unsigned long int adj;
29 result = mmorecore(mdp, size);
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.
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 * but not doing so seems to lead to issues).
38 adj = RESIDUAL(result, BLOCKSIZE);
40 adj = BLOCKSIZE - adj;
42 result = (char *) result + adj;
47 /* Finish the initialization of the mheap. If we want to inline it
48 * properly, we need to make the align function publicly visible, too */
49 static int initialize(xbt_mheap_t mdp)
51 mdp->heapsize = HEAP / BLOCKSIZE;
52 mdp->heapinfo = (malloc_info *)
53 align(mdp, mdp->heapsize * sizeof(malloc_info));
54 if (mdp->heapinfo == NULL) {
57 memset((void *) mdp->heapinfo, 0, mdp->heapsize * sizeof(malloc_info));
58 mdp->heapinfo[0].free.size = 0;
59 mdp->heapinfo[0].free.next = mdp->heapinfo[0].free.prev = 0;
61 mdp->heapbase = (void *) mdp->heapinfo;
62 mdp->flags |= MMALLOC_INITIALIZED;
66 /* Get neatly aligned memory from the low level layers, and register it
67 * into the heap info table as necessary. */
68 static void *register_morecore(struct mdesc *mdp, size_t size)
71 malloc_info *newinfo, *oldinfo;
74 result = align(mdp, size);
79 /* Check if we need to grow the info table (in a multiplicative manner) */
80 if ((size_t) BLOCK((char *) result + size) > mdp->heapsize) {
82 newsize = mdp->heapsize;
83 while ((size_t) BLOCK((char *) result + size) > newsize)
86 /* Copy old info into new location */
87 oldinfo = mdp->heapinfo;
88 newinfo = (malloc_info *) align(mdp, newsize * sizeof(malloc_info));
89 memset(newinfo, 0, newsize * sizeof(malloc_info));
90 memcpy(newinfo, oldinfo, mdp->heapsize * sizeof(malloc_info));
91 mdp->heapinfo = newinfo;
93 /* mark the space previously occupied by the block info as free by first marking it
94 * as occupied in the regular way, and then freing it */
95 newinfo[BLOCK(oldinfo)].busy.type = 0;
96 newinfo[BLOCK(oldinfo)].busy.info.block.size = BLOCKIFY(mdp->heapsize * sizeof(malloc_info));
97 newinfo[BLOCK(oldinfo)].busy.info.block.busy_size = size;
98 __mmalloc_free(mdp, (void *) oldinfo);
99 mdp->heapsize = newsize;
102 mdp->heaplimit = BLOCK((char *) result + size);
106 /* Allocate memory from the heap. */
108 void *mmalloc(xbt_mheap_t mdp, size_t size)
111 size_t block, blocks, lastblocks, start;
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 ;)
122 // printf("(%s) Mallocing %d bytes on %p (default: %p)...",xbt_thread_self_name(),size,mdp,__mmalloc_default_mdp);fflush(stdout);
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;
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) {
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;
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;
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;
213 mdp->heapinfo[block].free.size += (blocks - lastblocks);
216 result = register_morecore(mdp, blocks * BLOCKSIZE);
217 if (result == NULL) {
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;
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;
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;
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;
256 //printf("(%s) Done mallocing. Result is %p\n",xbt_thread_self_name(),result);fflush(stdout);