void cli_shutdown(struct cli_state *cli) { struct cli_state *cli_head; if (cli == NULL) { return; } DLIST_HEAD(cli, cli_head); if (cli_head == cli) { /* * head of a DFS list, shutdown all subsidiary DFS * connections. */ struct cli_state *p, *next; for (p = cli_head->next; p; p = next) { next = p->next; DLIST_REMOVE(cli_head, p); _cli_shutdown(p); } } else { DLIST_REMOVE(cli_head, cli); } _cli_shutdown(cli); }
int main(int argc, char *argv[]) { myelmt_t *newelmt; myelmt_t *elmt; myctx_t *ctx; int i; int action; int j; int r; ctx = malloc(sizeof (myctx_t)); DLIST_INIT(ctx, headlist); DLIST_INIT(ctx, taillist); for ( i = 0; i < 50000000 ; i++) { action = rand_range(0, 9); printf("action: %i\n", action); switch (action) { case ACTION_INSHEAD: elmt = malloc(sizeof (myelmt_t)); elmt->blah = i; DLIST_INS_HEAD(ctx, headlist, elmt); break; case ACTION_INSTAIL: elmt = malloc(sizeof (myelmt_t)); elmt->blah = i; DLIST_INS_TAIL(ctx, headlist, elmt); break; case ACTION_INSNEXT: printf("* insert after a random element.\n"); newelmt = malloc(sizeof (myelmt_t)); newelmt->blah = i; if (DLIST_IS_EMPTY(ctx, headlist)) { printf(" list is empty\n"); DLIST_INS_HEAD(ctx, headlist, newelmt); continue ; } r = rand_range(0, DLIST_COUNT(ctx, headlist)); for (j = 0, elmt = DLIST_HEAD(ctx, headlist) ; j < r; j++, elmt = DLIST_NEXT(headlist, elmt)) ; DLIST_INS_NEXT(ctx, headlist, elmt, newelmt); break; case ACTION_INSPREV: printf("* insert before a random element.\n"); newelmt = malloc(sizeof (myelmt_t)); newelmt->blah = i; if (DLIST_IS_EMPTY(ctx, headlist)) { printf(" list is empty\n"); DLIST_INS_HEAD(ctx, headlist, newelmt); continue ; } r = rand_range(0, DLIST_COUNT(ctx, headlist)); for (j = 0, elmt = DLIST_HEAD(ctx, headlist) ; j < r; j++, elmt = DLIST_NEXT(headlist, elmt)) ; DLIST_INS_PREV(ctx, headlist, elmt, newelmt); break; case ACTION_UNLINK1: case ACTION_UNLINK2: case ACTION_UNLINK3: case ACTION_UNLINK4: case ACTION_UNLINK5: printf("* unlink a random element.\n"); if (DLIST_IS_EMPTY(ctx, headlist)) { printf(" list is empty\n"); continue ; } r = rand_range(0, DLIST_COUNT(ctx, headlist)); for (j = 0, elmt = DLIST_HEAD(ctx, headlist) ; j < r; j++, elmt = DLIST_NEXT(headlist, elmt)) ; DLIST_UNLINK(ctx, headlist, elmt); free(elmt); break; case ACTION_SHOWLIST: printf("* show list content: (%i elements)\n", DLIST_COUNT(ctx, headlist)); if ( DLIST_IS_EMPTY(ctx, headlist) ) { printf(" list is empty\n"); } else { j = 0; DLIST_FOR_EACH(elmt, ctx, headlist) { printf(" elmt %i: val: %i\n", j, elmt->blah); j++; } } break; } }