static void test_5(void) { ulong x = 0x100; size_t i,oldFree; sSLList *list; bool res = true; test_caseStart("Create & append & insert somewhere & destroy"); oldFree = heapspace(); list = sll_create(); for(i = 0; i < 5; i++) { sll_append(list,(void*)x++); } sll_insert(list,(void*)0x200,3); if(sll_get(list,3) != (void*)0x200) res = false; sll_insert(list,(void*)0x201,2); if(sll_get(list,2) != (void*)0x201) res = false; sll_insert(list,(void*)0x202,1); if(sll_get(list,1) != (void*)0x202) res = false; sll_insert(list,(void*)0x203,0); if(sll_get(list,0) != (void*)0x203) res = false; sll_insert(list,(void*)0x204,7); if(sll_get(list,7) != (void*)0x204) res = false; if(sll_length(list) != 10) res = false; sll_destroy(list,false); test_assertTrue(res); test_assertSize(heapspace(),oldFree); test_caseSucceeded(); }
static void test_case_char (void) { SLL_T* head; int i; head = sll_new (CDATA, iter_cb_char, pre_iterator, post_iterator); for (i = 'a'; i <= 'z'; i++) sll_insert (head, i); sll_iter (head); printf ("\n"); fprintf (stderr, "deleting a\n"); sll_delete (head, 'a'); fprintf (stderr, "deleting f\n"); sll_delete (head, 'f'); fprintf (stderr, "deleting z\n"); sll_delete (head, 'z'); sll_iter (head); fprintf (stderr, "inserting a \n"); sll_insert (head, 'a'); fprintf (stderr, "inserting z\n"); sll_insert (head, 'z'); fprintf (stderr, "inserting z\n"); sll_insert (head, 'z'); sll_iter (head); fprintf (stderr, "deleting z\n"); sll_delete (head, 'z'); sll_iter (head); }
int main(void) { NODE *p_root = NULL; int del_val; p_root = creat_list(); /*创建一个非循环单链表,返回根指针*/ traverse_linkedlist(p_root); /*遍历链表*/ /*插入新结点*/ if (0 == sll_insert(&p_root, 19)) { printf("插入节点失败,请重试!"); } printf("插入后的链表"); traverse_linkedlist(p_root); printf("现在节点个数为:%d\n", sll_count_nodes(p_root->link)); /*删除节点*/ if (sll_remove(&p_root, p_root->link->link, &del_val)) { printf("删除节点成功,被删除的节点的值为:%d\n", del_val); } printf("删除后的链表为:\n"); traverse_linkedlist(p_root); free(p_root); return 0; }
/* * scheduler_task_yield * @tcb: The task's control block that is needed to be scheduled in the * aperiodic scheduler. * @from: From where this task is being scheduled. * This is yield function required by a scheduling class, this is called when a * task is needed to be scheduled in the aperiodic scheduler. */ void scheduler_task_yield(TASK *tcb, uint8_t from) { /* Adjust the task control block as required. */ switch (from) { case YIELD_SLEEP: /* Task is being resumed from sleep. */ tcb->tick_sleep = 0; tcb->state = TASK_SLEEP_RESUME; break; default: /* Task is resuming normally. */ tcb->state = TASK_RESUME; break; } /* Schedule the task being yielded/re-enqueued. */ sll_insert(&sch_ready_task_list, tcb, &scheduler_task_sort, OFFSETOF(TASK, next)); } /* scheduler_task_yield */
static void test_case_int_3 (void) { SLL_T* sll; SLL_NODE* node = NULL; int i; fprintf (stderr, "test case int#3\n"); sll = sll_new (IDATA, iter_cb_int, pre_iterator, post_iterator); fprintf (stderr, "inserting elements from 10 - 20 in the list\n"); for (i = 10; i <= 20; i++) sll_insert (sll, i); while (NULL != (node = sll_next_get (sll, node))) { fprintf (stderr, "%ld ", node->FIDATA); } fprintf (stderr, "\n"); node = sll_last_get(sll); while (NULL != (node = sll_prev_get (sll, node))) { fprintf (stderr, "%ld ", node->FIDATA); } fprintf (stderr, "\n"); }
// Create a new session // FIXME - This is currently open to a DoS attack. // but since we have a session limit, just for opendias. char *create_session() { char *session_id; // Check upper session count limit int current_session_count = sll_count( sll_findFirstElement( sessions ) ); o_log( DEBUGM, "There are currently %d active sessions", current_session_count); if( current_session_count > MAX_SESSIONS ) { return NULL; } // Generate session key uuid_t uu; char *sid = malloc(36+1); uuid_generate(uu); uuid_unparse(uu, sid); session_id = o_strdup(sid); free(sid); o_log(DEBUGM, "Generated new session: %s", session_id ); // Create new session structure struct session_data *session_element = malloc( sizeof(struct session_data) ); session_element->last_accessed = time(NULL); session_element->session_container = sll_init(); // Save and return sll_insert( sessions, o_strdup(session_id), session_element ); return session_id; }
/* * sleep_add_to_list * @tcb: Task's control block that is needed to be added in the sleeping task's * list. * @ticks: Number of ticks for which this task is needed to sleep. * This function is called when a task is needed to sleep for a particular * number of system ticks. This function adds the given task in the sleeping * tasks list. */ void sleep_add_to_list(TASK *tcb, uint64_t ticks) { /* Calculate system tick at which task will be invoked. */ tcb->tick_sleep = current_system_tick() + ticks; /* Put this task on the list of sleeping tasks. */ /* This will also schedule this task. */ sll_insert(&sleep_scheduler.ready_tasks, tcb, &sleep_task_sort, OFFSETOF(TASK, next_sleep)); } /* sleep_add_to_list */
/******************************************************************************* * test_case_int_2 - test routines to expand a linked list * * RETURNS: void */ static void test_case_int_2 (void) { SLL_T* first; SLL_T* second; int i; fprintf (stdout, "TC[LL]: test case int#2\n"); first = sll_new (IDATA, iter_cb_int, pre_iterator, post_iterator); second = sll_new (IDATA, iter_cb_int, pre_iterator, post_iterator); for (i = 10; i <= 20; i++) sll_insert (first, i); for (i = 20; i < 30; i++) sll_insert (second, i); sll_iter (first); sll_iter (second); fprintf (stdout, "TC[LL]: concatenating\n"); sll_concat (first, second); sll_iter (first); }
/** * @brief * * @return A new linked list (SLL_T*) */ static void tc_perf_1 (void) { SKL_T* skl; SLL_T* sll; void* st; void* et; unsigned long item; unsigned long diff=0; int i; int iter; fprintf (stdout, "TC[%5s]: test case int#1\n", "skl"); for (iter = 0; iter < 100; iter++) { fprintf (stdout, "TC[%5s]: iteration %d\n", "skl", iter+1); skl = skl_new (IDATA, 16, iter_cb_int, pre_iterator, post_iterator); sll = sll_new (IDATA, iter_cb_int, pre_iterator, post_iterator); for (i = 1; i <= 10000; i++) { item = dslib_random (30000); //fprintf (stdout, "inserting %lu \n", item); skl_insert_idata (skl, item); sll_insert (sll, item); } //fprintf (stdout, "\n"); //sll_iter (sll); //skl_iter (skl); st = dslib_record_time(); if (NULL != skl_find_idata (skl, 1000)) fprintf (stdout, "TC[%5s]: found 1000\n", "skl"); else fprintf (stdout, "TC[%5s]: did not find 1000\n", "skl"); et = dslib_record_time(); if (ERR_UTILS_OK != dslib_time_diff (st, et, &diff)) fprintf (stdout, "TC[%5s]: dslib_time_diff() error\n", "skl"); fprintf (stdout, "TC[%5s]: find took %ldns\n", "skl", diff); st = dslib_record_time(); if (NULL != sll_find (sll, 1000)) fprintf (stdout, "TC[%5s]: found 1000\n", "sll"); else fprintf (stdout, "TC[%5s]: did not find 1000\n", "sll"); et = dslib_record_time(); if (ERR_UTILS_OK != dslib_time_diff (st, et, &diff)) fprintf (stdout, "TC[%5s]: dslib_time_diff() error\n", "skl"); fprintf (stdout, "TC[%5s]: find took %ldns\n\n", "skl", diff); } }
/******************************************************************************* * test_case_int_1 - test core linked list operations * * RETURNS: void */ static void test_case_int_4 (void) { SLL_T* head; int i; fprintf (stdout, "TC[LL]: test case int#1\n"); head = sll_new (IDATA, iter_cb_int, pre_iterator, post_iterator); fprintf (stdout, "TC[LL]: inserting 1...19\n"); for (i = 1; i < 20; i++) sll_insert (head, i); sll_iter (head); fprintf (stdout, "TC[LL]: deleting 10\n"); sll_delete (head, 10); fprintf (stdout, "TC[LL]: deleting 15\n"); sll_delete (head, 15); fprintf (stdout, "TC[LL]: deleting 19\n"); sll_delete (head, 19); sll_iter (head); fprintf (stdout, "TC[LL]: inserting 19\n"); sll_insert (head, 19); fprintf (stdout, "TC[LL]: inserting 10\n"); sll_insert (head, 10); sll_iter (head); if (NULL != sll_find (head, 5)) fprintf (stdout, "TC[LL]: OK found 5\n"); else fprintf (stdout, "TC[LL]: ERROR Not found 5\n"); if (NULL != sll_find (head, 67)) fprintf (stdout, "TC[LL]: ERROR found 67\n"); else fprintf (stdout, "TC[LL]: OK Not found 67\n"); }
bool sll_append(sSLList *list,const void *data) { sList *l = (sList*)list; return sll_insert(list,data,l->length); }
void init_session_management( int max_sessions, int max_session_age) { sessions = sll_init(); sll_insert( sessions, "placeholder", "placeholder" ); MAX_SESSIONS = max_sessions; MAX_AGE = max_session_age;; }