revert between 56095 -> 55830 in arch
[AROS.git] / compiler / stdc / realloc_nocopy.c
blob66adb9bda40818e8b69ba6547564e4245c20aa46
1 /*
2 Copyright © 1995-2014, 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 <stdlib.h>
14 void * realloc_nocopy (
16 /* SYNOPSIS */
17 void * oldmem,
18 size_t size)
20 /* FUNCTION
21 Change the size of an allocated part of memory. The memory must
22 have been allocated by malloc(), calloc(), realloc() or realloc_nocopy().
24 The reallocated buffer, unlike with realloc(), is not guaranteed to hold
25 a copy of the old one.
27 INPUTS
28 oldmem - What you got from malloc(), calloc(), realloc() or realloc_nocopy().
29 If NULL, the function will behave exactly like malloc().
30 size - The new size. If 0, the buffer will be freed.
32 RESULT
33 A pointer to the allocated memory or NULL. If you don't need the
34 memory anymore, you can pass this pointer to free(). If you don't,
35 the memory will be freed for you when the application exits.
37 NOTES
38 If you get NULL, the memory at oldmem will not have been freed and
39 can still be used.
41 This function is AROS specific.
43 EXAMPLE
45 BUGS
47 SEE ALSO
48 free(), malloc(), calloc(), realloc()
50 INTERNALS
52 ******************************************************************************/
54 UBYTE * mem, * newmem;
55 size_t oldsize;
57 if (!oldmem)
58 return malloc (size);
60 mem = (UBYTE *)oldmem - AROS_ALIGN(sizeof(size_t));
61 oldsize = *((size_t *)mem);
63 /* Reduce or enlarge the memory ? */
64 if (size < oldsize)
66 /* Don't change anything for small changes */
67 if ((oldsize - size) < 4096)
68 newmem = oldmem;
69 else
70 goto alloc;
72 else if (size == oldsize) /* Keep the size ? */
73 newmem = oldmem;
74 else
76 alloc:
77 newmem = malloc (size);
79 if (newmem)
80 free (oldmem);
83 return newmem;
84 } /* realloc */