heap.h

Go to the documentation of this file.
00001 #ifndef CTL_HEAP_H
00002 #define CTL_HEAP_H
00003 
00013 typedef struct ctl_heap
00014 {
00015     const uint8*    begin;  
00016     const uint8*    end;    
00017     uint8*          curr;   
00018 } ctl_heap;
00019 
00026 #define ctl_heap_init( heap, buff, size )\
00027 {\
00028     (heap)->begin = (heap)->curr = (uint8*)(buff);\
00029     (heap)->end = (heap)->begin + (size);\
00030 }
00031 
00037 #define ctl_heap_auto( heapname, size )\
00038     static uint8* ppConcat(heapname,_buff)[size];\
00039     ctl_heap heapname = { ppConcat(heapname,_buff), ppConcat(heapname,_buff), ppConcat(heapname,_buff) + (size) }
00040 
00045 #define ctl_heap_size(heap)     ((heap)->end - (heap)->begin)
00046 
00051 #define ctl_heap_used(heap)     ((heap)->curr - (heap)->begin)
00052 
00057 #define ctl_heap_remain(heap)   ((heap)->end - (heap)->curr)
00058 
00065 #define ctl_heap_alloc( heap, ptr, size )\
00066 {\
00067     ctl_heap* pheap = (heap);\
00068     size_t size_aligned = ((size_t)(size) + 7) & ~7;\
00069     if( pheap->curr + size_aligned <= pheap->end )\
00070     {   /* Enough heap left */\
00071         *((void**)&(ptr)) = pheap->curr;\
00072         pheap->curr += size_aligned;\
00073     }\
00074     else\
00075     {\
00076         throwassert("Not enough heap left");\
00077         (ptr) = NULL;\
00078     }\
00079 }
00080 
00086 #define ctl_heap_free( heap, ptr )\
00087 {\
00088     ctl_heap* pheap = (heap);\
00089     void** pptr = &(ptr);\
00090     if( *pptr >= pheap->begin && *pptr < pheap->end )\
00091     {   /* Good heap pointer */\
00092         pheap->curr = (uint8*)*pptr;\
00093     }\
00094     else\
00095     {\
00096         throwassert("Bad heap pointer");\
00097     }\
00098     *pptr = NULL;\
00099 }
00100 
00101 #endif /* CTL_HEAP_H */
00102 

Generated on Fri Jan 2 15:28:34 2009 for Squat by  doxygen 1.5.6