diff 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
line wrap: on
line diff
--- a/include/mb_tools.h	Sun Jul 26 22:13:36 2009 +0800
+++ b/include/mb_tools.h	Tue Jul 28 15:11:42 2009 +0800
@@ -124,6 +124,21 @@
 	da->ds[da->num++] = v;				\
 	return 0;					\
     }
+#define DARRAY_DEFINE_ADV(name, type)			\
+    static int name ## _adv(name ## _t *da, int n) {	\
+	type *new_ds;					\
+	int max;					\
+	if((da->num + n) > (da)->max) {			\
+	    max = ((da)->num + n + 31) & ~0x1f;		\
+	    new_ds = realloc(da->ds,			\
+			     max * sizeof(type));	\
+	    if(new_ds == NULL) return -1;		\
+	    da->ds = new_ds;				\
+	    da->max = max;				\
+	}						\
+	da->num += n;					\
+	return 0;					\
+    }
 #define DARRAY_CLEAN(da) do { (da)->num = 0; } while(0)
 #define DARRAY_INIT(da) \
     do { (da)->num = (da)->max = 0; (da)->ds = NULL; } while(0)