heap.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef _SYS_HEAP_H_
  2. #define _SYS_HEAP_H_
  3. /*
  4. * Copyright (C) 2009 by egnite GmbH
  5. * Copyright (C) 2001-2003 by egnite Software GmbH
  6. *
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the copyright holders nor the names of
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  32. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * For additional information see http://www.ethernut.de/
  36. */
  37. /*
  38. * $Id: heap.h 4477 2012-08-20 17:50:01Z haraldkipp $
  39. */
  40. #include <cfg/memory.h>
  41. #include <sys/types.h>
  42. /*!
  43. * \file sys/heap.h
  44. * \brief Heap management definitions.
  45. */
  46. /*!
  47. * \struct _HEAPNODE heap.h sys/heap.h
  48. * \brief Heap memory node information structure.
  49. */
  50. typedef struct _HEAPNODE HEAPNODE;
  51. /*!
  52. * \typedef HEAPNODE
  53. * \brief Heap memory node type.
  54. */
  55. struct _HEAPNODE {
  56. size_t hn_size; /*!< \brief Size of this node. */
  57. #ifdef NUTDEBUG_HEAP
  58. HEAPNODE *ht_next;
  59. size_t ht_size;
  60. const char *ht_file;
  61. int ht_line;
  62. #endif
  63. HEAPNODE *hn_next; /*!< \brief Link to next free node. */
  64. };
  65. extern HEAPNODE *heapFreeList;
  66. #define NutHeapAdd(a, s) NutHeapRootAdd(&heapFreeList, a, s)
  67. #define NutHeapAvailable() NutHeapRootAvailable(&heapFreeList)
  68. #define NutHeapRegionAvailable() NutHeapRootRegionAvailable(&heapFreeList)
  69. #ifdef NUTDEBUG_HEAP
  70. #define NutHeapAlloc(s) NutHeapDebugRootAlloc(&heapFreeList, s, __FILE__, __LINE__)
  71. #define NutHeapAllocClear(s) NutHeapDebugRootAllocClear(&heapFreeList, s, __FILE__, __LINE__)
  72. #define NutHeapFree(p) NutHeapDebugRootFree(&heapFreeList, p, __FILE__, __LINE__)
  73. #define NutHeapRealloc(p, s) NutHeapDebugRootRealloc(&heapFreeList, p, s, __FILE__, __LINE__)
  74. #else
  75. #define NutHeapAlloc(s) NutHeapRootAlloc(&heapFreeList, s)
  76. #define NutHeapAllocClear(s) NutHeapRootAllocClear(&heapFreeList, s)
  77. #define NutHeapFree(p) NutHeapRootFree(&heapFreeList, p)
  78. #define NutHeapRealloc(p, s) NutHeapRootRealloc(&heapFreeList, p, s)
  79. #endif
  80. #if defined(NUTMEM_STACKHEAP)
  81. /* Dedicated stack memory. */
  82. #ifndef NUTMEM_SPLIT_FAST
  83. #define NUTMEM_SPLIT_FAST
  84. #endif
  85. #define NutStackAdd(a, s) NutHeapFastMemAdd(a, s)
  86. #define NutStackAlloc(s) NutHeapFastMemAlloc(s)
  87. #define NutStackFree(p) NutHeapFastMemFree(p)
  88. #else /* NUTMEM_STACKHEAP */
  89. /* Thread stacks resides in normal heap. */
  90. #define NutStackAlloc(s) NutHeapAlloc(s)
  91. #define NutStackFree(p) NutHeapFree(p)
  92. #endif /* NUTMEM_STACKHEAP */
  93. #ifdef NUTMEM_SPLIT_FAST
  94. extern HEAPNODE *heapFastMemFreeList;
  95. #define NutHeapFastMemAdd(a, s) NutHeapRootAdd(&heapFastMemFreeList, a, s)
  96. #define NutHeapFastMemAvailable() NutHeapRootAvailable(&heapFastMemFreeList)
  97. #define NutHeapFastMemRegionAvailable() NutHeapRootRegionAvailable(&heapFastMemFreeList)
  98. #ifdef NUTDEBUG_HEAP
  99. #define NutHeapFastMemAlloc(s) NutHeapDebugRootAlloc(&heapFastMemFreeList, s, __FILE__, __LINE__)
  100. #define NutHeapFastMemAllocClear(s) NutHeapDebugRootAllocClear(&heapFastMemFreeList, s, __FILE__, __LINE__)
  101. #define NutHeapFastMemFree(p) NutHeapDebugRootFree(&heapFastMemFreeList, p, __FILE__, __LINE__)
  102. #define NutHeapFastMemRealloc(p, s) NutHeapDebugRootRealloc(&heapFastMemFreeList, p, s, __FILE__, __LINE__)
  103. #else
  104. #define NutHeapFastMemAlloc(s) NutHeapRootAlloc(&heapFastMemFreeList, s)
  105. #define NutHeapFastMemAllocClear(s) NutHeapRootAllocClear(&heapFastMemFreeList, s)
  106. #define NutHeapFastMemFree(p) NutHeapRootFree(&heapFastMemFreeList, p)
  107. #define NutHeapFastMemRealloc(p, s) NutHeapRootRealloc(&heapFastMemFreeList, p, s)
  108. #endif
  109. #endif /* NUTMEM_SPLIT_FAST */
  110. extern void NutHeapRootAdd(HEAPNODE** root, void *addr, size_t size);
  111. extern size_t NutHeapRootAvailable(HEAPNODE** root);
  112. extern size_t NutHeapRootRegionAvailable(HEAPNODE** root);
  113. #ifdef NUTDEBUG_HEAP
  114. extern void *NutHeapDebugRootAlloc(HEAPNODE** root, size_t size, const char *file, int line);
  115. extern void *NutHeapDebugRootAllocClear(HEAPNODE** root, size_t size, const char *file, int line);
  116. extern int NutHeapDebugRootFree(HEAPNODE** root, void *block, const char *file, int line);
  117. extern void *NutHeapDebugRootRealloc(HEAPNODE** root, void * block, size_t size, const char *file, int line);
  118. #else
  119. extern void *NutHeapRootAlloc(HEAPNODE** root, size_t size);
  120. extern void *NutHeapRootAllocClear(HEAPNODE** root, size_t size);
  121. extern int NutHeapRootFree(HEAPNODE** root, void *block);
  122. extern void *NutHeapRootRealloc(HEAPNODE** root, void * block, size_t size);
  123. #endif
  124. extern int NutHeapCheck(void);
  125. extern void NutHeapDump(void * stream);
  126. #endif