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>
17 // AROS_LH3(APTR, AllocVec,
24 // AROS_LHA(APTR, oldmem, A0),
25 // AROS_LHA(ULONG, byteSize, D0),
26 // AROS_LHA(ULONG, requirements, D1),
28 // struct ExecBase *, SysBase, 1xx, Exec)
31 Change the size of an AllocVec:ed part of memory. The memory must
32 have been allocated by AllocVec(). If you reduce the
33 size, the old contents will be lost. If you enlarge the size,
34 the new contents will be undefined.
37 oldmen - What you got from AllocVec().
38 newsize - The new size.
39 requirements - The (new) requirements.
40 Note that if no new block of memory is allocated, the
41 requirements are not considered.
44 A pointer to the allocated memory or NULL. If you don't need the
45 memory anymore, you can pass this pointer to FreeVec().
48 If you get NULL, the memory at oldmem will not have been freed and
50 Note that if no new block of memory is allocated, the requirements
53 This function must not be used in a shared library or in a
54 threaded application. (???)
62 AllocVec(), FreeVec(), CopyMem()
68 ******************************************************************************/
71 UBYTE
* mem
, * newmem
;
75 return AllocVec (newsize
, requirements
);
77 mem
= (UBYTE
*)oldmem
- AROS_ALIGN(sizeof(ULONG
));
78 oldsize
= *((ULONG
*)mem
) - sizeof(ULONG
);
80 /* Reduce or enlarge the memory ? */
81 if (newsize
< oldsize
)
83 /* Don't change anything for small changes */
84 if ((oldsize
- newsize
) < REALLOC_MINDECREASE
)
89 else if (newsize
== oldsize
) /* Keep the size ? */
93 /* It is likely that if memory is ReAllocVec:ed once it will
94 be ReAllocVec:ed again, so don't be too stingy with memory */
95 if ((newsize
- oldsize
) < REALLOC_MININCREASE
)
96 newsize
= oldsize
+ REALLOC_MININCREASE
;
98 newmem
= AllocVec(newsize
, requirements
);
102 CopyMem (oldmem
, newmem
, newsize
);