fix config file path
[AROS.git] / compiler / clib / realloc.c
blob32d3a593d40e249f78a83cae23bf436f94706f8b
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 function realloc().
6 */
8 #include <aros/cpu.h>
9 #include <proto/exec.h>
11 /*****************************************************************************
13 NAME */
14 #include <sys/types.h>
15 #include <stdlib.h>
17 void * realloc (
19 /* SYNOPSIS */
20 void * oldmem,
21 size_t size)
23 /* FUNCTION
24 Change the size of an allocated part of memory. The memory must
25 have been allocated by malloc() or calloc(). If you reduce the
26 size, the old contents will be lost. If you enlarge the size,
27 the new contents will be undefined.
29 INPUTS
30 oldmem - What you got from malloc() or calloc().
31 size - The new size.
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.
46 EXAMPLE
48 BUGS
50 SEE ALSO
51 free(), malloc(), calloc()
53 INTERNALS
55 ******************************************************************************/
57 UBYTE * mem, * newmem;
58 size_t oldsize;
60 if (!oldmem)
61 return malloc (size);
63 mem = (UBYTE *)oldmem - AROS_ALIGN(sizeof(size_t));
64 oldsize = *((size_t *)mem);
66 /* Reduce or enlarge the memory ? */
67 if (size < oldsize)
69 /* Don't change anything for small changes */
70 if ((oldsize - size) < 4096)
71 newmem = oldmem;
72 else
73 goto copy;
75 else if (size == oldsize) /* Keep the size ? */
76 newmem = oldmem;
77 else
79 copy:
80 newmem = malloc (size);
82 if (newmem)
84 if (size > oldsize)
85 size = oldsize;
86 CopyMem (oldmem, newmem, size);
87 free (oldmem);
91 return newmem;
92 } /* realloc */