static void test_01() { int i; slist sl; slist *l = &sl; slist_init(&sl); for (i = 0; i < 255; i++) { slist_add(&sl, (void *)i); } for (i = 0; i < 128; i++) { slist_remove_first(&sl); } for (i = 0; i < 128; i++) { slist_add(&sl, (void *)(i + 255)); } ASSERT((int)slist_get(&sl, 127) == 255); ASSERT((int)slist_get(&sl, 254) == 129 + 253); ASSERT((int)slist_length(&sl) == 255); /* dump(&sl); */ /* printf("==\n"); */ slist_add(&sl, (void *)(128 + 255)); ASSERT((int)slist_get(&sl, 127) == 255); /* ASSERT((int)slist_get(&sl, 255) == 128 + 255); */ ASSERT((int)slist_length(&sl) == 256); /* dump(&sl); */ }
static void test_itr_subr_01(slist *l) { int i; for (i = 0; i < slist_length(l); i++) slist_set(l, i, (void *)(i + 1)); slist_itr_first(l); ASSERT((int)slist_itr_next(l) == 1); /* normal iterate */ ASSERT((int)slist_itr_next(l) == 2); /* normal iterate */ slist_remove(l, 2); /* remove next. "3" is removed */ ASSERT((int)slist_itr_next(l) == 4); /* removed item is skipped */ slist_remove(l, 1); /* remove past item. "2" is removed */ ASSERT((int)slist_itr_next(l) == 5); /* no influence */ ASSERT((int)slist_get(l, 0) == 1); /* checking for removing */ ASSERT((int)slist_get(l, 1) == 4); /* checking for removing */ ASSERT((int)slist_get(l, 2) == 5); /* checking for removing */ /* * Total number was 255. We removed 2 items and iterated 4 times. * 1 removing was past item, so the remaining is 250. */ for (i = 0; i < 249; i++) ASSERT(slist_itr_next(l) != NULL); ASSERT(slist_itr_next(l) != NULL); ASSERT(slist_itr_next(l) == NULL); /* * Same as above except removing before getting the last item. */ /* Reset (253 items) */ for (i = 0; i < slist_length(l); i++) slist_set(l, i, (void *)(i + 1)); slist_itr_first(l); ASSERT(slist_length(l) == 253); for (i = 0; i < 252; i++) ASSERT(slist_itr_next(l) != NULL); slist_remove(l, 252); ASSERT(slist_itr_next(l) == NULL); /* The last item is NULL */ slist_itr_first(l); while (slist_length(l) > 0) slist_remove_first(l); ASSERT(slist_length(l) == 0); ASSERT(slist_itr_next(l) == NULL); }
/* * Add a {@link :l2tpd_listener} to the {@link ::l2tpd L2TP daemon} * @param _this {@link ::l2tpd L2TP daemon} * @param idx index of the lisnter * @param tun_name tunnel name (ex. "L2TP") * @param bindaddr bind address */ int l2tpd_add_listener(l2tpd *_this, int idx, struct l2tp_conf *conf, struct sockaddr *addr) { l2tpd_listener *plistener, *plsnr; plistener = NULL; if (idx == 0 && slist_length(&_this->listener) > 0) { slist_itr_first(&_this->listener); while (slist_itr_has_next(&_this->listener)) { slist_itr_next(&_this->listener); plsnr = slist_itr_remove(&_this->listener); L2TPD_ASSERT(plsnr != NULL); L2TPD_ASSERT(plsnr->sock == -1); free(plsnr); } } L2TPD_ASSERT(slist_length(&_this->listener) == idx); if (slist_length(&_this->listener) != idx) { l2tpd_log(_this, LOG_ERR, "Invalid argument error on %s(): idx must be %d but %d", __func__, slist_length(&_this->listener), idx); goto fail; } if ((plistener = calloc(1, sizeof(l2tpd_listener))) == NULL) { l2tpd_log(_this, LOG_ERR, "calloc() failed in %s: %m", __func__); goto fail; } L2TPD_ASSERT(sizeof(plistener->bind) >= addr->sa_len); memcpy(&plistener->bind, addr, addr->sa_len); if (plistener->bind.sin6.sin6_port == 0) plistener->bind.sin6.sin6_port = htons(L2TPD_DEFAULT_UDP_PORT); plistener->sock = -1; plistener->self = _this; plistener->index = idx; plistener->conf = conf; strlcpy(plistener->tun_name, conf->name, sizeof(plistener->tun_name)); if (slist_add(&_this->listener, plistener) == NULL) { l2tpd_log(_this, LOG_ERR, "slist_add() failed in %s: %m", __func__); goto fail; } return 0; fail: free(plistener); return 1; }
static void test_11() { slist sl; slist *l = &sl; slist_init(l); slist_add(l, (void *)1); slist_add(l, (void *)2); ASSERT((int)slist_remove_last(l) == 2); ASSERT((int)slist_length(l) == 1); ASSERT((int)slist_remove_last(l) == 1); ASSERT((int)slist_length(l) == 0); }
/** * Remove the search from the gui and update all widget accordingly. */ void search_gui_remove_search(search_t *search) { GtkTreeIter iter; GtkTreeModel *model; bool stopped; g_return_if_fail(search); stopped = search_gui_start_massive_update(search); if (search_gui_get_current_search() == search) { GtkTreeView *tv = GTK_TREE_VIEW(search->tree); tree_view_save_widths(tv, PROP_SEARCH_RESULTS_COL_WIDTHS); tree_view_save_visibility(tv, PROP_SEARCH_RESULTS_COL_VISIBLE); tree_view_motion_clear_callback(&tvm_search); } g_assert(0 == slist_length(search->queue)); slist_free(&search->queue); model = gtk_tree_view_get_model(tree_view_search); if (tree_find_iter_by_data(model, c_sl_sch, search, &iter)) { gtk_list_store_remove(GTK_LIST_STORE(model), &iter); } if (stopped) search_gui_end_massive_update(search); }
/* disconnect all calls on the control context * @return return # of calls that is not waiting cleanup. */ static int l2tp_ctrl_disconnect_all_calls(l2tp_ctrl *_this, int drop) { int i, len, ncalls; l2tp_call *call; L2TP_CTRL_ASSERT(_this != NULL); ncalls = 0; len = slist_length(&_this->call_list); for (i = 0; i < len; i++) { call = slist_get(&_this->call_list, i); if (call->state != L2TP_CALL_STATE_CLEANUP_WAIT) { ncalls++; if (l2tp_ctrl_txwin_is_full(_this)) { L2TP_CTRL_DBG((_this, LOG_INFO, "Too many calls. Sending window is not " "enough to send CDN to all clients.")); if (drop) l2tp_call_drop(call); } else l2tp_call_admin_disconnect(call); } } return ncalls; }
/* * nortify disconnection to peer * * @return 0: all CDN and StopCCN have been sent. * N: if the remaining calls which still not sent CDN exist, * return # of the calls. * -1: when try to send of StopCCN failed. */ static int l2tp_ctrl_send_disconnect_notify(l2tp_ctrl *_this) { int ncalls; L2TP_CTRL_ASSERT(_this != NULL) L2TP_CTRL_ASSERT(_this->state == L2TP_CTRL_STATE_ESTABLISHED || _this->state == L2TP_CTRL_STATE_CLEANUP_WAIT); /* this control is not actively closing or StopCCN have been sent */ if (_this->active_closing == 0) return 0; /* Send CDN all Calls */ ncalls = 0; if (slist_length(&_this->call_list) != 0) { ncalls = l2tp_ctrl_disconnect_all_calls(_this, 0); if (ncalls > 0) { /* * Call the function again to check whether the * sending window is fulled. In case ncalls == 0, * it means we've sent CDN for all calls. */ ncalls = l2tp_ctrl_disconnect_all_calls(_this, 0); } } if (ncalls > 0) return ncalls; if (l2tp_ctrl_send_StopCCN(_this, _this->active_closing) != 0) return -1; _this->active_closing = 0; return 0; }
END_TEST START_TEST(test_network_nodes) { Edge *e1 = NULL, *e2 = NULL, *e3 = NULL; SList *nodes = NULL; Node ntmp = 0; e1 = edge_create(1, 2, 1, 0); e2 = edge_create(3, 4, 1, 0); e3 = edge_create(5, 6, 1, 0); net = network_create(); network_add_edge(net, e1); network_add_edge(net, e2); network_add_edge(net, e3); nodes = network_nodes(net); fail_unless(nodes != NULL); fail_unless(slist_length(nodes) == 6); while(nodes != NULL){ ntmp = *((Node *) slist_head_data(nodes)); fail_unless((1 <= ntmp) && (ntmp <= 6)); nodes = slist_next(nodes); } network_destroy(net); }
slist slist_insert(slist lista, int ind, int dato) { int lon = slist_length(lista); int i; slist nodoAux = slist_create(); slist nodoNuevo = slist_create(); slist listaAux = lista; if( ind == 0) { lista = slist_preppend(lista, dato); return lista; } if( ind == lon) { lista = slist_append(lista, dato); return lista; } if( ind > 0 && ind < lon) { nodoNuevo = slist_append(nodoNuevo, dato); for(i = 0; i < ind - 1; lista = slist_next(lista), i++) ; nodoAux = slist_next(lista); slist_next(lista) = nodoNuevo; slist_next(nodoNuevo) = nodoAux; } return listaAux; }
slist slist_remove(slist lista, int ind) { int lon = slist_length(lista); slist listaAux = lista; slist nodoAux; int i; if(ind < 0 || ind >= lon) return lista; if(ind == 0) { nodoAux = lista; lista = slist_next(lista); free(nodoAux); return lista; } else { for( i = 0; i < ind - 1; lista = slist_next(lista), i++) ; nodoAux = slist_next(lista); slist_next(lista) = slist_next(nodoAux); free(nodoAux); } return listaAux; }
static void dump(slist *l) { int i; fprintf(stderr, "\tl->itr_curr = %d\n" "\tl->itr_next = %d\n" "\tl->first_idx = %d\n" "\tl->last_idx = %d\n" "\tl->list_size = %d\n" , l->itr_curr, l->itr_next, l->first_idx, l->last_idx , l->list_size); for (i = 0; i < slist_length(l); i++) { if ((i % 16) == 0) fprintf(stderr, "%08x ", i); fprintf(stderr, " %3d", (int)slist_get(l, i)); if ((i % 16) == 7) fprintf(stderr, " -"); if ((i % 16) == 15) fprintf(stderr, "\n"); } if ((i % 16) != 0) fprintf(stderr, "\n"); }
static void test_10() { int i; slist sl; slist *l = &sl; slist_init(l); slist_add(l, (void *)1); slist_add(l, (void *)2); slist_add(l, (void *)3); slist_itr_first(l); ASSERT((int)slist_itr_next(l) == 1); ASSERT((int)slist_itr_next(l) == 2); for (i = 4; i < 10000; i++) { ASSERT(slist_itr_has_next(l)); ASSERT((int)slist_itr_next(l) == i - 1); if (i % 3 == 1) slist_add(l, (void *)i); if (i % 3 == 0) ASSERT((int)slist_itr_remove(l) == i - 1); if (i % 3 != 1) slist_add(l, (void *)i); } slist_itr_first(l); while (slist_itr_has_next(l)) { slist_itr_next(l); slist_itr_remove(l); } ASSERT((int)slist_length(l) == 0); slist_fini(l); }
static void search_gui_flush_queue(search_t *search) { g_return_if_fail(search); g_return_if_fail(search->tree); if (slist_length(search->queue) > 0) { GtkTreeModel *model; guint max_items; struct result_data *data; bool stopped; stopped = search_gui_start_massive_update(search); model = gtk_tree_view_get_model(GTK_TREE_VIEW(search->tree)); max_items = search_gui_is_sorted(search) ? 100 : 500; while (max_items-- > 0 && NULL != (data = slist_shift(search->queue))) { search_gui_flush_queue_data(search, model, data); } if (stopped) search_gui_end_massive_update(search); } }
SListValue *slist_to_array(SListEntry *list) { SListEntry *rover; SListValue *array; unsigned int length; unsigned int i; /* Allocate an array equal in size to the list length */ length = slist_length(list); array = malloc(sizeof(SListValue) * length); if (array == NULL) { return NULL; } /* Add all entries to the array */ rover = list; for (i = 0; i < length; ++i) { /* Add this node's data */ array[i] = rover->data; /* Jump to the next list node */ rover = rover->next; } return array; }
SList slist_remove(SList list, unsigned int pos, RemoveFunc remove) { SList list_aux = list; if(pos == 0) { list = slist_next(list); //remove(&slist_data(list_aux)); //free(list_aux->data); free(list_aux); } else { int i; for( i = 0; i < slist_length(list); i++) { if(i == pos - 1) { SList node_aux = slist_next(list_aux); slist_next(list_aux) = slist_next(slist_next(list_aux)); //remove(&slist_data(node_aux)); free(node_aux); } list_aux = slist_next(list_aux); } } return list; }
void *queue_pop_tail (Queue *queue) { void *element_data = NULL; SList *temp_previous = NULL; /* Precondition */ assert(queue != NULL); assert(queue->length != 0); if (queue->length == 1) { element_data = slist_nth_data(queue->tail, 0); slist_free_1(queue->tail); queue->head = NULL; queue->tail = NULL; } else { /* Minus 2, because list starts at pos 0 */ temp_previous = slist_nth(queue->head, queue->length - 2); element_data = slist_nth_data(queue->tail, 0); slist_free_1(queue->tail); temp_previous->next = NULL; queue->tail = temp_previous; } queue->length--; /* Postcondicion */ assert((int) queue->length == slist_length(queue->head)); return element_data; }
int pptpd_assign_call(pptpd *_this, pptp_call *call) { int shuffle_cnt = 0, call_id; shuffle_cnt = 0; slist_itr_first(&_this->call_free_list); while (slist_length(&_this->call_free_list) > 1 && slist_itr_has_next(&_this->call_free_list)) { call_id = (int)slist_itr_next(&_this->call_free_list); if (call_id == 0) break; slist_itr_remove(&_this->call_free_list); if (call_id == PPTPD_SHUFFLE_MARK) { if (shuffle_cnt++ > 0) break; slist_shuffle(&_this->call_free_list); slist_add(&_this->call_free_list, (void *)PPTPD_SHUFFLE_MARK); slist_itr_first(&_this->call_free_list); continue; } call->id = call_id; hash_insert(_this->call_id_map, CALL_MAP_KEY(call), call); return 0; } errno = EBUSY; pptpd_log(_this, LOG_ERR, "call request reached limit=%d", PPTP_MAX_CALL); return -1; }
/* * Configuration */ int l2tpd_reload(l2tpd *_this, struct l2tp_confs *l2tp_conf) { int i; struct l2tp_conf *conf; l2tpd_listener *listener; struct l2tp_listen_addr *addr; if (slist_length(&_this->listener) > 0) { /* * TODO: add / remove / restart listener. */ slist_itr_first(&_this->listener); while (slist_itr_has_next(&_this->listener)) { listener = slist_itr_next(&_this->listener); TAILQ_FOREACH(conf, l2tp_conf, entry) { if (strcmp(listener->tun_name, conf->name) == 0) { listener->conf = conf; break; } } } return 0; }
unsigned search_gui_queue_length(const struct search *search) { g_return_val_if_fail(search, 0); g_return_val_if_fail(search->queue, 0); return slist_length(search->queue); }
SList *queue_find_custom (Queue *queue, const void *data, CompareFunc(func)) { /* Precondition */ assert(queue != NULL); /* Postcondicion */ assert((int) queue->length == slist_length(queue->head)); return slist_find_custom(queue->head, data, func); }
SList *queue_find (Queue *queue, const void *data) { /* Precondition */ assert(queue != NULL); /* Postcondicion */ assert((int) queue->length == slist_length(queue->head)); return slist_find(queue->head, data); }
void queue_foreach (Queue *queue, UserFunc(func), void *user_data) { /* Precondition */ assert(queue != NULL); slist_foreach(queue->head, func, user_data); /* Postcondicion */ assert((int) queue->length == slist_length(queue->head)); }
unsigned int queue_get_length (Queue *queue) { /* Precondition */ assert(queue != NULL); /* Postcondicion */ assert((int) queue->length == slist_length(queue->head)); return queue->length; }
bool queue_is_empty (Queue *queue) { /* Precondition */ assert(queue != NULL); /* Postcondicion */ assert((int) queue->length == slist_length(queue->head)); return queue->length == 0; }
SList *queue_list (Queue *queue) { /* Precondition */ assert(queue != NULL); /* Postcondicion */ assert((int) queue->length == slist_length(queue->head)); return queue->head; }
void *queue_peek_tail (Queue *queue) { /* Precondition */ assert(queue != NULL); assert(queue->length > 0); /* Postcondicion */ assert((int) queue->length == slist_length(queue->head)); return slist_nth_data(queue->tail, 0); }
void *queue_peek_nth (Queue *queue, unsigned int n) { /* Precondition */ assert(queue != NULL); assert(queue->length > 0); assert(n < queue->length); /* Postcondicion */ assert((int) queue->length == slist_length(queue->head)); return slist_nth_data(queue->head, n); }
static void test_02() { int i; slist sl; slist *l = &sl; slist_init(&sl); /* Place 300 items for left side and 211 items for right side. */ for (i = 0; i < 511; i++) slist_add(&sl, (void *)i); for (i = 0; i <= 300; i++) slist_remove_first(&sl); for (i = 0; i <= 300; i++) slist_add(&sl, (void *)i); /* Set values to make index number and value the same. */ for (i = 0; i < slist_length(&sl); i++) slist_set(&sl, i, (void *)(i + 1)); ASSERT(slist_length(&sl) == 511); /* The logical length is 511. */ ASSERT((int)sl.list[511] == 211); /* The most right is 211th. */ ASSERT((int)sl.list[0] == 212); /* The most left is 212th. */ ASSERT(sl.list_size == 512); /* The physical size is 512. */ slist_add(&sl, (void *)512); /* Add 512th item. */ ASSERT(sl.list_size == 768); /* The physical size is extended. */ ASSERT(slist_length(&sl) == 512); /* The logical length is 512. */ ASSERT((int)sl.list[511] == 211); /* boundary */ ASSERT((int)sl.list[512] == 212); /* boundary */ ASSERT((int)sl.list[767] == 467); /* The most right is 467th. */ ASSERT((int)sl.list[0] == 468); /* The most left is 468th. */ /* Check all items */ for (i = 0; i < slist_length(&sl); i++) ASSERT((int)slist_get(&sl, i) == i + 1); /* check */ }
void queue_push_head (Queue *queue, void *data) { /* Precondition */ assert(queue != NULL); queue->head = slist_prepend (queue->head, data); if (queue->length == 0) { queue->tail = queue->head; } queue->length++; /* Postcondicion */ assert((int) queue->length == slist_length(queue->head)); }
static void test_06() { int i, j; slist sl; slist *l = &sl; slist_init(l); for (i = 0; i < 255; i++) slist_add(l, (void *)i); i = 255; for (slist_itr_first(l); slist_itr_has_next(l); ) { ASSERT(slist_length(l) == i); slist_itr_next(l); ASSERT((int)slist_itr_remove(l) == 255 - i); ASSERT(slist_length(l) == i - 1); for (j = i; j < slist_length(l); j++) ASSERT((int)slist_get(l, j) == i + j); i--; } }