2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
5 Desc: New Exec pendant of ANSI C function realloc() using AllocVec()
9 #define REALLOC_MININCREASE 1024
10 #define REALLOC_MINDECREASE 4096
12 /*****************************************************************************
15 #include <proto/exec.h>
25 Change the size of an AllocVec:ed part of memory. The memory must
26 have been allocated by AllocVec(). If you reduce the
27 size, the old contents will be lost. If you enlarge the size,
28 the new contents will be undefined.
31 oldmen - What you got from AllocVec().
32 newsize - The new size.
33 requirements - The (new) requirements.
34 Note that if no new block of memory is allocated, the
35 requirements are not considered.
38 A pointer to the allocated memory or NULL. If you don't need the
39 memory anymore, you can pass this pointer to FreeVec().
42 If you get NULL, the memory at oldmem will not have been freed and
44 Note that if no new block of memory is allocated, the requirements
47 This function must not be used in a shared library or in a
48 threaded application. (???)
56 exec.library/AllocVec(), exec.library/FreeVec(), exec.library/CopyMem()
62 ******************************************************************************/
65 UBYTE
* mem
, * newmem
;
69 return AllocVec (newsize
, requirements
);
71 mem
= (UBYTE
*)oldmem
- AROS_ALIGN(sizeof(ULONG
));
72 oldsize
= *((ULONG
*)mem
) - sizeof(ULONG
);
74 /* Reduce or enlarge the memory ? */
75 if (newsize
< oldsize
)
77 /* Don't change anything for small changes */
78 if ((oldsize
- newsize
) < REALLOC_MINDECREASE
)
83 else if (newsize
== oldsize
) /* Keep the size ? */
87 /* It is likely that if memory is ReAllocVec:ed once it will
88 be ReAllocVec:ed again, so don't be too stingy with memory */
89 if ((newsize
- oldsize
) < REALLOC_MININCREASE
)
90 newsize
= oldsize
+ REALLOC_MININCREASE
;
92 newmem
= AllocVec(newsize
, requirements
);
96 CopyMem (oldmem
, newmem
, newsize
);