pool.h

Go to the documentation of this file.
00001 #ifndef CTL_POOL_H
00002 #define CTL_POOL_H
00003 
00016 #define ctl_fp_decl(type,count, label )\
00017 typedef type ppConcat(label,_type);\
00018 struct ppConcat(label,_freenode)\
00019 {\
00020     struct ppConcat(label,_freenode)* free;\
00021 };\
00022 typedef struct label\
00023 {\
00024     struct ppConcat(label,_freenode)* free;\
00025     type    data[count];\
00026 } label;
00027 
00033 #define ctl_fp_init(label, instance )\
00034 {\
00035     size_t fpicurr = countof((instance)->data);\
00036     (instance)->free = NULL;\
00037     while( fpicurr-- )\
00038     {\
00039         struct ppConcat(label,_freenode)* fpipCurr = (struct ppConcat(label,_freenode)*) ((instance)->data + fpicurr);\
00040         fpipCurr->free = (instance)->free;\
00041         (instance)->free = fpipCurr;\
00042     }\
00043 }
00044 
00052 #define ctl_fp_valid(label, instance, ptr )     memberof((instance)->data,(ppConcat(label,_type)*)(ptr))
00053 
00063 #define ctl_fp_index(label, instance, ptr )     indexof((instance)->data,(ppConcat(label,_type)*)(ptr))
00064 
00074 #define ctl_fp_lut(label, instance, index )     ( ((unsigned)(index) < sizeof(instance->data)) ? instance->data + (index) : NULL )
00075 
00082 #define ctl_fp_alloc(label, instance, result )\
00083 {\
00084     (result) = (ppConcat(label,_type)*)(instance)->free;\
00085     /* Assert: memory link damage? */\
00086     assert( NULL == (result) || ctl_fp_valid(label, instance, (result) ) );\
00087     if( (instance)->free )\
00088         (instance)->free = (instance)->free->free;\
00089 }
00090 
00097 #define ctl_fp_calloc(label, instance, result )\
00098 {\
00099     ctl_fp_alloc(label, instance, result );\
00100     memset((result),0,sizeof(ppConcat(label,_type)));\
00101 }
00102 
00109 #define ctl_fp_free(label, instance, ptr )\
00110 {\
00111     if( (ptr) )\
00112     {\
00113         struct ppConcat(label,_freenode)* fpipfree = (struct ppConcat(label,_freenode)*)(ptr);\
00114         /* Assert: Memory pointer must belong to this pool */\
00115         assert( ctl_fp_valid(label, instance, ptr ) );\
00116         fpipfree->free = (instance)->free;\
00117         (instance)->free = fpipfree;\
00118         (ptr) = NULL;\
00119     }\
00120 }
00121 
00122 
00123 #endif /* CTL_POOL_H */

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