clean up bcm bases and add primecell peripheral defines
[AROS.git] / compiler / clib / realloc_nocopy.c
blob191d2f61f3da0937325c968d5b3f2ecb3ca86ade
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 <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 oldmen - 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 must not be used in a shared library or
42 in a threaded application.
44 This function is AROS specific.
46 EXAMPLE
48 BUGS
50 SEE ALSO
51 free(), malloc(), calloc(), realloc()
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 alloc;
75 else if (size == oldsize) /* Keep the size ? */
76 newmem = oldmem;
77 else
79 alloc:
80 newmem = malloc (size);
82 if (newmem)
83 free (oldmem);
86 return newmem;
87 } /* realloc */