static struct filelist * merge_sort(struct filelist *head, int (*cmp)(const struct filelist *, const struct filelist *)) { struct filelist *left, *right; if (head == NULL || head->fl_next == NULL) return head; list_split(head, &left, &right); left = merge_sort(left, cmp); right = merge_sort(right, cmp); return sorted_merge(left, right, cmp); }
int ipv4_init(struct fins_module *module, uint32_t flows_num, uint32_t *flows, metadata_element *params, struct envi_record *envi) { PRINT_IMPORTANT("Entered: module=%p, params=%p, envi=%p", module, params, envi); module->state = FMS_INIT; module_create_structs(module); ipv4_init_params(module); module->data = secure_malloc(sizeof(struct ipv4_data)); struct ipv4_data *md = (struct ipv4_data *) module->data; if (module->flows_max < flows_num) { PRINT_ERROR("todo error"); return 0; } md->flows_num = flows_num; int i; for (i = 0; i < flows_num; i++) { md->flows[i] = flows[i]; } md->addr_list = list_create(IPV4_ADDRESS_LIST_MAX); list_for_each1(envi->if_list, ipv4_ifr_get_addr_func, md->addr_list); if (envi->if_loopback) { md->addr_loopback = (struct addr_record *) list_find(envi->if_loopback->addr_list, addr_is_v4); } if (envi->if_main) { md->addr_main = (struct addr_record *) list_find(envi->if_main->addr_list, addr_is_v4); } md->route_list = list_filter(envi->route_list, route_is_addr4, route_clone); if (md->route_list->len > IPV4_ROUTE_LIST_MAX) { PRINT_ERROR("todo"); struct linked_list *leftover = list_split(md->route_list, IPV4_ROUTE_LIST_MAX - 1); list_free(leftover, free); } md->route_list->max = IPV4_ROUTE_LIST_MAX; //when recv pkt would need to check addresses //when send pkt would need to check routing table & addresses (for ip address) //both of these would need to be updated by switch etc //routing_table = IP4_get_routing_table(); PRINT_DEBUG("after ip4 sort route table"); memset(&md->stats, 0, sizeof(struct ipv4_stats)); return 1; }
void main(int argc, char* argv[]) { pstud phead,ptail,phead1; int i; phead=NULL; ptail=NULL; while(scanf("%d",&i) != EOF) { list_tail_insert(&phead,&ptail,i); } list_print(phead); phead1=phead->pnext; list_split(phead,phead1); list_print(phead); list_print(phead1); }
int generate_histdb_prepare(genhistdb_handle_type *_handle, char *dirname, char *dbname, unsigned int nr_threads, generate_mode_t mode) { struct genhistdb_struct *handle; Node *head = NULL; Node *compute = NULL; unsigned int compute_len = 0; if(!dirname || !dbname) return -1; handle = (struct genhistdb_struct *)malloc(sizeof(struct genhistdb_struct)); if(!handle) return -1; if(nr_threads == 0) handle->nr_threads = DEFAULT_NTHREADS; else handle->nr_threads = nr_threads; head = GetMusicFiles(dirname); handle->hist_list = NULL; handle->hist_len = 0; if(mode == UPDATE_MODE) { if(read_histdb(&handle->hist_list, &handle->hist_len, dbname)) { compute = head; } else { compute = get_compute(head, handle->hist_list, handle->hist_len); } } else { compute = get_compute(head, handle->hist_list, handle->hist_len); } compute_len = list_len(compute); if(compute_len > 0) handle->hist_list = realloc(handle->hist_list, sizeof(hist_t) * (handle->hist_len + compute_len)); if(!handle->hist_list) { free(handle); goto realloc_failed; } if(compute_len <= handle->nr_threads) handle->nr_threads = 1; handle->threads = (pthread_t *)calloc(handle->nr_threads, sizeof(pthread_t)); if(!handle->threads) goto threads_failed; handle->per_thread_list = (Node **)calloc(handle->nr_threads, sizeof(Node *)); if(!handle->per_thread_list) goto list_creation_failed; handle->completed = 0; handle->updated = 0; handle->to_complete = compute_len; list_split(handle->per_thread_list, handle->nr_threads, compute); strcpy(handle->dbname, dbname); *_handle = handle; return 0; list_creation_failed: free(handle->threads); threads_failed: free(handle->hist_list); realloc_failed: free(handle); return -1; }