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. */
23 mrealloc (void *md, void *ptr, size_t size)
28 size_t block, blocks, oldlimit;
33 return (mmalloc (md, 0));
37 return (mmalloc (md, size));
42 if (mdp -> mrealloc_hook != NULL)
44 return ((*mdp -> mrealloc_hook) (md, ptr, size));
49 type = mdp -> heapinfo[block].busy.type;
53 /* Maybe reallocate a large block to a small fragment. */
54 if (size <= BLOCKSIZE / 2)
56 result = mmalloc (md, size);
59 memcpy (result, ptr, size);
65 /* The new size is a large allocation as well;
66 see if we can hold it in place. */
67 blocks = BLOCKIFY (size);
68 if (blocks < mdp -> heapinfo[block].busy.info.size)
70 /* The new size is smaller; return excess memory to the free list. */
71 mdp -> heapinfo[block + blocks].busy.type = 0;
72 mdp -> heapinfo[block + blocks].busy.info.size
73 = mdp -> heapinfo[block].busy.info.size - blocks;
74 mdp -> heapinfo[block].busy.info.size = blocks;
75 mfree (md, ADDRESS (block + blocks));
78 else if (blocks == mdp -> heapinfo[block].busy.info.size)
80 /* No size change necessary. */
85 /* Won't fit, so allocate a new region that will.
86 Free the old region first in case there is sufficient
87 adjacent free space to grow without moving. */
88 blocks = mdp -> heapinfo[block].busy.info.size;
89 /* Prevent free from actually returning memory to the system. */
90 oldlimit = mdp -> heaplimit;
93 mdp -> heaplimit = oldlimit;
94 result = mmalloc (md, size);
97 mmalloc (md, blocks * BLOCKSIZE);
102 memmove (result, ptr, blocks * BLOCKSIZE);
108 /* Old size is a fragment; type is logarithm
109 to base two of the fragment size. */
110 if (size > (size_t) (1 << (type - 1)) && size <= (size_t) (1 << type))
112 /* The new size is the same kind of fragment. */
117 /* The new size is different; allocate a new space,
118 and copy the lesser of the new size and the old. */
119 result = mmalloc (md, size);
124 memcpy (result, ptr, MIN (size, (size_t) 1 << type));
133 /* Useless prototype to make gcc happy */
134 void *realloc (void *ptr, size_t size);
136 /* When using this package, provide a version of malloc/realloc/free built
137 on top of it, so that if we use the default sbrk() region we will not
138 collide with another malloc package trying to do the same thing, if
139 the application contains any "hidden" calls to malloc/realloc/free (such
140 as inside a system library).
141 FIXME: disabled for now */
143 //void * realloc (void *ptr, size_t size) {
146 // result = mrealloc (NULL, ptr, size);