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. */
80 if ((size_t) BLOCK((char *) result + size) > mdp->heapsize) {
81 newsize = mdp->heapsize;
82 while ((size_t) BLOCK((char *) result + size) > newsize) {
85 newinfo = (malloc_info *) align(mdp, newsize * sizeof(malloc_info));
86 if (newinfo == NULL) {
87 mmorecore(mdp, -size);
90 memset((void *) newinfo, 0, newsize * sizeof(malloc_info));
91 memcpy((void *) newinfo, (void *) mdp->heapinfo,
92 mdp->heapsize * sizeof(malloc_info));
93 oldinfo = mdp->heapinfo;
94 newinfo[BLOCK(oldinfo)].busy.type = 0;
95 newinfo[BLOCK(oldinfo)].busy.info.block.size
96 = BLOCKIFY(mdp->heapsize * sizeof(malloc_info));
97 newinfo[BLOCK(oldinfo)].busy.info.block.busy_size = size;
98 mdp->heapinfo = newinfo;
99 __mmalloc_free(mdp, (void *) oldinfo);
100 mdp->heapsize = newsize;
103 mdp->heaplimit = BLOCK((char *) result + size);
107 /* Allocate memory from the heap. */
109 void *mmalloc(xbt_mheap_t mdp, size_t size)
112 size_t block, blocks, lastblocks, start;
117 /* 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
118 * glibc malloc does not use this trick but return a constant pointer, but my hack is quicker to implement ;)
123 // printf("(%s) Mallocing %d bytes on %p (default: %p)...",xbt_thread_self_name(),size,mdp,__mmalloc_default_mdp);fflush(stdout);
125 if (!(mdp->flags & MMALLOC_INITIALIZED)) {
126 if (!initialize(mdp)) {
131 if (size < sizeof(struct list)) {
132 size = sizeof(struct list);
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. */
141 while ((size /= 2) != 0) {
145 /* Look in the fragment lists for a
146 free fragment of the desired size. */
147 next = mdp->fraghead[log].next;
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;
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;
164 /* No free fragments of the desired size, so get a new block
165 and break it into fragments, returning the first. */
166 //printf("(%s) No free fragment...",xbt_thread_self_name());
167 result = mmalloc(mdp, BLOCKSIZE);
168 //printf("(%s) Fragment: %p...",xbt_thread_self_name(),result);
169 if (result == NULL) {
173 /* Link all fragments but the first into the free list. */
174 for (i = 1; i < (size_t) (BLOCKSIZE >> log); ++i) {
175 next = (struct list *) ((char *) result + (i << log));
176 next->next = mdp->fraghead[log].next;
177 next->prev = &mdp->fraghead[log];
178 next->prev->next = next;
179 if (next->next != NULL) {
180 next->next->prev = next;
184 /* Initialize the nfree and first counters for this block. */
185 block = BLOCK(result);
186 mdp->heapinfo[block].busy.type = log;
187 mdp->heapinfo[block].busy.info.frag.nfree = i - 1;
188 mdp->heapinfo[block].busy.info.frag.first = i - 1;
191 /* Large allocation to receive one or more blocks.
192 Search the free list in a circle starting at the last place visited.
193 If we loop completely around without finding a large enough
194 space we will have to get more memory from the system. */
195 blocks = BLOCKIFY(size);
196 start = block = MALLOC_SEARCH_START;
197 while (mdp->heapinfo[block].free.size < blocks) {
198 block = mdp->heapinfo[block].free.next;
199 if (block == start) {
200 /* Need to get more from the system. Check to see if
201 the new core will be contiguous with the final free
202 block; if so we don't need to get as much. */
203 block = mdp->heapinfo[0].free.prev;
204 lastblocks = mdp->heapinfo[block].free.size;
205 if (mdp->heaplimit != 0 &&
206 block + lastblocks == mdp->heaplimit &&
207 mmorecore(mdp, 0) == ADDRESS(block + lastblocks) &&
208 (register_morecore(mdp, (blocks - lastblocks) * BLOCKSIZE)) != NULL) {
209 /* Which block we are extending (the `final free
210 block' referred to above) might have changed, if
211 it got combined with a freed info table. */
212 block = mdp->heapinfo[0].free.prev;
214 mdp->heapinfo[block].free.size += (blocks - lastblocks);
217 result = register_morecore(mdp, blocks * BLOCKSIZE);
218 if (result == NULL) {
221 block = BLOCK(result);
222 mdp->heapinfo[block].busy.type = 0;
223 mdp->heapinfo[block].busy.info.block.size = blocks;
224 mdp->heapinfo[block].busy.info.block.busy_size = size;
229 /* At this point we have found a suitable free list entry.
230 Figure out how to remove what we need from the list. */
231 result = ADDRESS(block);
232 if (mdp->heapinfo[block].free.size > blocks) {
233 /* The block we found has a bit left over,
234 so relink the tail end back into the free list. */
235 mdp->heapinfo[block + blocks].free.size
236 = mdp->heapinfo[block].free.size - blocks;
237 mdp->heapinfo[block + blocks].free.next
238 = mdp->heapinfo[block].free.next;
239 mdp->heapinfo[block + blocks].free.prev
240 = mdp->heapinfo[block].free.prev;
241 mdp->heapinfo[mdp->heapinfo[block].free.prev].free.next
242 = mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev
243 = mdp->heapindex = block + blocks;
245 /* The block exactly matches our requirements,
246 so just remove it from the list. */
247 mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev
248 = mdp->heapinfo[block].free.prev;
249 mdp->heapinfo[mdp->heapinfo[block].free.prev].free.next
250 = mdp->heapindex = mdp->heapinfo[block].free.next;
253 mdp->heapinfo[block].busy.type = 0;
254 mdp->heapinfo[block].busy.info.block.size = blocks;
255 mdp->heapinfo[block].busy.info.block.busy_size = size;
257 //printf("(%s) Done mallocing. Result is %p\n",xbt_thread_self_name(),result);fflush(stdout);