]> git.sur5r.net Git - cc65/blob - libsrc/common/realloc.c
The spans do now contain the size of a span, no longer the end offset.
[cc65] / libsrc / common / realloc.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 realloc.c                                 */
4 /*                                                                           */
5 /*               Change the size of an allocated memory block                */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2004 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <_heap.h>
39
40
41
42 void* __fastcall__ realloc (void* block, size_t size)
43 {
44     struct usedblock* b;
45     struct usedblock* newblock;
46     unsigned oldsize;
47     unsigned newhptr;
48     int diff;
49
50     /* Check the block parameter */
51     if (!block) {
52         /* Block is NULL, same as malloc */
53         return malloc (size);
54     }
55
56     /* Check the size parameter */
57     if (size == 0) {
58         /* Block is not NULL, but size is: free the block */
59         free (block);
60         return 0;
61     }
62
63     /* Make the internal used size from the given size */
64     size += HEAP_ADMIN_SPACE;
65     if (size < sizeof (struct freeblock)) {
66         size = sizeof (struct freeblock);
67     }
68
69     /* The word below the user block contains a pointer to the start of the
70      * raw memory block. The first word of this raw memory block is the full
71      * size of the block. Get a pointer to the real block, get the old block
72      * size.
73      */
74     b = (((struct usedblock*) block) - 1)->start;
75     oldsize = b->size;
76
77     /* Get the size difference as a signed quantity */
78     diff = size - oldsize;
79
80     /* Is the block at the current heap top? */
81     if (((unsigned) b) + oldsize == ((unsigned) _heapptr)) {
82         /* Check if we've enough memory at the heap top */
83         newhptr = ((unsigned) _heapptr) + diff;
84         if (newhptr <= ((unsigned) _heapend)) {
85             /* Ok, there's space enough */
86             _heapptr = (unsigned*) newhptr;
87             b->size = size;
88             b->start = b;
89             return block;
90         }
91     }
92
93     /* The given block was not located on top of the heap, or there's no
94      * room left. Try to allocate a new block and copy the data.
95      */
96     if (newblock = malloc (size)) {
97
98         /* Adjust the old size to the user visible portion */
99         oldsize -= HEAP_ADMIN_SPACE;
100
101         /* If the new block is larger than the old one, copy the old
102          * data only
103          */
104         if (size > oldsize) {
105             size = oldsize;
106         }
107
108         /* Copy the block data */
109         memcpy (newblock, block, size);
110         free (block);
111     }
112     return newblock;
113 }
114
115
116
117