malloc.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*-------------------------------------------------------------------------
  2. malloc.h - dynamic memory allocation header
  3. Copyright (C) 2004, Vangelis Rokas <vrokas AT otenet.gr>
  4. This library is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this library; see the file COPYING. If not, write to the
  14. Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  15. MA 02110-1301, USA.
  16. As a special exception, if you link this library with other files,
  17. some of which are compiled with SDCC, to produce an executable,
  18. this library does not by itself cause the resulting executable to
  19. be covered by the GNU General Public License. This exception does
  20. not however invalidate any other reasons why the executable file
  21. might be covered by the GNU General Public License.
  22. -------------------------------------------------------------------------*/
  23. /*
  24. * Structure of memory block header:
  25. * bit 7 (MSB): allocated flag
  26. * bits 0-6: pointer to next block (max length: 126)
  27. *
  28. */
  29. #ifndef __MALLOC_H__
  30. #define __MALLOC_H__
  31. /* set EMULATION to 1 to enable native Linux malloc emulation layer. This is
  32. * for debugging purposes only */
  33. #ifndef EMULATION
  34. #define EMULATION 0
  35. #endif
  36. #if EMULATION
  37. //#define malloc pic16_malloc
  38. //#define free pic16_free
  39. //#define realloc pic16_realloc
  40. //#define calloc pic16_calloc
  41. //#define lmalloc pic16_lmalloc
  42. //#define lfree pic16_lfree
  43. //#define lrealloc pic16_lrealloc
  44. //#define lcalloc pic16_lcalloc
  45. #define _MALLOC_SPEC
  46. #else
  47. #pragma library c
  48. #define _MALLOC_SPEC __data
  49. #endif
  50. /* when MALLOC_MAX_FIRST is 1, the memory allocator tries to find a block
  51. * that fits the requested size without merging (initially), if this block
  52. * is not found, then tries to merge adjacent blocks. If MALLOC_MAX_FIRST is
  53. * set 0, then the allocator tries to merge adjacent blocks in the first
  54. * place. Both behaviours may give better results when used in certain
  55. * circumstancs. I.e. if realloc is to be used, leaving some space after the
  56. * block, will allow realloc to allocate it, otherwise it may result in much
  57. * more memory fragmentation. An algorithm can be implemented to allow small
  58. * fragments to be allocated but this is much too complicated for the PIC18F's
  59. * architecture */
  60. #define MALLOC_MAX_FIRST 0
  61. #define MAX_BLOCK_SIZE 0x7f /* 127 bytes */
  62. #define MAX_HEAP_SIZE 0x200 /* 512 bytes */
  63. #define _MAX_HEAP_SIZE (MAX_HEAP_SIZE-1)
  64. #define ALLOC_FLAG 0x80
  65. #define HEADER_SIZE 1
  66. /* memory block header, max size 127 bytes, 126 usable */
  67. typedef union {
  68. unsigned char datum;
  69. struct {
  70. unsigned count: 7;
  71. unsigned alloc: 1;
  72. } bits;
  73. } _malloc_rec;
  74. /* initialize heap, should be called before any call to malloc/realloc/calloc */
  75. void _initHeap(unsigned char _MALLOC_SPEC *dHeap, unsigned int heapsize);
  76. /* start searching for a block of size at least bSize, merge adjacent blocks
  77. * if necessery */
  78. _malloc_rec _MALLOC_SPEC *_mergeHeapBlock(_malloc_rec _MALLOC_SPEC *sBlock, unsigned char bSize);
  79. /* allocate a memory block */
  80. unsigned char _MALLOC_SPEC *malloc(unsigned char len);
  81. /* same as malloc, but clear memory */
  82. unsigned char _MALLOC_SPEC *calloc(unsigned char len);
  83. /* expand or reduce a memory block, if mblock is NULL, then same as malloc */
  84. unsigned char _MALLOC_SPEC *realloc(unsigned char _MALLOC_SPEC *mblock, unsigned char len);
  85. /* free a memory block */
  86. void free(unsigned char _MALLOC_SPEC *);
  87. /* returns the size of all the unallocated memory */
  88. unsigned int memfree(void);
  89. /* return the size of the maximum unallocated memory block */
  90. unsigned int memfreemax(void);
  91. #endif /* __MALLOC_H__ */