add a missing section header table index conversion
[tangerine.git] / compiler / clib / realloc_nocopy.c
blobd67483e8f4af380a41d0c427042cd050a459e149
1 /*
2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <aros/cpu.h>
7 #include <proto/exec.h>
9 /*****************************************************************************
11 NAME */
12 #include <sys/types.h>
13 #include <stdlib.h>
15 void * realloc_nocopy (
17 /* SYNOPSIS */
18 void * oldmem,
19 size_t size)
21 /* FUNCTION
22 Change the size of an allocated part of memory. The memory must
23 have been allocated by malloc(), calloc(), realloc() or realloc_nocopy().
25 The reallocated buffer, unlike with realloc(), is not guaranteed to hold
26 a copy of the old one.
28 INPUTS
29 oldmen - What you got from malloc(), calloc(), realloc() or realloc_nocopy().
30 If NULL, the function will behave exactly like malloc().
31 size - The new size. If 0, the buffer will be freed.
33 RESULT
34 A pointer to the allocated memory or NULL. If you don't need the
35 memory anymore, you can pass this pointer to free(). If you don't,
36 the memory will be freed for you when the application exits.
38 NOTES
39 If you get NULL, the memory at oldmem will not have been freed and
40 can still be used.
42 This function must not be used in a shared library or
43 in a threaded application.
45 This function is AROS specific.
47 EXAMPLE
49 BUGS
51 SEE ALSO
52 free(), malloc(), calloc(), realloc()
54 INTERNALS
56 ******************************************************************************/
58 UBYTE * mem, * newmem;
59 size_t oldsize;
61 if (!oldmem)
62 return malloc (size);
64 mem = (UBYTE *)oldmem - AROS_ALIGN(sizeof(size_t));
65 oldsize = *((size_t *)mem);
67 /* Reduce or enlarge the memory ? */
68 if (size < oldsize)
70 /* Don't change anything for small changes */
71 if ((oldsize - size) < 4096)
72 newmem = oldmem;
73 else
74 goto alloc;
76 else if (size == oldsize) /* Keep the size ? */
77 newmem = oldmem;
78 else
80 alloc:
81 newmem = malloc (size);
83 if (newmem)
84 free (oldmem);
87 return newmem;
88 } /* realloc */