1 /*-------------------------------------------------------------------------
2 malloc.h - dynamic memory allocation header
4 Copyright (C) 2004, Vangelis Rokas <vrokas AT otenet.gr>
6 This library 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 library 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 library; see the file COPYING. If not, write to the
18 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
21 As a special exception, if you link this library with other files,
22 some of which are compiled with SDCC, to produce an executable,
23 this library does not by itself cause the resulting executable to
24 be covered by the GNU General Public License. This exception does
25 not however invalidate any other reasons why the executable file
26 might be covered by the GNU General Public License.
27 -------------------------------------------------------------------------*/
30 * Structure of memory block header:
31 * bit 7 (MSB): allocated flag
32 * bits 0-6: pointer to next block (max length: 126)
41 /* set EMULATION to 1 to enable native Linux malloc emulation layer. This is
42 * for debugging purposes only */
49 //#define malloc pic16_malloc
50 //#define free pic16_free
51 //#define realloc pic16_realloc
52 //#define calloc pic16_calloc
54 //#define lmalloc pic16_lmalloc
55 //#define lfree pic16_lfree
56 //#define lrealloc pic16_lrealloc
57 //#define lcalloc pic16_lcalloc
64 #define _MALLOC_SPEC __data
68 /* when MALLOC_MAX_FIRST is 1, the memory allocator tries to find a block
69 * that fits the requested size without merging (initially), if this block
70 * is not found, then tries to merge adjacent blocks. If MALLOC_MAX_FIRST is
71 * set 0, then the allocator tries to merge adjacent blocks in the first
72 * place. Both behaviours may give better results when used in certain
73 * circumstancs. I.e. if realloc is to be used, leaving some space after the
74 * block, will allow realloc to allocate it, otherwise it may result in much
75 * more memory fragmentation. An algorithm can be implemented to allow small
76 * fragments to be allocated but this is much too complicated for the PIC18F's
78 #define MALLOC_MAX_FIRST 0
80 #define MAX_BLOCK_SIZE 0x7f /* 127 bytes */
81 #define MAX_HEAP_SIZE 0x200 /* 512 bytes */
82 #define _MAX_HEAP_SIZE (MAX_HEAP_SIZE-1)
84 #define ALLOC_FLAG 0x80
87 /* memory block header, max size 127 bytes, 126 usable */
97 /* initialize heap, should be called before any call to malloc/realloc/calloc */
98 void _initHeap(unsigned char _MALLOC_SPEC
*dHeap
, unsigned int heapsize
);
101 /* start searching for a block of size at least bSize, merge adjacent blocks
103 _malloc_rec _MALLOC_SPEC
*_mergeHeapBlock(_malloc_rec _MALLOC_SPEC
*sBlock
, unsigned char bSize
);
106 /* allocate a memory block */
107 unsigned char _MALLOC_SPEC
*malloc(unsigned char len
);
110 /* same as malloc, but clear memory */
111 unsigned char _MALLOC_SPEC
*calloc(unsigned char len
);
114 /* expand or reduce a memory block, if mblock is NULL, then same as malloc */
115 unsigned char _MALLOC_SPEC
*realloc(unsigned char _MALLOC_SPEC
*mblock
, unsigned char len
);
118 /* free a memory block */
119 void free(unsigned char _MALLOC_SPEC
*);
122 /* returns the size of all the unallocated memory */
123 unsigned int memfree(void);
126 /* return the size of the maximum unallocated memory block */
127 unsigned int memfreemax(void);
129 #endif /* __MALLOC_H__ */