GSList* g_slist_alloc (void) { GSList *list; G_LOCK (current_allocator); if (!current_allocator) { GAllocator *allocator = g_allocator_new ("GLib default GSList allocator", 128); g_slist_validate_allocator (allocator); allocator->last = NULL; current_allocator = allocator; } if (!current_allocator->free_lists) { list = g_chunk_new (GSList, current_allocator->mem_chunk); list->data = NULL; } else { if (current_allocator->free_lists->data) { list = current_allocator->free_lists->data; current_allocator->free_lists->data = list->next; list->data = NULL; } else { list = current_allocator->free_lists; current_allocator->free_lists = list->next; } } G_UNLOCK (current_allocator); list->next = NULL; return list; }
int main (int argc,char *argv[]) { GList *list1 = NULL,*list2 = NULL,*l,*l1,*list3 = NULL; int i; int *value; const char mem_allocator[] = "mem_allocator"; GAllocator *allocator; int num1[] = { 1,2,3 }; int num2[] = { 4,5,6 }; #ifdef SYMBIAN g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); #endif /*SYMBIAN*/ allocator = g_allocator_new(mem_allocator,500); for(i=0;i<3;i++) list1 = g_list_append (list1, &num1[i]); for(i=0;i<3;i++) list2 = g_list_append (list2, &num2[i]); list1 = g_list_concat(list1,list2); for(i=0;i<6;i++) { l = g_list_nth(list1,i); g_assert(*(gint *)(l->data) == i+1); } list2 = g_list_copy(list1); for(i=0;i<3;i++) { l = g_list_nth(list2,i); g_assert(*(gint *)(l->data) == i+1); } l = g_list_first(list2); g_assert(*(gint *)(l->data) == 1); value = (int *)g_list_nth_data(list1,1); g_assert(*value == 2); l = g_list_nth(list1,3); l1 = g_list_nth_prev(l,2); for(i=0;i<5;i++) { l = g_list_nth(l1,i); g_assert(*(gint *)(l->data) == i+2); } g_list_push_allocator(allocator); list3 = g_list_append(list3,&num1[2]); g_assert(*(gint *)(list3->data) == 3); g_list_pop_allocator(); list3 = g_list_append(list3,&num1[0]); g_assert(*(gint *)(list3->next->data) == 1); list1 = g_list_append(list1,&num1[0]); i = g_list_length(list1); list1 = g_list_remove_all(list1,&num1[0]); i = g_list_length(list1); g_assert(g_list_length(list1) == 5); // should be this value as we will remove 2 1's from the list for(i==0;i<g_list_length(list1);i++) { l = g_list_nth(list1,i); g_assert(*(gint *)(l->data) != 1); } g_allocator_free(allocator); #ifdef SYMBIAN testResultXml("list_test"); #endif /* EMULATOR */ return 0; }
// Tests for slist void tg_slist_tests() { GSList *slist,*st,*rem; gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; gint chk_buf[20]; gint i; gint g_slist_insert_data; gint g_slist_insert_before_data; gint ip1 = 10; gint ip2 = 15; gint ip3 = 5; gint ip4 = 12; gint g_slist_nth_data_op,g_slist_find_custom_op; //Trying to use the allocators so that even they get tested! GAllocator* alloc = g_allocator_new ("alloc_slist",5000); g_slist_push_allocator (alloc); slist = NULL; for (i = 0; i < 10; i++) slist = g_slist_append (slist, &nums[i]); //List looks like: // 0 1 2 3 4 5 6 7 8 9 //Test for g_slist_insert....inserted 10 at pos 4 g_slist_insert(slist,&ip1,4); st = g_slist_nth (slist,0); for(i = 0;i < 4;i++) st = st->next; g_slist_insert_data = *((gint*) st->data); g_assert(g_slist_insert_data == 10); /* for (i = 0; i < 10; i++) { st = g_slist_nth (slist, i); chk_buf[i] = *((gint*) st->data); }*/ //List looks like: // 0 1 2 3 10 4 5 6 7 8 9 //Test for g_slist_insert_before....inserted 15 at pos 7 st = g_slist_nth (slist,7); g_slist_insert_before(slist,st,&ip2); st = g_slist_nth (slist,0); for(i = 0;i < 7;i++) st = st->next; g_slist_insert_before_data = *((gint*) st->data); g_assert(g_slist_insert_before_data == 15); //List looks like: // 0 1 2 3 10 4 5 15 6 7 8 9 //Test for g_slist_index....finding 15 at pos 7 st = g_slist_nth (slist,0); g_assert(g_slist_index(st,&ip2)==7); //Test for g_slist_nth_data....getting 6 at position 8 g_slist_nth_data_op = *((gint*) g_slist_nth_data(slist,8)); g_assert(g_slist_nth_data_op == 6) ; //Test for g_slist_position st = g_slist_nth (slist,7); g_assert(g_slist_position (slist,st) == 7); //Test for g_slist_find_custom st = g_slist_find_custom(slist,&ip3,compare_fun_gr); g_slist_find_custom_op = *((gint*) st->data); g_assert(g_slist_find_custom_op == 5); //Test for g_slist_sort_with_data st = g_slist_sort_with_data(slist,compare_fun_gr_data,&ip3); for (i = 0; i < 10; i++) { st = g_slist_nth (slist, i); g_assert (*((gint*) st->data) == i); } //List looks like: // 0 1 2 3 4 5 6 7 8 9 10 15 //Test for g_slist_remove_link st = g_slist_nth (slist, 5); rem = g_slist_remove_link(slist , st); st = g_slist_nth (slist, 5); g_assert (*((gint*) st->data) == 6); //List looks like: // 0 1 2 3 4 6 7 8 9 10 15 //Test for g_slist_remove_all g_slist_insert(slist,&ip4,4); g_slist_insert(slist,&ip4,6); g_slist_insert(slist,&ip4,8); //List looks like: // 0 1 2 3 4 12 6 7 12 8 12 9 10 15 g_slist_remove_all(slist ,&ip4); g_slist_free (slist); g_slist_pop_allocator (); }