2 * malloc.h - dynamic memory allocation header
4 * written by Vangelis Rokas, 2004 (vrokas@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 * Structure of memory block header:
30 * bit 7 (MSB): allocated flag
31 * bits 0-6: pointer to next block (max length: 126)
40 /* set EMULATION to 1 to enable native Linux malloc emulation layer. This is
41 * for debugging purposes only */
48 //#define malloc pic16_malloc
49 //#define free pic16_free
50 //#define realloc pic16_realloc
51 //#define calloc pic16_calloc
53 //#define lmalloc pic16_lmalloc
54 //#define lfree pic16_lfree
55 //#define lrealloc pic16_lrealloc
56 //#define lcalloc pic16_lcalloc
63 #define _MALLOC_SPEC __data
67 /* when MALLOC_MAX_FIRST is 1, the memory allocator tries to find a block
68 * that fits the requested size without merging (initially), if this block
69 * is not found, then tries to merge adjacent blocks. If MALLOC_MAX_FIRST is
70 * set 0, then the allocator tries to merge adjacent blocks in the first
71 * place. Both behaviours may give better results when used in certain
72 * circumstancs. I.e. if realloc is to be used, leaving some space after the
73 * block, will allow realloc to allocate it, otherwise it may result in much
74 * more memory fragmentation. An algorithm can be implemented to allow small
75 * fragments to be allocated but this is much too complicated for the PIC18F's
77 #define MALLOC_MAX_FIRST 0
79 #define MAX_BLOCK_SIZE 0x7f /* 127 bytes */
80 #define MAX_HEAP_SIZE 0x200 /* 512 bytes */
81 #define _MAX_HEAP_SIZE (MAX_HEAP_SIZE-1)
83 #define ALLOC_FLAG 0x80
86 /* memory block header, max size 127 bytes, 126 usable */
96 /* initialize heap, should be called before any call to malloc/realloc/calloc */
97 void _initHeap(unsigned char _MALLOC_SPEC *dHeap, unsigned int heapsize);
100 /* start searching for a block of size at least bSize, merge adjacent blocks
102 _malloc_rec _MALLOC_SPEC *_mergeHeapBlock(_malloc_rec _MALLOC_SPEC *sBlock, unsigned char bSize);
105 /* allocate a memory block */
106 unsigned char _MALLOC_SPEC *malloc(unsigned char len);
109 /* same as malloc, but clear memory */
110 unsigned char _MALLOC_SPEC *calloc(unsigned char len);
113 /* expand or reduce a memory block, if mblock is NULL, then same as malloc */
114 unsigned char _MALLOC_SPEC *realloc(unsigned char _MALLOC_SPEC *mblock, unsigned char len);
117 /* free a memory block */
118 void free(unsigned char _MALLOC_SPEC *);
121 /* returns the size of all the unallocated memory */
122 unsigned int memfree(void);
125 /* return the size of the maximum unallocated memory block */
126 unsigned int memfreemax(void);
129 #endif /* __MALLOC_H__ */