/* Establish connection to a peer. As a backend, it uses the internal and more * generic connection creater which takes care of DNS querying etc. */ enum bittorrent_state make_bittorrent_peer_connection(struct bittorrent_connection *bittorrent, struct bittorrent_peer *peer_info) { enum bittorrent_state result = BITTORRENT_STATE_OUT_OF_MEM; struct uri *uri = NULL; struct string uri_string = NULL_STRING; struct bittorrent_peer_connection *peer; peer = init_bittorrent_peer_connection(-1); if (!peer) goto out; peer->local.initiater = 1; add_to_list(bittorrent->peers, peer); peer->bittorrent = bittorrent; peer->bitfield = init_bitfield(bittorrent->meta.pieces); if (!peer->bitfield) goto out; memcpy(peer->id, peer_info->id, sizeof(peer->id)); /* XXX: Very hacky; construct a fake URI from which make_connection() * can extract the IP address and port number. */ /* FIXME: Rather change the make_connection() interface. This is an ugly * hack. */ if (!init_string(&uri_string)) goto out; if (!add_format_to_string(&uri_string, #ifdef CONFIG_IPV6 strchr(peer_info->ip, ':') ? "bittorrent-peer://[%s]:%u/" : #endif "bittorrent-peer://%s:%u/", peer_info->ip, (unsigned) peer_info->port)) goto out; uri = get_uri(uri_string.source, 0); if (!uri) goto out; make_connection(peer->socket, uri, send_bittorrent_peer_handshake, 1); result = BITTORRENT_STATE_OK; out: if (uri) done_uri(uri); done_string(&uri_string); if (peer && result != BITTORRENT_STATE_OK) done_bittorrent_peer_connection(peer); return result; }
void test_deleteElementAt_forFloat_if_listLength_one(){ LinkedList list = createList(); float number = 23.43; add_to_list(&list, &number); assert(list.length == 1); float deleted_ele = 23.43; void *delete_element = deleteElementAt(&list, 0); assert(_TYPEFLOAT_(delete_element) == deleted_ele); void *ele = getElementAt(list,0); assert(list.length == 0); assert(ele == NULL); };
void test_filter_is_divisible() { int hint = 3; LinkedList list = createList(); int arr[7] = {2, 3, 4, 6, 1, 12, 45}; for (size_t i = 0; i < 7; i++) { add_to_list(&list, &arr[i]); } LinkedList output = filter(list, &isDivisible, &hint); int *index1 = getElementAt(output, 1); int *index2 = getElementAt(output, 2); assert(*(int *)output.head->value == 3); assert(*index1 == 6); assert(*index2 == 12); assert(*(int *)output.tail->value == 45); };
static void device_added_callback( LibHalContext *ctx, const char *udi ) { if( libhal_device_property_exists( ctx, udi, "battery", NULL ) ) { char *type = libhal_device_get_property_string( ctx, udi, "battery.type", NULL ); if( type ) { /* We only track 'primary' batteries (ie: to avoid monitoring * batteries in cordless mice or UPSes etc.) */ if( !strcmp( type, "primary" ) ) add_to_list( ctx, &batteries, udi, sizeof (struct battery_info) ); libhal_free_string( type ); } } if( libhal_device_property_exists( ctx, udi, "ac_adapter", NULL ) ) add_to_list( ctx, &adaptors, udi, sizeof (struct adaptor_info) ); }
void test_for_deleteElement_it_delete_the_ANY_element_of_float_type_from_list(){ Linked_list list=createList(); float _1st =22.5,_2nd=23.0,_3rd=24.50; add_to_list(&list,&_1st); add_to_list(&list,&_2nd); add_to_list(&list,&_3rd); assert(list.length==3); assert(TYPEFLOAT(list.head->value)==22.5); assert(TYPEFLOAT(list.head->next->value)==23.0); assert(TYPEFLOAT(list.tail->value)==24.50); deleteElementAt(&list,1); assert(TYPEFLOAT(list.head->value)==22.5); assert(TYPEFLOAT(list.head->next->value)==24.50); assert(TYPEFLOAT(list.tail->value)==24.50); assert(list.length==2); }
void test_for_getElementAt_returns_the_value_at_index_double_type_of_list(){ Linked_list list = createList(); double _0th,_1st,_2nd; _0th =6.50000000000000000000000000000000000000; _1st =99.9999999999876787657865467865786547876546545456754567865478654786546786546754675467546754657545650; _2nd =_2nd+1876578654678654675678546750.5; add_to_list(&list ,&_0th); add_to_list(&list ,&_1st); add_to_list(&list ,&_2nd); Element *e0=getElementAt(list,0); Element *e1=getElementAt(list,1); Element *e2=getElementAt(list,2); assert(TYPEDOUBLE(e0->value)==6.50000000000000000000000000000000000000); assert(TYPEDOUBLE(e1->value)==99.9999999999876787657865467865786547876546545456754567865478654786546786546754675467546754657545650); assert(TYPEDOUBLE(e2->value)==_2nd); assert(getElementAt(list,9)==NULL); }
void test_for_getElementAt_returns_the_value_at_index_float_type_of_list(){ Linked_list list = createList(); float _0th,_1st,_2nd; _0th =6.50; _1st =99.0; _2nd =10.5; add_to_list(&list ,&_0th); add_to_list(&list ,&_1st); add_to_list(&list ,&_2nd); Element *e0=getElementAt(list,0); Element *e1=getElementAt(list,1); Element *e2=getElementAt(list,2); assert(TYPEFLOAT(e0->value)==6.50); assert(TYPEFLOAT(e1->value)==99.0); assert(TYPEFLOAT(e2->value)==10.5); assert(getElementAt(list,9)==NULL); }
void test_reverse_for_float_using_getElementAt(){ LinkedList list = createList(); float float_point = 12.01,float_point2 = 32.21, float_point3 = 45.65, float_point4 = 76.32; add_to_list(&list, &float_point); add_to_list(&list, &float_point2); add_to_list(&list, &float_point3); add_to_list(&list, &float_point4); LinkedList reverse_float_list = reverse(list); float f_valAfterReverse = 76.32; void *float_ele = getElementAt(reverse_float_list, 0); assert(_TYPEFLOAT_(float_ele) == f_valAfterReverse); void *float_ele2 = getElementAt(reverse_float_list, 2); float t_valAfterReverse = 32.21; assert(_TYPEFLOAT_(float_ele2) == t_valAfterReverse); float l_valAfterReverse = 12.01; void *float_ele3 = getElementAt(reverse_float_list, 3); assert(_TYPEFLOAT_(float_ele3) == l_valAfterReverse); };
/* * this is the function that actually listens in a seperate thread for * interrupts as they come in */ void* test_thread(void *args) { struct interrupt_thread_parameters* params = (struct interrupt_thread_parameters *) args; u_int8_t endpoint_address = params->bEndpointAddress; u_int16_t max_size = params->wMaxPacketSize; usb_dev_handle* handle = params->handle; struct interrupt_result_list* list = params->list; free(params); char* data = malloc(max_size); pthread_cleanup_push(free_data, data); int result; while(1) { result = usb_interrupt_read(handle, endpoint_address, data, max_size, 0); if(result < 0) break; struct interrupt_result_list_entry* list_item = (struct interrupt_result_list_entry*) malloc(sizeof(struct interrupt_result_list_entry) + result); // printf("buffalo "); // for(i = 0; i < result; i++) // printf(" %x", data[i]); // printf("\n"); memcpy(list_item->data, data, result); list_item->data_size = result; list_item->next = NULL; add_to_list(list, list_item); } //an error occured pthread_cleanup_pop(0); free(data); static struct interrupt_result_list_entry error_item; error_item.data_size = -1; //negative size indicates an error error_item.next = NULL; add_to_list(list, &error_item); return NULL; }
struct terminal *init_term(int fdin, int fdout, void (*root_window)(struct window *, struct event *, int)) { struct terminal *term; struct window *win; term = mem_alloc(sizeof (struct terminal)); memset(term, 0, sizeof(struct terminal)); term->fdin = fdin; term->fdout = fdout; term->master = term->fdout == get_output_handle(); /*term->x = 0; term->y = 0; term->cx = 0; term->cy = 0;*/ term->lcx = -1; term->lcy = -1; term->dirty = 1; term->redrawing = 0; term->blocked = -1; term->screen = DUMMY; term->last_screen = DUMMY; term->spec = &dumb_term; term->term[0] = 0; term->cwd[0] = 0; term->input_queue = DUMMY; term->qlen = 0; init_list(term->windows); win = mem_alloc(sizeof (struct window)); win->handler = root_window; win->data = NULL; win->term = term; add_to_list(term->windows, win); /*alloc_term_screen(term, 80, 25);*/ add_to_list(terminals, term); set_handlers(fdin, (void (*)(void *))in_term, NULL, (void (*)(void *))destroy_terminal, term); return term; }
void test_deleteElementAt_deleting_from_middle(){ LinkedList list = createList(); void *item = (int *)malloc(sizeof(int)); *(int *)item = 10; int length = add_to_list(&list, item); void *item1 = (int *)malloc(sizeof(int)); *(int *)item1 = 11; length = add_to_list(&list, item1); void *item2 = (int *)malloc(sizeof(int)); *(int *)item2 = 12; length = add_to_list(&list, item2); void *item3 = (char *)malloc(sizeof(char)); *(char *)item3 = 'P'; length = add_to_list(&list, item3); int index = 2; int * deleted_item = deleteElementAt(&list,index); assert(*(int *)deleted_item == 12); assert(list.length == 3); printf("Item has been deleted from the middle and returned\n"); assert(indexOf(list, item2) == -1); printf("deleted item can not be found\n"); }
void test_deleteElementAt(){ LinkedList list = createList(); void *item = (int *)malloc(sizeof(int)); *(int *)item = 10; int length = add_to_list(&list, item); void *item1 = (int *)malloc(sizeof(int)); *(int *)item1 = 11; length = add_to_list(&list, item1); void *item2 = (int *)malloc(sizeof(int)); *(int *)item2 = 12; length = add_to_list(&list, item2); void *item3 = (char *)malloc(sizeof(char)); *(char *)item3 = 'P'; length = add_to_list(&list, item3); int index = 0; int * deleted_item = deleteElementAt(&list,index); assert(*(int *)deleted_item == 10); printf("Item has been deleted and returned\n"); int item_to_find = 10; assert(indexOf(list, item) == -1); printf("deleted item can not be found\n"); index = 2; char * deleted_item_1 = deleteElementAt(&list,index); assert(*(char *)deleted_item_1 == 'P'); assert(list.length == 2); printf("Last item has been deleted and returned\n"); assert(indexOf(list, &item3) == -1); printf("deleted item can not be found\n"); }
void test_add_to_list(){ LinkedList list = createList(); void *item = (int *)malloc(sizeof(int)); *(int *)item=20; int length = add_to_list(&list, item); assert(list.length == 1); printf("One item has been added to the list\n"); assert(*(int *)list.first->value == 20); assert(*(int *)list.last->value == 20); printf("Item 10 has been added to the first and the last of the linked list for the first insertion\n"); void *item1 = (int *)malloc(sizeof(int)); *(int *)item1 = 10; length = add_to_list(&list, item1); assert(list.length == 2); printf("Second item has been added\n"); assert(*(int *)list.first->value == 20); assert(*(int *)list.last->value == 10); printf("Second item has been added to the last of the list\n"); };
/* As of 3.63, there are only global, descriptor, and character events. This * is due to the potential scope of the necessary debugging if events were * included with rooms, objects, spells or any other structure type. Adding * events to these other systems should be just as easy as adding the current * library was, and should be available in a future release. - Vat */ void attach_mud_event(struct mud_event_data *pMudEvent, long time) { struct event * pEvent; struct descriptor_data * d; struct char_data * ch; pEvent = event_create(mud_event_index[pMudEvent->iId].func, pMudEvent, time); pEvent->isMudEvent = TRUE; pMudEvent->pEvent = pEvent; switch (mud_event_index[pMudEvent->iId].iEvent_Type) { case EVENT_WORLD: add_to_list(pEvent, world_events); break; case EVENT_DESC: d = (struct descriptor_data *) pMudEvent->pStruct; add_to_list(pEvent, d->events); break; case EVENT_CHAR: ch = (struct char_data *) pMudEvent->pStruct; add_to_list(pEvent, ch->events); break; } }
void * safe_realloc(void *old_pointer, size_t size){ void *res = realloc(old_pointer, size); if (!old_pointer && size != 0){ free(old_pointer); perror_message("realloc"); if (fail_safe) for (int i = 0; i < mem_count; i++) free(mem_list[i]); abort(); } else{ if (fail_safe){ remove_from_list(old_pointer); add_to_list(res); } return res; } }
static void write_data_packet(descriptor_callback_context *ctx, USHORT deviceAddress, void *payload, int payload_length, BOOL out) { int data_len = sizeof(USBPCAP_BUFFER_CONTROL_HEADER) + payload_length; UINT8 *data = (UINT8*)malloc(data_len); PUSBPCAP_BUFFER_CONTROL_HEADER hdr = (PUSBPCAP_BUFFER_CONTROL_HEADER)data; initialize_control_header(hdr, ctx->roothub, deviceAddress, payload_length, USBPCAP_CONTROL_STAGE_DATA, TRUE, out); memcpy(&data[sizeof(USBPCAP_BUFFER_CONTROL_HEADER)], payload, payload_length); add_to_list(ctx, data, data_len); }
void input(char *buffer, int length, int screen_index){ //get input from keyboard keyboard_buffer_descriptor *keyboard_buffer = (keyboard_buffer_descriptor *)malloc(sizeof(keyboard_buffer_descriptor)); keyboard_buffer->screen_index = screen_index; keyboard_buffer->keyboard_buffer = (char *)malloc(length); keyboard_buffer->keyboard_buffer_index = 0; keyboard_buffer->need_to_buffer = 1; add_to_list(keyboard_buffers, keyboard_buffer); while(keyboard_buffer->keyboard_buffer_index < length && keyboard_buffer->need_to_buffer) asm("hlt"); memcpy(buffer, keyboard_buffer->keyboard_buffer, keyboard_buffer->keyboard_buffer_index); buffer[keyboard_buffer->keyboard_buffer_index] = '\0'; remove_from_list(keyboard_buffers, keyboard_buffer); free(keyboard_buffer->keyboard_buffer); free(keyboard_buffer); }
static int parse_unignore (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err) { do { mutt_extract_token (buf, s, 0); /* don't add "*" to the unignore list */ if (strcmp (buf->data, "*")) add_to_list (&UnIgnore, buf->data); remove_from_list (&Ignore, buf->data); } while (MoreArgs (s)); return 0; }
int main() { int check = 1; char str[80]; while(check) { printf("Would you like to enter an integer (y for yes/n for no)? "); scanf("%s", str); if(str[0] == 'y' && str[1] == '\0') { add_to_list(); } else { check = 0; } } print_list(); return 0; }
void test_reverse() { LinkedList list = createList(); int arr[7] = {2, 3, 4, 6, 1, 12, 45}; for (size_t i = 0; i < 7; i++) { add_to_list(&list, &arr[i]); } LinkedList res = reverse(list); int *index1 = getElementAt(res, 1); int *index2 = getElementAt(res, 2); int *index5 = getElementAt(res, 5); assert(*(int *)res.head->value == 45); assert(*index1 == 12); assert(*index2 == 1); assert(*index5 == 3); assert(*(int *)res.tail->value == 2); };
void remove_path(MVGraph* g){ while(g->path_found.length>0){ MVNodeP current = pop(g->path_found); if(!current->deleted){ //avoid double work current->deleted = true; MVNodePos * itt; for_eachp(itt,current->pred_to,{ if(!itt->n->deleted){ itt->n->preds.list[itt->pos] = NULL; if((--(itt->n->number_preds)) <=0){ add_to_list(g->path_found,itt->n); } } }); }
void test_reverse_double() { LinkedList list = createList(); double arr[7] = {2.21, 3.212, 4.87, 6.34, 1.435, 12.43, 45.22}; for (size_t i = 0; i < 7; i++) { add_to_list(&list, &arr[i]); } LinkedList res = reverse(list); double *index1 = getElementAt(res, 1); double *index2 = getElementAt(res, 2); double *index5 = getElementAt(res, 5); assert(*(double *)res.head->value == 45.22); assert(*index1 == 12.43); assert(*index2 == 1.435); assert(*index5 == 3.212); assert(*(double *)res.tail->value == 2.21); };
void add_seg(struct load_command *com, t_lsection *list) { unsigned int i; struct section_64 *sec; struct segment_command_64 *seg; i = 0; seg = (struct segment_command_64*)com; sec = (struct section_64*)(seg + sizeof(seg) / sizeof(void*)); while (i < seg->nsects) { add_to_list(sec->sectname, list); sec = (struct section_64 *)(((void*)sec) + sizeof(struct section_64)); i++; } }
/** * hil_create_proc * * This function creates a HIL proc instance for given CPU id and populates * it with platform info. * * @param cpu_id - cpu id * * @return - pointer to proc instance * */ struct hil_proc *hil_create_proc(int cpu_id) { struct hil_proc *proc = NULL; struct llist *node = NULL; struct llist *proc_hd = procs.proc_list; int status; /* If proc already exists then return it */ while (proc_hd != NULL) { proc = (struct hil_proc *)proc_hd->data; if (proc->cpu_id == (unsigned int)cpu_id) { return proc; } proc_hd = proc_hd->next; } /* Allocate memory for proc instance */ proc = env_allocate_memory(sizeof(struct hil_proc)); if (!proc) { return NULL; } /* Get HW specfic info */ status = platform_get_processor_info(proc, cpu_id); if (status) { env_free_memory(proc); return NULL; } /* Enable mapping for the shared memory region */ env_map_memory((unsigned int)proc->sh_buff.start_addr, (unsigned int)proc->sh_buff.start_addr, proc->sh_buff.size, (SHARED_MEM | UNCACHED)); /* Put the new proc in the procs list */ node = env_allocate_memory(sizeof(struct llist)); if (!node) { env_free_memory(proc); return NULL; } node->data = proc; add_to_list(&procs.proc_list, node); return proc; }
void test_for_map_for_adding_5_in_each_element() { void *hint; LinkedList list = createList(); int num[10] = {32,43,223,54,76,7,45,3,4,23}; int arr[10] = {37,48,228,59,81,12,50,8,9,28}; for (int i = 0; i < 10; ++i) { add_to_list(&list,&num[i]); } LinkedList newList = map(list,&add_5,&hint); Element *ele = newList.head; int i =0; while(ele!=NULL) { assert(*(int *)ele->value==arr[i]); i++; ele = ele->next; } }
void join_group(struct char_data *ch, struct group_data *group) { add_to_list(ch, group->members); if (group->leader == NULL) group->leader = ch; ch->group = group; if (IS_SET(group->group_flags, GROUP_NPC) && !IS_NPC(ch)) REMOVE_BIT(GROUP_FLAGS(group), GROUP_NPC); if (group->leader == ch) send_to_group(NULL, group, "%s becomes leader of the group.\r\n", GET_NAME(ch)); else send_to_group(NULL, group, "%s joins the group.\r\n", GET_NAME(ch)); }
static int do_insert_watcher_object(zk_hashtable *ht, const char *path, watcher_object_t* wo) { int res=1; watcher_object_list_t* wl; wl=hashtable_search(ht->ht,(void*)path); if(wl==0) { int res; /* inserting a new path element */ res=hashtable_insert(ht->ht,strdup(path),create_watcher_object_list(wo)); assert(res); } else { /* path already exists; check if the watcher already exists */ res = add_to_list(&wl, wo, 1); } return res; }
/** * Malloc for garbage collection, uses first fit * @arg alloc_size size of allocated memory (in bytes) */ void * GC_malloc(size_t alloc_size) { header_t *block_ptr; block_ptr = first_fit(&freeptr, alloc_size); if(block_ptr == NULL) { block_ptr = morecore(alloc_size); } //Add to used blocks add_to_list(&usedptr, block_ptr); //Give word aligned memory return start_of_block(block_ptr); }
/** * Function to parse and tokenise NMEA sentence strings * @param sentence_in pointer to sentence struct (sentence.h) * @return a linked list containing each token of sentence as separate * nodes (linked_list.h) */ list_ptr parseSentence(sentence_ptr sentence_in){ char *data, *outTemp; data = sentence_in->sentenceData; list_ptr temp_list; init_list(&temp_list); while((outTemp = strsep(&data, ",*")) != NULL){ node_ptr sentence_segment; init_node(&sentence_segment, outTemp); add_to_list(&sentence_segment, &temp_list); } line_count++; return temp_list; }
void html_tag(struct f_data *f, unsigned char *t, int x, int y) { struct tag *tag; unsigned char *tt; int ll; if (!f) return; tt = init_str(); ll = 0; add_conv_str(&tt, &ll, t, strlen(t), -2); tag = mem_alloc(sizeof(struct tag) + strlen(tt) + 1); tag->x = x; tag->y = y; strcpy(tag->name, tt); add_to_list(f->tags, tag); if ((void *)last_tag_for_newline == &f->tags) last_tag_for_newline = tag; mem_free(tt); }