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 void 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.
25 * It never returns NULL, but dies verbosely on error.
27 static void *align(struct mdesc *mdp, size_t size)
30 unsigned long int adj;
32 result = mmorecore(mdp, size);
34 /* if this reservation does not fill up the last block of our resa,
35 * complete the reservation by also asking for the full lastest block.
37 * Also, the returned block is aligned to the end of block (but I've
38 * no fucking idea of why, actually -- http://abstrusegoose.com/432 --
39 * but not doing so seems to lead to issues).
41 adj = RESIDUAL(result, BLOCKSIZE);
43 adj = BLOCKSIZE - adj;
45 result = (char *) result + adj;
50 /* Finish the initialization of the mheap. If we want to inline it
51 * properly, we need to make the align function publicly visible, too */
52 static void initialize(xbt_mheap_t mdp)
54 mdp->heapsize = HEAP / BLOCKSIZE;
55 mdp->heapinfo = (malloc_info *)
56 align(mdp, mdp->heapsize * sizeof(malloc_info));
58 memset((void *) mdp->heapinfo, 0, mdp->heapsize * sizeof(malloc_info));
59 mdp->heapinfo[0].type=-1;
60 mdp->heapinfo[0].free_block.size = 0;
61 mdp->heapinfo[0].free_block.next = mdp->heapinfo[0].free_block.prev = 0;
63 mdp->heapbase = (void *) mdp->heapinfo;
64 mdp->flags |= MMALLOC_INITIALIZED;
67 /* Get neatly aligned memory from the low level layers, and register it
68 * into the heap info table as necessary. */
69 static void *register_morecore(struct mdesc *mdp, size_t size)
72 malloc_info *newinfo, *oldinfo;
75 result = align(mdp, size); // Never returns NULL
77 /* Check if we need to grow the info table (in a multiplicative manner) */
78 if ((size_t) BLOCK((char *) result + size) > mdp->heapsize) {
81 newsize = mdp->heapsize;
82 while ((size_t) BLOCK((char *) result + size) > newsize)
85 /* Copy old info into new location */
86 oldinfo = mdp->heapinfo;
87 newinfo = (malloc_info *) align(mdp, newsize * sizeof(malloc_info));
88 memset(newinfo, 0, newsize * sizeof(malloc_info));
89 memcpy(newinfo, oldinfo, mdp->heapsize * sizeof(malloc_info));
90 mdp->heapinfo = newinfo;
92 /* mark the space previously occupied by the block info as free by first marking it
93 * as occupied in the regular way, and then freing it */
94 for (it=0; it<BLOCKIFY(mdp->heapsize * sizeof(malloc_info)); it++)
95 newinfo[BLOCK(oldinfo)+it].type = 0;
97 newinfo[BLOCK(oldinfo)].busy_block.size = BLOCKIFY(mdp->heapsize * sizeof(malloc_info));
98 newinfo[BLOCK(oldinfo)].busy_block.busy_size = size;
99 newinfo[BLOCK(oldinfo)].busy_block.bt_size = 0;// FIXME setup the backtrace
100 mfree(mdp, (void *) oldinfo);
101 mdp->heapsize = newsize;
104 mdp->heaplimit = BLOCK((char *) result + size);
108 /* Allocate memory from the heap. */
109 void *mmalloc(xbt_mheap_t mdp, size_t size) {
110 void *res= mmalloc_no_memset(mdp,size);
114 /* Spliting mmalloc this way is mandated by a trick in mrealloc, that gives
115 back the memory of big blocks to the system before reallocating them: we don't
116 want to loose the beginning of the area when this happens */
117 void *mmalloc_no_memset(xbt_mheap_t mdp, size_t size)
120 size_t block, blocks, lastblocks, start;
126 size_t requested_size = size; // The amount of memory requested by user, for real
130 /* Work even if the user was stupid enough to ask a ridicullously small block (even 0-length),
131 * ie return a valid block that can be realloced and freed.
132 * glibc malloc does not use this trick but return a constant pointer, but we need to enlist the free fragments later on.
134 if (size < SMALLEST_POSSIBLE_MALLOC)
135 size = SMALLEST_POSSIBLE_MALLOC;
137 // printf("(%s) Mallocing %d bytes on %p (default: %p)...",xbt_thread_self_name(),size,mdp,__mmalloc_default_mdp);fflush(stdout);
139 if (!(mdp->flags & MMALLOC_INITIALIZED))
142 /* Determine the allocation policy based on the request size. */
143 if (size <= BLOCKSIZE / 2) {
144 /* Small allocation to receive a fragment of a block.
145 Determine the logarithm to base two of the fragment size. */
148 while ((size /= 2) != 0) {
152 /* Look in the fragment lists for a
153 free fragment of the desired size. */
154 next = mdp->fraghead[log].next;
156 /* There are free fragments of this size.
157 Pop a fragment out of the fragment list and return it.
158 Update the block's nfree and first counters. */
160 result = (void *) next;
161 block = BLOCK(result);
163 frag_nb = RESIDUAL(result, BLOCKSIZE) >> log;
164 mdp->heapinfo[block].busy_frag.frag_size[frag_nb] = requested_size;
165 xbt_backtrace_no_malloc(mdp->heapinfo[block].busy_frag.bt[frag_nb],XBT_BACKTRACE_SIZE);
167 next->prev->next = next->next;
168 if (next->next != NULL) {
169 next->next->prev = next->prev;
171 if (--mdp->heapinfo[block].busy_frag.nfree != 0) {
172 mdp->heapinfo[block].busy_frag.first =
173 RESIDUAL(next->next, BLOCKSIZE) >> log;
176 /* Update the statistics. */
177 mdp -> heapstats.chunks_used++;
178 mdp -> heapstats.bytes_used += 1 << log;
179 mdp -> heapstats.chunks_free--;
180 mdp -> heapstats.bytes_free -= 1 << log;
183 /* No free fragments of the desired size, so get a new block
184 and break it into fragments, returning the first. */
185 //printf("(%s) No free fragment...",xbt_thread_self_name());
187 result = mmalloc(mdp, BLOCKSIZE); // does not return NULL
189 /* Link all fragments but the first into the free list, and mark their requested size to -1. */
190 block = BLOCK(result);
191 for (i = 1; i < (size_t) (BLOCKSIZE >> log); ++i) {
192 mdp->heapinfo[block].busy_frag.frag_size[i] = -1;
193 next = (struct list *) ((char *) result + (i << log));
194 next->next = mdp->fraghead[log].next;
195 next->prev = &mdp->fraghead[log];
196 next->prev->next = next;
197 if (next->next != NULL) {
198 next->next->prev = next;
201 mdp->heapinfo[block].busy_frag.frag_size[0] = requested_size;
202 xbt_backtrace_no_malloc(mdp->heapinfo[block].busy_frag.bt[0],XBT_BACKTRACE_SIZE);
204 /* Initialize the nfree and first counters for this block. */
205 mdp->heapinfo[block].type = log;
206 mdp->heapinfo[block].busy_frag.nfree = i - 1;
207 mdp->heapinfo[block].busy_frag.first = i - 1;
209 mdp -> heapstats.chunks_free += (BLOCKSIZE >> log) - 1;
210 mdp -> heapstats.bytes_free += BLOCKSIZE - (1 << log);
211 mdp -> heapstats.bytes_used -= BLOCKSIZE - (1 << log);
214 /* Large allocation to receive one or more blocks.
215 Search the free list in a circle starting at the last place visited.
216 If we loop completely around without finding a large enough
217 space we will have to get more memory from the system. */
218 blocks = BLOCKIFY(size);
219 start = block = MALLOC_SEARCH_START;
220 while (mdp->heapinfo[block].free_block.size < blocks) {
221 if (mdp->heapinfo[block].type >=0) { // Don't trust xbt_die and friends in malloc-level library, you fool!
222 fprintf(stderr,"Internal error: found a free block not marked as such (block=%lu type=%lu). Please report this bug.\n",(unsigned long)block,(unsigned long)mdp->heapinfo[block].type);
226 block = mdp->heapinfo[block].free_block.next;
227 if (block == start) {
228 /* Need to get more from the system. Check to see if
229 the new core will be contiguous with the final free
230 block; if so we don't need to get as much. */
231 block = mdp->heapinfo[0].free_block.prev;
232 lastblocks = mdp->heapinfo[block].free_block.size;
233 if (mdp->heaplimit != 0 &&
234 block + lastblocks == mdp->heaplimit &&
235 mmorecore(mdp, 0) == ADDRESS(block + lastblocks) &&
236 (register_morecore(mdp, (blocks - lastblocks) * BLOCKSIZE)) != NULL) {
237 /* Which block we are extending (the `final free
238 block' referred to above) might have changed, if
239 it got combined with a freed info table. */
240 block = mdp->heapinfo[0].free_block.prev;
242 mdp->heapinfo[block].free_block.size += (blocks - lastblocks);
245 result = register_morecore(mdp, blocks * BLOCKSIZE);
247 block = BLOCK(result);
248 for (it=0;it<blocks;it++){
249 mdp->heapinfo[block+it].type = 0;
251 mdp->heapinfo[block].busy_block.size = blocks;
252 mdp->heapinfo[block].busy_block.busy_size = requested_size;
253 mdp->heapinfo[block].busy_block.bt_size=xbt_backtrace_no_malloc(mdp->heapinfo[block].busy_block.bt,XBT_BACKTRACE_SIZE);
254 mdp -> heapstats.chunks_used++;
255 mdp -> heapstats.bytes_used += blocks * BLOCKSIZE;
261 /* Need large block(s), but found some in the existing heap */
264 /* At this point we have found a suitable free list entry.
265 Figure out how to remove what we need from the list. */
266 result = ADDRESS(block);
267 if (mdp->heapinfo[block].free_block.size > blocks) {
268 /* The block we found has a bit left over,
269 so relink the tail end back into the free list. */
270 mdp->heapinfo[block + blocks].free_block.size
271 = mdp->heapinfo[block].free_block.size - blocks;
272 mdp->heapinfo[block + blocks].free_block.next
273 = mdp->heapinfo[block].free_block.next;
274 mdp->heapinfo[block + blocks].free_block.prev
275 = mdp->heapinfo[block].free_block.prev;
276 mdp->heapinfo[mdp->heapinfo[block].free_block.prev].free_block.next
277 = mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev
278 = mdp->heapindex = block + blocks;
280 /* The block exactly matches our requirements,
281 so just remove it from the list. */
282 mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev
283 = mdp->heapinfo[block].free_block.prev;
284 mdp->heapinfo[mdp->heapinfo[block].free_block.prev].free_block.next
285 = mdp->heapindex = mdp->heapinfo[block].free_block.next;
288 for (it=0;it<blocks;it++){
289 mdp->heapinfo[block+it].type = 0;
291 mdp->heapinfo[block].busy_block.size = blocks;
292 mdp->heapinfo[block].busy_block.busy_size = requested_size;
293 //mdp->heapinfo[block].busy_block.bt_size = 0;
294 mdp->heapinfo[block].busy_block.bt_size = xbt_backtrace_no_malloc(mdp->heapinfo[block].busy_block.bt,XBT_BACKTRACE_SIZE);
296 mdp -> heapstats.chunks_used++;
297 mdp -> heapstats.bytes_used += blocks * BLOCKSIZE;
298 mdp -> heapstats.bytes_free -= blocks * BLOCKSIZE;
301 //printf("(%s) Done mallocing. Result is %p\n",xbt_thread_self_name(),result);fflush(stdout);