Imported Upstream version 2.9.0
[debian/cc1111] / device / include / pic16 / malloc.h
1 /*
2  * malloc.h - dynamic memory allocation header
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: malloc.h 3714 2005-04-02 13:13:53Z vrokas $
25  */
26
27
28 /*
29  * Structure of memory block header:
30  * bit 7 (MSB): allocated flag
31  * bits 0-6: pointer to next block (max length: 126)
32  *
33  */
34
35
36 #ifndef __MALLOC_H__
37 #define __MALLOC_H__
38
39
40 /* set EMULATION to 1 to enable native Linux malloc emulation layer. This is
41  * for debugging purposes only */
42  
43 #ifndef EMULATION
44 #define EMULATION       0
45 #endif
46
47 #if EMULATION
48 //#define malloc        pic16_malloc
49 //#define free  pic16_free
50 //#define realloc       pic16_realloc
51 //#define calloc        pic16_calloc
52
53 //#define lmalloc               pic16_lmalloc
54 //#define lfree         pic16_lfree
55 //#define lrealloc      pic16_lrealloc
56 //#define lcalloc               pic16_lcalloc
57 #define _MALLOC_SPEC
58
59 #else
60
61 #pragma library c
62
63 #define _MALLOC_SPEC    __data
64
65 #endif
66
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
76  * architecture */
77 #define MALLOC_MAX_FIRST        0
78
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)
82
83 #define ALLOC_FLAG              0x80
84 #define HEADER_SIZE             1
85
86 /* memory block header, max size 127 bytes, 126 usable */
87 typedef union {
88   unsigned char datum;
89   struct {
90     unsigned count: 7;
91     unsigned alloc: 1;
92   } bits;
93 } _malloc_rec;
94
95
96 /* initialize heap, should be called before any call to malloc/realloc/calloc */
97 void _initHeap(unsigned char _MALLOC_SPEC *dHeap, unsigned int heapsize);
98
99
100 /* start searching for a block of size at least bSize, merge adjacent blocks
101  * if necessery */
102 _malloc_rec _MALLOC_SPEC *_mergeHeapBlock(_malloc_rec _MALLOC_SPEC *sBlock, unsigned char bSize);
103
104
105 /* allocate a memory block */
106 unsigned char _MALLOC_SPEC *malloc(unsigned char len);
107
108
109 /* same as malloc, but clear memory */
110 unsigned char _MALLOC_SPEC *calloc(unsigned char len);
111
112
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);
115
116
117 /* free a memory block */
118 void free(unsigned char _MALLOC_SPEC *);
119
120
121 /* returns the size of all the unallocated memory */
122 unsigned int memfree(void);
123
124
125 /* return the size of the maximum unallocated memory block */
126 unsigned int memfreemax(void);
127
128
129 #endif /* __MALLOC_H__ */