add a missing section header table index conversion
[tangerine.git] / compiler / alib / reallocvec.c
blobac55b39b59944d91f348f09d2839867f9642348a
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 // AROS_LH3(APTR, AllocVec,
18 APTR ReAllocVec (
20 /* SYNOPSIS */
21 APTR oldmem,
22 ULONG newsize,
23 ULONG requirements)
24 // AROS_LHA(APTR, oldmem, A0),
25 // AROS_LHA(ULONG, byteSize, D0),
26 // AROS_LHA(ULONG, requirements, D1),
27 /* SYNOPSIS */
28 // struct ExecBase *, SysBase, 1xx, Exec)
30 /* FUNCTION
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.
36 INPUTS
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.
43 RESULT
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().
47 NOTES
48 If you get NULL, the memory at oldmem will not have been freed and
49 can still be used.
50 Note that if no new block of memory is allocated, the requirements
51 are not considered.
53 This function must not be used in a shared library or in a
54 threaded application. (???)
57 EXAMPLE
59 BUGS
61 SEE ALSO
62 AllocVec(), FreeVec(), CopyMem()
64 INTERNALS
66 HISTORY
68 ******************************************************************************/
70 // AROS_LIBFUNC_INIT
71 UBYTE * mem, * newmem;
72 ULONG oldsize;
74 if (!oldmem)
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)
85 newmem = oldmem;
86 else
87 goto copy;
89 else if (newsize == oldsize) /* Keep the size ? */
90 newmem = oldmem;
91 else
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;
97 copy:
98 newmem = AllocVec(newsize, requirements);
100 if (newmem)
102 CopyMem (oldmem, newmem, newsize);
103 FreeVec (oldmem);
107 return newmem;
108 // AROS_LIBFUNC_EXIT
109 } /* ReAllocVec */