Nut/OS  4.10.3
API Reference
heap.h
Go to the documentation of this file.
00001 #ifndef _SYS_HEAP_H_
00002 #define _SYS_HEAP_H_
00003 
00004 /*
00005  * Copyright (C) 2009 by egnite GmbH
00006  * Copyright (C) 2001-2003 by egnite Software GmbH
00007  *
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted provided that the following conditions
00012  * are met:
00013  *
00014  * 1. Redistributions of source code must retain the above copyright
00015  *    notice, this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright
00017  *    notice, this list of conditions and the following disclaimer in the
00018  *    documentation and/or other materials provided with the distribution.
00019  * 3. Neither the name of the copyright holders nor the names of
00020  *    contributors may be used to endorse or promote products derived
00021  *    from this software without specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00024  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00027  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00030  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00031  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00033  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00034  * SUCH DAMAGE.
00035  *
00036  * For additional information see http://www.ethernut.de/
00037  */
00038 
00039 /*
00040  * $Id: heap.h 2609 2009-04-15 13:41:35Z haraldkipp $
00041  */
00042 
00043 #include <cfg/memory.h>
00044 #include <sys/types.h>
00045 
00046 
00056 typedef struct _HEAPNODE HEAPNODE;
00057 
00062 struct _HEAPNODE {
00063     size_t hn_size;     
00064 #ifdef NUTDEBUG_HEAP
00065     HEAPNODE *ht_next;
00066     size_t ht_size;
00067     CONST char *ht_file;
00068     int ht_line;
00069 #endif
00070     HEAPNODE *hn_next;  
00071 };
00072 
00073 
00074 extern HEAPNODE *heapFreeList;
00075 
00076 #define NutHeapAdd(a, s)                NutHeapRootAdd(&heapFreeList, a, s)
00077 #define NutHeapAvailable()              NutHeapRootAvailable(&heapFreeList)
00078 #define NutHeapRegionAvailable()        NutHeapRootRegionAvailable(&heapFreeList)
00079 
00080 #ifdef NUTDEBUG_HEAP
00081 #define NutHeapAlloc(s)                 NutHeapDebugRootAlloc(&heapFreeList, s, __FILE__, __LINE__)
00082 #define NutHeapAllocClear(s)            NutHeapDebugRootAllocClear(&heapFreeList, s, __FILE__, __LINE__)
00083 #define NutHeapFree(p)                  NutHeapDebugRootFree(&heapFreeList, p, __FILE__, __LINE__)
00084 #define NutHeapRealloc(p, s)            NutHeapDebugRootRealloc(&heapFreeList, p, s, __FILE__, __LINE__)
00085 #else
00086 #define NutHeapAlloc(s)                 NutHeapRootAlloc(&heapFreeList, s)
00087 #define NutHeapAllocClear(s)            NutHeapRootAllocClear(&heapFreeList, s)
00088 #define NutHeapFree(p)                  NutHeapRootFree(&heapFreeList, p)
00089 #define NutHeapRealloc(p, s)            NutHeapRootRealloc(&heapFreeList, p, s)
00090 #endif
00091 
00092 #if defined(NUTMEM_STACKHEAP)
00093 /* Dedicated stack memory. */
00094 #ifndef NUTMEM_SPLIT_FAST
00095 #define NUTMEM_SPLIT_FAST
00096 #endif
00097 #define NutStackAdd(a, s)               NutHeapFastMemAdd(a, s)
00098 #define NutStackAlloc(s)                NutHeapFastMemAlloc(s)
00099 #define NutStackFree(p)                 NutHeapFastMemFree(p)
00100 #else /* NUTMEM_STACKHEAP */
00101 /* Thread stacks resides in normal heap. */
00102 #define NutStackAlloc(s)                NutHeapAlloc(s)
00103 #define NutStackFree(p)                 NutHeapFree(p)
00104 #endif /* NUTMEM_STACKHEAP */
00105 
00106 #ifdef NUTMEM_SPLIT_FAST
00107 
00108 extern HEAPNODE *heapFastMemFreeList;
00109 
00110 #define NutHeapFastMemAdd(a, s)         NutHeapRootAdd(&heapFastMemFreeList, a, s)
00111 #define NutHeapFastMemAvailable()       NutHeapRootAvailable(&heapFastMemFreeList)
00112 #define NutHeapFastMemRegionAvailable() NutHeapRootRegionAvailable(&heapFastMemFreeList)
00113 
00114 #ifdef NUTDEBUG_HEAP
00115 #define NutHeapFastMemAlloc(s)          NutHeapDebugRootAlloc(&heapFastMemFreeList, s, __FILE__, __LINE__)
00116 #define NutHeapFastMemAllocClear(s)     NutHeapDebugRootAllocClear(&heapFastMemFreeList, s, __FILE__, __LINE__)
00117 #define NutHeapFastMemFree(p)           NutHeapDebugRootFree(&heapFastMemFreeList, p, __FILE__, __LINE__)
00118 #define NutHeapFastMemRealloc(p, s)     NutHeapDebugRootRealloc(&heapFastMemFreeList, p, s, __FILE__, __LINE__)
00119 #else
00120 #define NutHeapFastMemAlloc(s)          NutHeapRootAlloc(&heapFastMemFreeList, s)
00121 #define NutHeapFastMemAllocClear(s)     NutHeapRootAllocClear(&heapFastMemFreeList, s)
00122 #define NutHeapFastMemFree(p)           NutHeapRootFree(&heapFastMemFreeList, p)
00123 #define NutHeapFastMemRealloc(p, s)     NutHeapRootRealloc(&heapFastMemFreeList, p, s)
00124 #endif
00125 
00126 #endif /* NUTMEM_SPLIT_FAST */
00127 
00128 __BEGIN_DECLS
00129 /* Prototypes */
00130 
00131 extern void NutHeapRootAdd(HEAPNODE** root, void *addr, size_t size);
00132 extern size_t NutHeapRootAvailable(HEAPNODE** root);
00133 extern size_t NutHeapRootRegionAvailable(HEAPNODE** root);
00134 
00135 #ifdef NUTDEBUG_HEAP
00136 extern void *NutHeapDebugRootAlloc(HEAPNODE** root, size_t size, CONST char *file, int line);
00137 extern void *NutHeapDebugRootAllocClear(HEAPNODE** root, size_t size, CONST char *file, int line);
00138 extern int NutHeapDebugRootFree(HEAPNODE** root, void *block, CONST char *file, int line);
00139 extern void *NutHeapDebugRootRealloc(HEAPNODE** root, void * block, size_t size, CONST char *file, int line);
00140 #else
00141 extern void *NutHeapRootAlloc(HEAPNODE** root, size_t size);
00142 extern void *NutHeapRootAllocClear(HEAPNODE** root, size_t size);
00143 extern int NutHeapRootFree(HEAPNODE** root, void *block);
00144 extern void *NutHeapRootRealloc(HEAPNODE** root, void * block, size_t size);
00145 #endif
00146 
00147 extern int NutHeapCheck(void);
00148 extern void NutHeapDump(void * stream);
00149 
00150 /* Prototypes */
00151 __END_DECLS
00152 
00153 #endif