2 * malloc.c - dynamic memory allocation
4 * written by Vangelis Rokas, 2004 (vrokas@otenet.gr)
10 extern unsigned char *_dynamicHeap; /* pointer to heap */
12 unsigned char *realloc(unsigned char *mblock, unsigned char len)
14 _malloc_rec *pHeap; /* pointer to block header */
16 unsigned char bLen; /* size of block */
19 if(len > MAX_BLOCK_SIZE)
20 return ((unsigned char *)0);
22 len++; /* increase to count header too */
24 pHeap = (_malloc_rec *)(mblock-1);
25 bLen = pHeap->bits.count;
27 /* new size is same as old, return pointer */
28 if(bLen == len)return (mblock);
31 /* new segment is smaller than the old one, that's easy! */
32 pHeap->bits.count = len;
35 temp->bits.count = bLen - len;
37 return (((unsigned char *)pHeap) + 1);
40 /* so, new segment has size bigger than the old one
41 * we can only return a valid pointer only when after
42 * the block there is an empty block that can be merged
43 * to produce a new block of the requested size, otherwise
46 temp = pHeap + pHeap->bits.count;
48 while((temp->datum) && (!temp->bits.alloc) && (eLen < len)) {
49 eLen += temp->bits.count;
50 temp += temp->bits.count;
55 /* so we found one, adjust memory blocks */
58 temp->bits.count = len;
63 if(eLen > MAX_BLOCK_SIZE)i = MAX_BLOCK_SIZE;
72 return (((unsigned char *)pHeap) + 1);
76 /* no could not find a valid block, return NULL */
78 return ((unsigned char *)0);