comparison include/mb_tools.h @ 422:c6c0d017dc8e

Use DARRAY to manage variable length list
author Thinker K.F. Li <thinker@branda.to>
date Tue, 28 Jul 2009 15:11:42 +0800
parents 44b8223f307c
children 586e50f82c1f
comparison
equal deleted inserted replaced
421:1e48453bb282 422:c6c0d017dc8e
122 da->max = max; \ 122 da->max = max; \
123 } \ 123 } \
124 da->ds[da->num++] = v; \ 124 da->ds[da->num++] = v; \
125 return 0; \ 125 return 0; \
126 } 126 }
127 #define DARRAY_DEFINE_ADV(name, type) \
128 static int name ## _adv(name ## _t *da, int n) { \
129 type *new_ds; \
130 int max; \
131 if((da->num + n) > (da)->max) { \
132 max = ((da)->num + n + 31) & ~0x1f; \
133 new_ds = realloc(da->ds, \
134 max * sizeof(type)); \
135 if(new_ds == NULL) return -1; \
136 da->ds = new_ds; \
137 da->max = max; \
138 } \
139 da->num += n; \
140 return 0; \
141 }
127 #define DARRAY_CLEAN(da) do { (da)->num = 0; } while(0) 142 #define DARRAY_CLEAN(da) do { (da)->num = 0; } while(0)
128 #define DARRAY_INIT(da) \ 143 #define DARRAY_INIT(da) \
129 do { (da)->num = (da)->max = 0; (da)->ds = NULL; } while(0) 144 do { (da)->num = (da)->max = 0; (da)->ds = NULL; } while(0)
130 #define DARRAY_DESTROY(da) do { if((da)->ds) free((da)->ds); } while(0) 145 #define DARRAY_DESTROY(da) do { if((da)->ds) free((da)->ds); } while(0)
131 /* @} */ 146 /* @} */