2 * memmisc.c - heap handling functions
4 * written by Vangelis Rokas, 2005 <vrokas AT otenet.gr>
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
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.
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.
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!
29 void _initHeap(unsigned char _MALLOC_SPEC *dheap, unsigned int heapsize)
31 _malloc_rec _MALLOC_SPEC *pHeap;
35 pHeap = (_malloc_rec _MALLOC_SPEC *)dheap;
37 while(hsize < heapsize-1) {
39 /* a guess of the next block size */
40 bsize = (heapsize - hsize);
41 if(bsize > MAX_BLOCK_SIZE)bsize = MAX_BLOCK_SIZE;
46 /* now we can create the block */
48 pHeap = (_malloc_rec _MALLOC_SPEC *)((unsigned int)pHeap + bsize);
55 /* search heap starting from sBlock for a block of size bSize, merging
56 * adjacent blocks if ne necessery */
57 _malloc_rec _MALLOC_SPEC *_mergeHeapBlock(_malloc_rec _MALLOC_SPEC *sBlock, unsigned char bSize)
59 _malloc_rec _MALLOC_SPEC *temp;
64 bLen = sBlock->bits.count;
66 /* current block is not enough, see if we can merge some adjacent memory
67 * blocks to make it fit */
68 temp = (_malloc_rec _MALLOC_SPEC *)((unsigned int)sBlock + bLen); //sBlock->bits.count);
70 while((temp->datum) && (!temp->bits.alloc) && (eLen < bSize)) {
71 eLen += (dat=temp->bits.count);
72 temp = (_malloc_rec _MALLOC_SPEC *)((unsigned int)temp + dat);
78 /* yes, there are some free blocks that can be merged, so merge them... */
81 if(eLen > MAX_BLOCK_SIZE)i = MAX_BLOCK_SIZE;
84 temp = (_malloc_rec _MALLOC_SPEC *)((unsigned int)temp + i);
88 /* return block starts at the old block start address */
92 /* no, there are no free blocks after sBlock, so return NULL */
93 return ((_malloc_rec _MALLOC_SPEC *)0);