boot.library is no more, *.handler to *-handler.
[AROS.git] / compiler / alib / reallocvec.c
blob254bfad71275470a42311a6d7a22461e6d3d2e6d
1 /*
2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: New Exec pendant of ANSI C function realloc() using AllocVec()
6 Lang: english
7 */
9 #define REALLOC_MININCREASE 1024
10 #define REALLOC_MINDECREASE 4096
12 /*****************************************************************************
14 NAME */
15 #include <proto/exec.h>
17 APTR ReAllocVec (
19 /* SYNOPSIS */
20 APTR oldmem,
21 ULONG newsize,
22 ULONG requirements)
24 /* FUNCTION
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.
30 INPUTS
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.
37 RESULT
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().
41 NOTES
42 If you get NULL, the memory at oldmem will not have been freed and
43 can still be used.
44 Note that if no new block of memory is allocated, the requirements
45 are not considered.
47 This function must not be used in a shared library or in a
48 threaded application. (???)
51 EXAMPLE
53 BUGS
55 SEE ALSO
56 exec.library/AllocVec(), exec.library/FreeVec(), exec.library/CopyMem()
58 INTERNALS
60 HISTORY
62 ******************************************************************************/
64 // AROS_LIBFUNC_INIT
65 UBYTE * mem, * newmem;
66 ULONG oldsize;
68 if (!oldmem)
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)
79 newmem = oldmem;
80 else
81 goto copy;
83 else if (newsize == oldsize) /* Keep the size ? */
84 newmem = oldmem;
85 else
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;
91 copy:
92 newmem = AllocVec(newsize, requirements);
94 if (newmem)
96 CopyMem (oldmem, newmem, newsize);
97 FreeVec (oldmem);
101 return newmem;
102 // AROS_LIBFUNC_EXIT
103 } /* ReAllocVec */