1 /* Change the size of a block allocated by `mmalloc'.
2 Copyright 1990, 1991 Free Software Foundation
3 Written May 1989 by Mike Haertel. */
5 /* Copyright (c) 2010. The SimGrid Team.
6 * All rights reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include <string.h> /* Prototypes for memcpy, memmove, memset, etc */
13 #include "mmprivate.h"
15 /* Resize the given region to the new size, returning a pointer
16 to the (possibly moved) region. This is optimized for speed;
17 some benchmarks seem to indicate that greater compactness is
18 achieved by unconditionally allocating and copying to a
19 new region. This module has incestuous knowledge of the
20 internals of both mfree and mmalloc. */
22 void* mrealloc (void *md, void *ptr, size_t size) {
26 size_t block, blocks, oldlimit;
31 return (mmalloc (md, 0));
32 } else if (ptr == NULL) {
33 return (mmalloc (md, size));
38 //printf("(%s)realloc %p to %d...",xbt_thread_self_name(),ptr,(int)size);
40 if ((char*)ptr < (char*)mdp->heapbase || BLOCK(ptr) > mdp->heapsize ) {
41 printf("FIXME. Ouch, this pointer is not mine. I will malloc it instead of reallocing it. (please report this bug)\n");
42 result = mmalloc(md,size);
48 if (mdp -> mrealloc_hook != NULL)
51 return ((*mdp -> mrealloc_hook) (md, ptr, size));
56 type = mdp -> heapinfo[block].busy.type;
60 /* Maybe reallocate a large block to a small fragment. */
61 if (size <= BLOCKSIZE / 2)
64 //printf("(%s) alloc large block...",xbt_thread_self_name());
65 result = mmalloc (md, size);
68 memcpy (result, ptr, size);
74 /* The new size is a large allocation as well;
75 see if we can hold it in place. */
77 blocks = BLOCKIFY (size);
78 if (blocks < mdp -> heapinfo[block].busy.info.size)
80 /* The new size is smaller; return excess memory to the free list. */
81 //printf("(%s) return excess memory...",xbt_thread_self_name());
82 mdp -> heapinfo[block + blocks].busy.type = 0;
83 mdp -> heapinfo[block + blocks].busy.info.size
84 = mdp -> heapinfo[block].busy.info.size - blocks;
85 mdp -> heapinfo[block].busy.info.size = blocks;
86 mfree (md, ADDRESS (block + blocks));
89 else if (blocks == mdp -> heapinfo[block].busy.info.size)
91 /* No size change necessary. */
96 /* Won't fit, so allocate a new region that will.
97 Free the old region first in case there is sufficient
98 adjacent free space to grow without moving. */
99 blocks = mdp -> heapinfo[block].busy.info.size;
100 /* Prevent free from actually returning memory to the system. */
101 oldlimit = mdp -> heaplimit;
102 mdp -> heaplimit = 0;
104 mdp -> heaplimit = oldlimit;
106 result = mmalloc (md, size);
107 if (result == NULL) {
108 mmalloc (md, blocks * BLOCKSIZE);
112 memmove (result, ptr, blocks * BLOCKSIZE);
118 /* Old size is a fragment; type is logarithm
119 to base two of the fragment size. */
120 if (size > (size_t) (1 << (type - 1)) && size <= (size_t) (1 << type)) {
121 /* The new size is the same kind of fragment. */
122 //printf("(%s) new size is same kind of fragment...",xbt_thread_self_name());
125 /* The new size is different; allocate a new space,
126 and copy the lesser of the new size and the old. */
127 //printf("(%s) new size is different...",xbt_thread_self_name());
130 result = mmalloc (md, size);
134 memcpy (result, ptr, MIN (size, (size_t) 1 << type));
139 //printf("(%s) Done reallocing: %p\n",xbt_thread_self_name(),result);fflush(stdout);