* src/pic16/device.c (Pics16[]): added devices 18F2550, 18F4331,
[fw/sdcc] / device / lib / pic16 / libc / stdlib / realloc.c
1 /*
2  * realloc.c - dynamic memory allocation
3  *
4  * written by Vangelis Rokas, 2004 (vrokas@otenet.gr)
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2, or (at your option) any
9  * later version.
10  * 
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  * 
20  * In other words, you are welcome to use, share and improve this program.
21  * You are forbidden to forbid anyone else to use, share and improve
22  * what you give them.   Help stamp out software-hoarding!  
23  *
24  * $Id$
25  */
26
27 #include <malloc.h>
28
29 extern unsigned char _MALLOC_SPEC *heap;
30
31 unsigned char _MALLOC_SPEC *realloc(unsigned char _MALLOC_SPEC *mblock, unsigned char len)
32 {
33   _malloc_rec _MALLOC_SPEC *pHeap;                      /* pointer to block header */
34   _malloc_rec _MALLOC_SPEC *temp;
35   unsigned char bLen;                   /* size of block  */
36
37     if(len >= MAX_BLOCK_SIZE)
38       return ((unsigned char _MALLOC_SPEC *)0);
39
40     /* if mblock is NULL, then same as malloc */
41     if(!mblock)
42       return (malloc(len));
43
44     /* if len is 0, */
45     if(len == 0) {
46       free(mblock);
47       return (malloc(0));
48     }
49     
50     len++;              /* increase to count header too */
51
52     pHeap = (_malloc_rec _MALLOC_SPEC *)((unsigned int)mblock - 1);
53     bLen = pHeap->bits.count;
54
55     /* new size is same as old, return pointer */
56     if(bLen == len)return (mblock);
57
58     if(bLen > len) {
59       /* new segment is smaller than the old one, that's easy! */
60       pHeap->bits.count = len;
61       temp = (_malloc_rec _MALLOC_SPEC *)((unsigned int)pHeap + len);
62       temp->bits.alloc = 0;
63       temp->bits.count = bLen - len;
64
65       return ((unsigned char _MALLOC_SPEC *)((unsigned int)pHeap + 1));
66     }
67
68     /* so, new segment has size bigger than the old one, we can return a
69      * valid pointer only when after the block there is an empty block that
70      * can be merged to produce a new block of the requested size, otherwise
71      * we return NULL */
72     temp = _mergeHeapBlock(pHeap, len);
73
74     if(!temp) {
75       /* no could not find a valid block, return NULL */
76       return ((unsigned char _MALLOC_SPEC *)0);
77     }
78
79     pHeap = temp;
80     bLen = pHeap->bits.count;
81     
82     /* allocate by filling the fields */
83     pHeap->bits.count = len;
84     pHeap->bits.alloc = 1;
85
86     if(bLen > len) {
87       /* if current block size is greater than the requested one,
88        * create a new empty block at the end of the newly allocated */
89       temp = (_malloc_rec _MALLOC_SPEC *)((unsigned int)pHeap + len);
90       temp->bits.count = bLen - len;
91       temp->bits.alloc = 0;
92     }
93
94   return ((unsigned char _MALLOC_SPEC *)((unsigned int)pHeap + 1));
95 }