ekg2
|
00001 #ifndef __EKG_DYNSTUFF_INLINE_H 00002 #define __EKG_DYNSTUFF_INLINE_H 00003 00004 /* we could use typeof() instead of passing paramtype, but let's be more portable */ 00005 00006 #define DYNSTUFF_USE_LIST3 1 00007 00008 #if DYNSTUFF_USE_LIST3 00009 # include <ekg/dynstuff.h> 00010 #endif 00011 00012 #ifdef __cplusplus 00013 extern "C" { 00014 #endif 00015 00016 #if DYNSTUFF_USE_LIST3 00017 00018 #define __DYNSTUFF_LIST_ADD(lista, typ, __notused) \ 00019 void lista##_add(typ *new_) { list_add3((list_t *) (void *) &lista, (list_t) new_); } 00020 00021 #define __DYNSTUFF_LIST_ADD_BEGINNING(lista, typ, __notused) \ 00022 void lista##_add(typ *new_) { list_add_beginning3((list_t *) (void *) &lista, (list_t) new_); } 00023 00024 #define __DYNSTUFF_LIST_ADD_SORTED(lista, typ, comparision) \ 00025 void lista##_add(typ *new_) { list_add_sorted3((list_t *) (void *) &lista, (list_t) new_, (void *) comparision); } 00026 00027 #define __DYNSTUFF_LIST_REMOVE_SAFE(lista, typ, free_func) \ 00028 void lista##_remove(typ *elem) { list_remove3((list_t *) (void *) &lista, (list_t) elem, (void *) free_func); } 00029 00030 #define __DYNSTUFF_LIST_REMOVE_ITER(lista, typ, free_func) \ 00031 typ *lista##_removei(typ *elem) { return list_remove3i((list_t *) (void *) &lista, (list_t) elem, (void *) free_func); } 00032 00033 #define __DYNSTUFF_LIST_UNLINK(lista, typ) \ 00034 void lista##_unlink(typ *elem) { list_unlink3((list_t *) (void *) &lista, (list_t) elem); } 00035 00036 #define __DYNSTUFF_LIST_DESTROY(lista, typ, free_func) \ 00037 void lista##_destroy(void) { list_destroy3((list_t) lista, (void *) free_func); lista = NULL; } 00038 00039 #define __DYNSTUFF_LIST_COUNT(lista, typ) \ 00040 int lista##_count(void) { return list_count((list_t) lista); } 00041 00042 #else 00043 00044 #define __DYNSTUFF_LIST_ADD(lista, typ, __notused) \ 00045 void lista##_add(typ *new_) { \ 00046 new_->next = NULL; \ 00047 if (!lista) { \ 00048 lista = new_; \ 00049 } else { \ 00050 typ *tmp = lista; \ 00051 \ 00052 while (tmp->next) \ 00053 tmp = tmp->next; \ 00054 tmp->next = new_; \ 00055 } \ 00056 } 00057 00058 #define __DYNSTUFF_LIST_ADD_BEGINNING(lista, typ, __notused) \ 00059 void lista##_add(typ *new_) { \ 00060 new_->next = lista; \ 00061 lista = new_; \ 00062 } 00063 00064 #define __DYNSTUFF_LIST_ADD_SORTED(lista, typ, comparision) \ 00065 void lista##_add(typ *new_) { \ 00066 new_->next = NULL; \ 00067 if (!lista) { \ 00068 lista = new_; \ 00069 } else { \ 00070 typ *tmp = lista; \ 00071 typ *prev = NULL; \ 00072 \ 00073 while (comparision(new_, tmp) > 0) { \ 00074 prev = tmp; \ 00075 tmp = tmp->next; \ 00076 if (!tmp) \ 00077 break; \ 00078 } \ 00079 \ 00080 if (!prev) { \ 00081 new_->next = lista; \ 00082 lista = new_; \ 00083 } else { \ 00084 prev->next = new_; \ 00085 new_->next = tmp; \ 00086 } \ 00087 } \ 00088 } 00089 00090 #define __DYNSTUFF_LIST_REMOVE_SAFE(lista, typ, free_func) \ 00091 void lista##_remove(typ *elem) { \ 00092 if (!lista) /* programmer's fault */ \ 00093 return; \ 00094 \ 00095 if (lista == elem) \ 00096 lista = lista->next; \ 00097 else { \ 00098 typ *tmp, *last = lista; \ 00099 \ 00100 for (tmp = lista->next; tmp; tmp = tmp->next) { \ 00101 if (tmp == elem) \ 00102 break; \ 00103 if (tmp->next == NULL) { \ 00104 /* errno = ENOENT; */ \ 00105 return; \ 00106 } \ 00107 last = tmp; \ 00108 } \ 00109 last->next = tmp->next; \ 00110 } \ 00111 /* if (free_func) */ \ 00112 free_func(elem); \ 00113 xfree(elem); \ 00114 } 00115 00116 #define __DYNSTUFF_LIST_REMOVE_ITER(lista, typ, free_func) \ 00117 typ *lista##_removei(typ *elem) { \ 00118 typ *ret; \ 00119 \ 00120 if (lista == elem) { \ 00121 lista = lista->next; \ 00122 ret = (typ *) &lista; \ 00123 } else { \ 00124 typ *tmp, *last = lista; \ 00125 \ 00126 for (tmp = lista->next; tmp; tmp = tmp->next) { \ 00127 if (tmp == elem) \ 00128 break; \ 00129 last = tmp; \ 00130 } \ 00131 last->next = tmp->next; \ 00132 ret = last; \ 00133 } \ 00134 /* if (free_func) */ \ 00135 free_func(elem); \ 00136 xfree(elem); \ 00137 return ret; \ 00138 } 00139 00140 #define __DYNSTUFF_LIST_UNLINK(lista, typ) \ 00141 void lista##_unlink(typ *elem) { \ 00142 if (!lista) /* programmer's fault */ \ 00143 return; \ 00144 \ 00145 if (lista == elem) \ 00146 lista = lista->next; \ 00147 else { \ 00148 typ *tmp, *last = lista; \ 00149 \ 00150 for (tmp = lista->next; tmp; tmp = tmp->next) { \ 00151 if (tmp == elem) \ 00152 break; \ 00153 if (tmp->next == NULL) { \ 00154 /* errno = ENOENT; */ \ 00155 return; \ 00156 } \ 00157 last = tmp; \ 00158 } \ 00159 last->next = tmp->next; \ 00160 } \ 00161 } 00162 00163 #define __DYNSTUFF_LIST_DESTROY(lista, typ, free_func) \ 00164 void lista##_destroy(void) { \ 00165 while (lista) { \ 00166 typ *tmp = lista; \ 00167 \ 00168 lista = lista->next; \ 00169 \ 00170 /* if (free_func) */ \ 00171 free_func(tmp); \ 00172 \ 00173 xfree(tmp); \ 00174 } \ 00175 } 00176 00177 #define __DYNSTUFF_LIST_COUNT(lista, typ) \ 00178 int lista##_count(void) { \ 00179 int count = 0; \ 00180 typ *list; \ 00181 \ 00182 for (list = lista; list; list = list->next) \ 00183 count++; \ 00184 return count; \ 00185 } 00186 00187 #endif /* !DYNSTUFF_USE_LIST3 */ 00188 00189 /* !!! for other lists !!! [when we (have many || don't know) head of list during compilation time] */ 00190 00191 #if DYNSTUFF_USE_LIST3 00192 00193 #define __DYNSTUFF_ADD(prefix, typ, __notused) \ 00194 void prefix##_add(typ **lista, typ *new_) { list_add3((list_t *) lista, (list_t) new_); } 00195 00196 #define __DYNSTUFF_ADD_BEGINNING(prefix, typ, __notused) \ 00197 void prefix##_add(typ **lista, typ *new_) { list_add_beginning3((list_t *) lista, (list_t) new_); } 00198 00199 #define __DYNSTUFF_ADD_SORTED(prefix, typ, comparision) \ 00200 void prefix##_add(typ **lista, typ *new_) { list_add_sorted3((list_t *) lista, (list_t) new_, (void *) comparision); } 00201 00202 #define __DYNSTUFF_REMOVE_SAFE(prefix, typ, free_func) \ 00203 void prefix##_remove(typ **lista, typ *elem) { \ 00204 list_remove3((list_t *) lista, (list_t) elem, (void *) free_func); \ 00205 } 00206 00207 #define __DYNSTUFF_REMOVE_ITER(prefix, typ, free_func) \ 00208 typ *prefix##_removei(typ **lista, typ *elem) { \ 00209 return list_remove3i((list_t *) lista, (list_t) elem, (void *) free_func); \ 00210 } 00211 00212 #define __DYNSTUFF_DESTROY(prefix, typ, free_func) \ 00213 void prefix##_destroy(typ **lista) { \ 00214 list_destroy3((list_t) *lista, (void *) free_func); \ 00215 *lista = NULL; \ 00216 } 00217 00218 #define __DYNSTUFF_COUNT(prefix, typ) \ 00219 int prefix##_count(typ *lista) { \ 00220 return list_count((list_t) lista); \ 00221 } 00222 00223 #define __DYNSTUFF_GET_NTH(prefix, typ) \ 00224 typ *prefix##_get_nth(typ *lista, int id) { \ 00225 return list_get_nth3((list_t) lista, id); \ 00226 } 00227 00228 #else 00229 /* XXX, checkit */ 00230 00231 #define __DYNSTUFF_ADD(prefix, typ, __notused) \ 00232 void prefix##_add(typ **lista, typ *new_) { \ 00233 typ *tmp = *lista; \ 00234 \ 00235 new_->next = NULL; \ 00236 if (!(tmp = *lista)) { \ 00237 *lista = new_; \ 00238 } else { \ 00239 while (tmp->next) \ 00240 tmp = tmp->next; \ 00241 tmp->next = new_; \ 00242 } \ 00243 } 00244 00245 #define __DYNSTUFF_ADD_BEGINNING(prefix, typ, __notused) \ 00246 void prefix##_add(typ **lista, typ *new_) { \ 00247 new_->next = *lista; \ 00248 *lista = new_; \ 00249 } 00250 00251 #define __DYNSTUFF_ADD_SORTED(prefix, typ, comparision) \ 00252 void prefix##_add(typ **lista, typ *new_) { \ 00253 typ *tmp; \ 00254 \ 00255 new_->next = NULL; \ 00256 if (!(tmp = *lista)) { \ 00257 *lista = new_; \ 00258 } else { \ 00259 typ *prev = NULL; \ 00260 \ 00261 while (comparision(new_, tmp) > 0) { \ 00262 prev = tmp; \ 00263 tmp = tmp->next; \ 00264 if (!tmp) \ 00265 break; \ 00266 } \ 00267 \ 00268 if (!prev) { \ 00269 new_->next = *lista; \ 00270 *lista = new_; \ 00271 } else { \ 00272 prev->next = new_; \ 00273 new_->next = tmp; \ 00274 } \ 00275 } \ 00276 } 00277 00278 #define __DYNSTUFF_REMOVE_SAFE(prefix, typ, free_func) \ 00279 void prefix##_remove(typ **lista, typ *elem) { \ 00280 if (!lista || !(*lista)) /* programmer's fault */\ 00281 return; \ 00282 \ 00283 if (*lista == elem) \ 00284 *lista = (*lista)->next; \ 00285 else { \ 00286 typ *tmp, *last = *lista; \ 00287 \ 00288 for (tmp = (*lista)->next; tmp; tmp = tmp->next) { \ 00289 if (tmp == elem) \ 00290 break; \ 00291 if (tmp->next == NULL) { \ 00292 /* errno = ENOENT; */ \ 00293 return; \ 00294 } \ 00295 last = tmp; \ 00296 } \ 00297 last->next = tmp->next; \ 00298 } \ 00299 /* if (free_func) */ \ 00300 free_func(elem); \ 00301 xfree(elem); \ 00302 } 00303 00304 #define __DYNSTUFF_REMOVE_ITER(prefix, typ, free_func) \ 00305 typ *prefix##_removei(typ **lista, typ *elem) { \ 00306 typ *ret; \ 00307 \ 00308 if (*lista == elem) { \ 00309 *lista = (*lista)->next; \ 00310 ret = (typ *) lista; \ 00311 } else { \ 00312 typ *tmp, *last = *lista; \ 00313 \ 00314 for (tmp = (*lista)->next; tmp; tmp = tmp->next) { \ 00315 if (tmp == elem) \ 00316 break; \ 00317 last = tmp; \ 00318 } \ 00319 last->next = tmp->next; \ 00320 ret = last; \ 00321 } \ 00322 /* if (free_func) */ \ 00323 free_func(elem); \ 00324 xfree(elem); \ 00325 return ret; \ 00326 } 00327 00328 #define __DYNSTUFF_UNLINK(prefix, typ) \ 00329 void prefix##_unlink(typ **lista, typ *elem) { \ 00330 if (!lista || !(*lista)) /* programmer's fault */ \ 00331 return; \ 00332 \ 00333 if (*lista == elem) \ 00334 *lista = (*lista)->next; \ 00335 else { \ 00336 typ *tmp, *last = *lista; \ 00337 \ 00338 for (tmp = (*lista)->next; tmp; tmp = tmp->next) { \ 00339 if (tmp == elem) \ 00340 break; \ 00341 if (tmp->next == NULL) { \ 00342 /* errno = ENOENT; */ \ 00343 return; \ 00344 } \ 00345 last = tmp; \ 00346 } \ 00347 last->next = tmp->next; \ 00348 } \ 00349 } 00350 00351 #define __DYNSTUFF_DESTROY(prefix, typ, free_func) \ 00352 void prefix##_destroy(typ **lista) { \ 00353 while (*lista) { \ 00354 typ *tmp = *lista; \ 00355 \ 00356 *lista = (*lista)->next; \ 00357 \ 00358 /* if (free_func) */ \ 00359 free_func(tmp); \ 00360 \ 00361 xfree(tmp); \ 00362 } \ 00363 } 00364 00365 #define __DYNSTUFF_COUNT(prefix, typ) \ 00366 int prefix##_count(typ *list) { \ 00367 int count = 0; \ 00368 \ 00369 for (; list; list = list->next) \ 00370 count++; \ 00371 return count; \ 00372 } 00373 00374 #define __DYNSTUFF_GET_NTH(prefix, typ) \ 00375 typ *prefix##_get_nth(typ *lista, int id) { \ 00376 while (lista) { \ 00377 if ((--id) == 0) \ 00378 return lista; \ 00379 \ 00380 lista = lista->next; \ 00381 } \ 00382 return NULL; \ 00383 } 00384 00385 #endif 00386 00387 #define __DYNSTUFF_NOREMOVE(lista, typ, free_func) 00388 #define __DYNSTUFF_NOUNLINK(lista, typ) 00389 #define __DYNSTUFF_NOCOUNT(lista, typ) 00390 #define __DYNSTUFF_NODESTROY(lista, typ, free_func) 00391 00392 #define DYNSTUFF_LIST_DECLARE_FULL(lista, type, compare_func, free_func, list_add, list_remove, list_remove2, list_unlink, list_destroy, list_count) \ 00393 list_add(lista, type, compare_func) \ 00394 list_remove(lista, type, free_func) \ 00395 list_remove2(lista, type, free_func) \ 00396 list_unlink(lista, type) \ 00397 list_destroy(lista, type, free_func) \ 00398 list_count(lista, type) 00399 00400 #define DYNSTUFF_LIST_DECLARE(lista, type, free_func, list_add, list_remove, list_destroy) \ 00401 DYNSTUFF_LIST_DECLARE_WC(lista, type, free_func, list_add, list_remove, list_destroy, __DYNSTUFF_NOCOUNT) 00402 00403 #define DYNSTUFF_LIST_DECLARE_NF(lista, type, list_add, list_unlink) \ 00404 DYNSTUFF_LIST_DECLARE_FULL(lista, type, NULL, NULL, list_add, __DYNSTUFF_NOREMOVE, __DYNSTUFF_NOREMOVE, list_unlink, __DYNSTUFF_NODESTROY, __DYNSTUFF_NOCOUNT) 00405 00406 #define DYNSTUFF_LIST_DECLARE_WC(lista, type, free_func, list_add, list_remove, list_destroy, list_count) \ 00407 DYNSTUFF_LIST_DECLARE_FULL(lista, type, NULL, free_func, list_add, list_remove, __DYNSTUFF_NOREMOVE, __DYNSTUFF_NOUNLINK, list_destroy, list_count) 00408 00409 #define DYNSTUFF_LIST_DECLARE_SORTED(lista, type, compare_func, free_func, list_add, list_remove, list_destroy) \ 00410 DYNSTUFF_LIST_DECLARE_FULL(lista, type, compare_func, free_func, list_add, list_remove, __DYNSTUFF_NOREMOVE, __DYNSTUFF_NOUNLINK, list_destroy, __DYNSTUFF_NOCOUNT) 00411 00412 00413 #define DYNSTUFF_LIST_DECLARE2(lista, type, free_func, list_add, list_remove, list_remove2, list_destroy) \ 00414 DYNSTUFF_LIST_DECLARE_FULL(lista, type, NULL, free_func, list_add, list_remove, list_remove2, __DYNSTUFF_NOUNLINK, list_destroy, __DYNSTUFF_NOCOUNT) 00415 00416 #define DYNSTUFF_LIST_DECLARE2_SORTED(lista, type, compare_func, free_func, list_add, list_remove, list_remove2, list_destroy) \ 00417 DYNSTUFF_LIST_DECLARE_FULL(lista, type, compare_func, free_func, list_add, list_remove, list_remove2, __DYNSTUFF_NOUNLINK, list_destroy, __DYNSTUFF_NOCOUNT) 00418 00419 #define DYNSTUFF_LIST_DECLARE_SORTED_NF(lista, type, compare_func, list_add, list_unlink) \ 00420 DYNSTUFF_LIST_DECLARE_FULL(lista, type, compare_func, NULL, list_add, __DYNSTUFF_NOREMOVE, __DYNSTUFF_NOREMOVE, list_unlink, __DYNSTUFF_NODESTROY, __DYNSTUFF_NOCOUNT) 00421 00422 #ifdef __cplusplus 00423 } 00424 #endif 00425 00426 #endif