/* * Startup the server listener. */ int startup_server(void) { int optval = 1; /* Initialize client_info list */ llist_init(&list_start); /* Create socket */ server_sockfd = socket(AF_INET, SOCK_STREAM, 0); if (setsockopt(server_sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval) != 0) { logline(LOG_DEBUG, "Error calling setsockopt(): %s", strerror(errno)); return -1; } /* Name the socket */ server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = inet_addr(params->ip); server_address.sin_port = htons(params->port); server_len = sizeof(server_address); if (bind(server_sockfd, (struct sockaddr *)&server_address, server_len) != 0) { logline(LOG_DEBUG, "Error calling bind(): %s", strerror(errno)); return -2; } /* Create a connection queue and wait for incoming connections */ if (listen(server_sockfd, 5) != 0) { logline(LOG_DEBUG, "Error calling listen(): %s", strerror(errno)); return -3; } return 0; }
int get_messages(char * host, int port,char * user, char * password, int msg_num,char * room, LinkedList ** r) { char response[ MAX_RESPONSE ]; char * pch; char * tt; int i = 1; char num[20*1024]; sprintf(num, "%d", msg_num); LinkedList * link = (LinkedList *) malloc (sizeof(LinkedList)); llist_init(link); sendCommand(host, port, strdup("GET-MESSAGES"), user, password, num, room, response); printf("GET-MESSAGES, Room %s User %s\n", room, user); printf("Messages from Server: %s\n", response); if(!strcmp(response,"NO-NEW-MESSAGES\r\n")){ return 2; } pch =strtok(response, "\r\n"); while(pch != NULL){ printf("Messages: %s\n", pch); llist_add(link, pch); pch = strtok(NULL, "\r\n"); } *r = link; return 1; }
// // It reads the list from the file_name indicated. If the list already has entries, // it will clear the entries. // int llist_read(LinkedList * list, char * file_name) { FILE * fd = fopen(file_name, "r"); int c; char word[300]; if (fd == NULL) return 0; else { int i=0; llist_init(list); while((c=fgetc(fd))!=EOF) { if (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\b'){ word[i] = 0; if(i > 0) llist_add(list,atoi(word)); //word = "\0";} i = 0; } else if (c == EOF) break; else word[i++] = c; } if(i > 0) llist_add(list,atoi(word)); (ListNode *)malloc(sizeof(ListNode)); } return 1; }
static struct pack_list * add_pack(struct packed_git *p) { struct pack_list l; unsigned long off = 0, step; const unsigned char *base; if (!p->pack_local && !(alt_odb || verbose)) return NULL; l.pack = p; llist_init(&l.all_objects); if (open_pack_index(p)) return NULL; base = p->index_data; base += 256 * 4 + ((p->index_version < 2) ? 4 : 8); step = the_hash_algo->rawsz + ((p->index_version < 2) ? 4 : 0); while (off < p->num_objects * step) { llist_insert_back(l.all_objects, (const struct object_id *)(base + off)); off += step; } /* this list will be pruned in cmp_two_packs later */ l.unique_objects = llist_copy(l.all_objects); if (p->pack_local) return pack_list_insert(&local_packs, &l); else return pack_list_insert(&altodb_packs, &l); }
// Add word to the tableand position. Position is added to the corresponding linked list. void wtable_add(WordTable * wtable, char * word, int position) { // Find first word if it exists for (int i = 0; i < wtable->nWords; i++) { if ( strcmp(wtable->wordArray[i].word, word)== 0 ) { // Found word. Add position in the list of positions llist_insert_last(&wtable->wordArray[i].positions, position); return; } } if (wtable-> nWords == wtable->maxWords) { wtable->maxWords = wtable->maxWords*2; int newsize = wtable->maxWords*sizeof(WordInfo); wtable->wordArray = (WordInfo*) realloc(wtable->wordArray, newsize); for (int i = wtable->nWords; i < wtable->maxWords; i++) { llist_init(&wtable->wordArray[i].positions); } } // Word not found. // Make sure that the array has space. // Expand the wordArray here. // Add new word and position wtable->wordArray[wtable->nWords].word = strdup(word); llist_insert_last(&wtable->wordArray[wtable->nWords].positions, position); wtable->nWords++; }
static struct llist * llist_copy(struct llist *list) { struct llist *ret; struct llist_item *new_item, *old_item, *prev; llist_init(&ret); if ((ret->size = list->size) == 0) return ret; new_item = ret->front = llist_item_get(); new_item->oid = list->front->oid; old_item = list->front->next; while (old_item) { prev = new_item; new_item = llist_item_get(); prev->next = new_item; new_item->oid = old_item->oid; old_item = old_item->next; } new_item->next = NULL; ret->back = new_item; return ret; }
LumieraMruCache lumiera_mrucache_init (LumieraMruCache self, lumiera_cache_destructor_fn destructor_cb) { REQUIRE (self); llist_init (&self->cache_list); self->cached = 0; self->destructor_cb = destructor_cb; return self; }
domaininfo *domaininfo_init(char *domainname) { domaininfo *domain = (domaininfo *)malloc(sizeof(domaininfo)); domain->name = (char *)malloc(sizeof(char) * (strlen(domainname) + 1)); strcpy(domain->name, domainname); domain->numpages = 0; llist_init(&domain->pages, &compare_domain_name); return domain; }
// Initializes a word table void wtable_init(WordTable * wtable) { // Allocate and initialize space for the table wtable->nWords = 0; wtable->maxWords = 10; wtable->wordArray = (WordInfo *) malloc(wtable->maxWords * sizeof(WordInfo)); for (int i = 0; i < wtable->maxWords; i++) { llist_init(&wtable->wordArray[i].positions); } }
void hash_map_init(hash_map *hm, int (*comp)(void*, void*), int (*hash)(void*), int hashLength) { hm->comp = comp; hm->hash = hash; hm->boxes = malloc(sizeof(llist*)*hashLength); for(int i=0; i<hashLength; i++) { hm->boxes[i] = malloc(sizeof(llist)); llist_init(hm->boxes[i]); } hm->hashLength = hashLength; }
void wtable_reinit(WordTable * wtable) { //wtable->wordArray = (WordInfo *) malloc(wtable->maxWords * sizeof(WordInfo)); //wtable->maxWords *= 2; //printf("maxWords %d\n", wtable->maxWords); int temp = wtable->maxWords; wtable->maxWords *= 2; wtable->wordArray = (WordInfo *) realloc(wtable->wordArray, (wtable->maxWords) * sizeof(WordInfo)); for (int i = temp + 1; i < wtable->maxWords; i++) { llist_init(&wtable->wordArray[i].positions); } }
TESTS_BEGIN TEST (basic) { LLIST_AUTO (node1); llist node2; llist_init (&node2); printf ("%d\n", llist_is_empty (&node1)); printf ("%d\n", llist_is_empty (&node2)); }
LumieraConfigLookupentry lumiera_config_lookupentry_init (LumieraConfigLookupentry self, const char* key) { TRACE (configlookup_dbg, "%s", key); if (self) { psplaynode_init (&self->node); llist_init (&self->configitems); self->full_key = lumiera_strndup (key, SIZE_MAX); } return self; }
static int llist_iteration() { LList l[1]; llist_init(l, sizeof(int)); for (int i = 1; i <= 10; i++) { pushback(l, i); } int remove_even(void* data) { int val = *(int*)data; return (val % 2 == 0) ? LLIST_REMOVE : 0; }
/* Copy list from (l) to (t) */ int llist_copy(llist *l, llist *t, void* (*f)(void *d, void *u), void *u) { llist_elm *i = l->head; llist_init(t); while( i ){ llist_add(t,f(i->data,u)); i = i->next; } return 0; }
struct curl_llist * Curl_llist_alloc(curl_llist_dtor dtor) { struct curl_llist *list; list = malloc(sizeof(struct curl_llist)); if(!list) return NULL; llist_init(list, dtor); return list; }
static void test_linked_list() { size_t i; LinkedList llist; llist_init(&llist); LinkedNode nodes[100]; for(i = 0; i < 100; i++) { nodes[i].data = i; llist_push(&llist, &nodes[i]); assert(llist.last == &nodes[i]); } }
void __hash_add(void **htable, unsigned int hash_size, const char *str, void *data, unsigned int size) { unsigned int h; h = __hash_code(str, hash_size); if (htable[h] == NULL) { htable[h] = _memalloc(sizeof(llist_t)); llist_init(htable[h]); } llist_add(htable[h], data, size); }
struct msg_que* new_msgque(uint32_t syn_size,item_destroyer destroyer) { pthread_once(&g_msg_que_key_once,msg_que_once_routine); struct msg_que *que = calloc(1,sizeof(*que)); pthread_key_create(&que->t_key,delete_per_thread_que); dlist_init(&que->blocks); que->mtx = mutex_create(); que->refbase.destroyer = delete_msgque; llist_init(&que->share_que); dlist_init(&que->can_interrupt); que->syn_size = syn_size; que->destroy_function = destroyer; get_per_thread_que(que,MSGQ_NONE); return que; }
static void log_once_routine(){ //pthread_key_create(&g_log_key,NULL); llist_init(&g_log_file_list); sys_log = calloc(1,sizeof(*sys_log)); sys_log->filename = new_string(SYSLOG_NAME); g_mtx_log_file_list = mutex_create(); mutex_lock(g_mtx_log_file_list); LLIST_PUSH_BACK(&g_log_file_list,sys_log); mutex_unlock(g_mtx_log_file_list); pending_log = new_msgque(64,default_item_destroyer); g_log_thd = create_thread(THREAD_JOINABLE); thread_start_run(g_log_thd,log_routine,NULL); atexit(on_process_end); LOG(sys_log,LOG_INFO,"log open success"); }
int server_socket_init(void) { int yes = 1; // first, load up address structs with getaddrinfo(): memset(&server, 0, sizeof(server)); /* Initialize client_info list */ llist_init(&list_start); //server.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever //server_fd = socket(AF_INET6, SOCK_STREAM, AI_PASSIVE); server.ai_family = AF_INET6; server.ai_socktype = SOCK_STREAM; server.ai_flags = AI_PASSIVE; // fill in my IP for me getaddrinfo(NULL, PORT, &server, &res); server_fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if(server_fd == -1) { printf("Unsuccessful socket creation\n"); } else { printf("Server Socket Created Successfully \n"); } // Avi check on this Sock reusable param. if(setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != 0) { printf("Error Setting Socket Reusable\n"); return -1; } if(bind(server_fd, res->ai_addr, res->ai_addrlen) != 0) { printf("Error Binding\n"); return -1 ; } if(listen(server_fd, BACKLOG) != 0) { printf("Error Listening\n"); } return 0; }
static int llist_basic() { LList l[1]; llist_init(l, sizeof(int)); pushfront(l, 3); pushback(l, 4); pushfront(l, 2); pushfront(l, 1); pushback(l, 5); assertEqual(l->size, 5); assertEqual(popfront(l), 1); assertEqual(popfront(l), 2); assertEqual(popfront(l), 3); assertEqual(popfront(l), 4); assertEqual(popfront(l), 5); assertEqual(l->size, 0); llist_close(l); return 0; }
void print_users(char * host, int port,char * user, char * password, LinkedList ** r) { char response[ MAX_RESPONSE ]; char * pch; LinkedList * link = (LinkedList *) malloc (sizeof(LinkedList)); llist_init(link); sendCommand(host, port, strdup("GET-ALL-USERS"), user, password, NULL, NULL, response); printf("GET-ALL-USERS, User %s\n", user); printf("Messages from Server: %s\n", response); pch =strtok(response, "\r\n"); while(pch != NULL){ printf("User: %s\n", pch); llist_add(link, pch); pch = strtok(NULL, "\r\n"); } //llist_sort(link, 1); *r = link; }
int test_datastructure_link_list() { llist_t *llist; allocator_t *allocator; struct test t1={1,2}; struct test t2={2,2}; struct test t3={3,2}; struct test t4={4,2}; int ret = 0; /* *allocator = allocator_creator(ALLOCATOR_TYPE_CTR_MALLOC); *allocator_ctr_init(allocator, 0, 0, 1024); *dbg_str(DBG_CONTAINER_DETAIL,"list allocator addr:%p",allocator); */ allocator = allocator_creator(ALLOCATOR_TYPE_SYS_MALLOC,0); //ct = container_creator(CONTAINER_TYPE_LIST,allocator); llist = llist_create(allocator,0); llist_init(llist,sizeof(struct test)); llist_push_front(llist,&t1); llist_push_front(llist,&t2); llist_push_front(llist,&t3); llist_push_front(llist,&t4); llist_push_back(llist,&t1); llist_push_back(llist,&t2); llist_push_back(llist,&t3); llist_push_back(llist,&t4); llist_for_each(llist,print_list_data); llist_destroy(llist); return ret; }
static void load_all_objects(void) { struct pack_list *pl = local_packs; struct llist_item *hint, *l; llist_init(&all_objects); while (pl) { hint = NULL; l = pl->all_objects->front; while (l) { hint = llist_insert_sorted_unique(all_objects, l->oid, hint); l = l->next; } pl = pl->next; } /* remove objects present in remote packs */ pl = altodb_packs; while (pl) { llist_sorted_difference_inplace(all_objects, pl->all_objects); pl = pl->next; } }
int cmd_pack_redundant(int argc, const char **argv, const char *prefix) { int i; struct pack_list *min, *red, *pl; struct llist *ignore; struct object_id *oid; char buf[GIT_MAX_HEXSZ + 2]; /* hex hash + \n + \0 */ if (argc == 2 && !strcmp(argv[1], "-h")) usage(pack_redundant_usage); for (i = 1; i < argc; i++) { const char *arg = argv[i]; if (!strcmp(arg, "--")) { i++; break; } if (!strcmp(arg, "--all")) { load_all_packs = 1; continue; } if (!strcmp(arg, "--verbose")) { verbose = 1; continue; } if (!strcmp(arg, "--alt-odb")) { alt_odb = 1; continue; } if (*arg == '-') usage(pack_redundant_usage); else break; } if (load_all_packs) load_all(); else while (*(argv + i) != NULL) add_pack_file(*(argv + i++)); if (local_packs == NULL) die("Zero packs found!"); load_all_objects(); cmp_local_packs(); if (alt_odb) scan_alt_odb_packs(); /* ignore objects given on stdin */ llist_init(&ignore); if (!isatty(0)) { while (fgets(buf, sizeof(buf), stdin)) { oid = xmalloc(sizeof(*oid)); if (get_oid_hex(buf, oid)) die("Bad object ID on stdin: %s", buf); llist_insert_sorted_unique(ignore, oid, NULL); } } llist_sorted_difference_inplace(all_objects, ignore); pl = local_packs; while (pl) { llist_sorted_difference_inplace(pl->unique_objects, ignore); pl = pl->next; } minimize(&min); if (verbose) { fprintf(stderr, "There are %lu packs available in alt-odbs.\n", (unsigned long)pack_list_size(altodb_packs)); fprintf(stderr, "The smallest (bytewise) set of packs is:\n"); pl = min; while (pl) { fprintf(stderr, "\t%s\n", pl->pack->pack_name); pl = pl->next; } fprintf(stderr, "containing %lu duplicate objects " "with a total size of %lukb.\n", (unsigned long)get_pack_redundancy(min), (unsigned long)pack_set_bytecount(min)/1024); fprintf(stderr, "A total of %lu unique objects were considered.\n", (unsigned long)all_objects->size); fprintf(stderr, "Redundant packs (with indexes):\n"); } pl = red = pack_list_difference(local_packs, min); while (pl) { printf("%s\n%s\n", sha1_pack_index_name(pl->pack->sha1), pl->pack->pack_name); pl = pl->next; } if (verbose) fprintf(stderr, "%luMB of redundant packs in total.\n", (unsigned long)pack_set_bytecount(red)/(1024*1024)); return 0; }
url_llist *getcache(char *folder, char *searchstring) { char *modifiedstring = tounderline(searchstring); char *path = getpath(folder, modifiedstring); FILE *file = fopen(path, "r"); // return null if unable to open for read (indicating file doesn't exist if (!file) return NULL; // get number of urls int numlinks; fscanf(file, "%d\n", &numlinks); // creat array of urls, and a corresponding array holding indexes of urls each url points to urlinfo *urls[numlinks]; llist outlink_indexes[numlinks]; // initialize output list url_llist *output = malloc(sizeof(url_llist)); url_llist_init(output); // read each url (each is on a separate line) // push to linked list char urlstring[MAXLENGTH]; int numoutlinks; urlinfo *url; // pass 1: index urls unsigned long *outlink_index; unsigned long i, j; while(fscanf(file, "%s %d\n", urlstring, &numoutlinks) != EOF) { // construct url (without outlinks) url = makeURL(urlstring, NULL); // push url and a linked list for its outlinks url_llist_push_back(output, url); urls[i] = url; llist_init(&(outlink_indexes[i]), (void *)comparelong); // read and push each outlink for (j = 0; j < numoutlinks; j++) { outlink_index = malloc(sizeof(unsigned long)); fscanf(file, "%lu ", outlink_index);//outlink_index)) llist_push_back(&(outlink_indexes[i]), outlink_index); } // read in '\n' fscanf(file, "\n"); i++; } lnode *current_node, *prev_node; // pass 2: get outlinks by their indexes for (i = 0; i < numlinks; i++) { url = urls[i]; current_node = outlink_indexes[i].front; while (current_node) { outlink_index = current_node->data; prev_node = current_node; llist_push_back(&urls[i]->outlinks, urls[(long)*outlink_index]); current_node = current_node->next; free(outlink_index); free(prev_node); } } free(path); free(modifiedstring); close(file); return output; }
int main(int argc, char **argv) { llist_t *paths,*src,*todo; set_t *incl; map_t *deps; if (argc < 2) { fprintf(stderr,"FastDep v%s for LAMMPS\n" "Usage: %s [-I <path> ...] -- <src1> [<src2> ...]\n", version,argv[0]); fprintf(stderr,"Supported extensions: %s, %s, %s\n", extensions[0], extensions[1], extensions[2]); return 1; } /* hash tables for all known included files and dependencies * we guesstimate a little over 2x as many entries as sources. */ incl = set_init(2*argc); deps = map_init(2*argc); /* list of include search paths. prefixed by "." and "..". */ paths = llist_init(); llist_append(paths,"."); llist_append(paths,".."); while (++argv, --argc > 0) { if (strncmp(*argv, "-I", 2) == 0) { if ((*argv)[2] != '\0') { llist_append(paths,trim_path(*argv+2)); } else { ++argv; --argc; if (argc > 0) { if (strcmp(*argv,"--") == 0) { break; } else { llist_append(paths,trim_path(*argv)); } } } } else if (strcmp(*argv,"--") == 0) { break; } /* ignore all unrecognized arguments before '--'. */ } src = llist_init(); while (++argv, --argc > 0) { llist_append(src,*argv); } /* process files to look for includes */ todo = llist_init(); find_includes(src->head,todo,paths,incl,deps); find_includes(todo->head,todo,paths,incl,deps); llist_free(todo); fprintf(stdout,"# FastDep v%s for LAMMPS\n",version); fputs("# Search path: ",stdout); llist_print(paths); fprintf(stdout,"# % 5d sources\n# % 5d includes\n# % 5d depfiles\n", llist_size(src),set_size(incl),map_size(deps)); set_free(incl); do_depend(src->head,deps); llist_free(src); llist_free(paths); map_free(deps); return 0; }
int main() { running = 1; signal(SIGINT, interrupt_handler); printf("Starting server\n"); if(!init_server_socket(&sock, PORT)) return -1; //A linked list might not be the most efficient for this llist client_sessions; llist_init(&client_sessions); queue message_queue; queue_init(&message_queue); //TODO: initialise thread pool const int num_workers = 4; pthread_t workers[num_workers]; for(int i=0; i<num_workers; i++) { pthread_create(&workers[i], NULL, worker_run, (void*)&message_queue); } //select stuff fd_set readsocketset; fd_set writesocketset; fd_set errorsocketset; struct timeval timeout; while(running) { timeout.tv_sec = 1; timeout.tv_usec = 0; printf("1\n"); build_socket_list(&client_sessions, &readsocketset); FD_ZERO(&readsocketset); FD_SET(sock, &readsocketset); int s = select(client_sessions.len, &readsocketset, 0, 0, &timeout); printf("2\n"); if(s < 0) { printf("ERROR: Select error\n"); exit(1); } //if we have a new connection create a new session if(FD_ISSET(sock, &readsocketset)) { int csock = check_for_connections(sock); session clientSession; session_init(&clientSession, csock); llist_append(&client_sessions, (void*)&clientSession); } printf("2\n"); //check if each session exists in the read socket thingE llist_node *cur = client_sessions.head; while(cur != NULL) { int sock = ((session*)cur->data)->sock; //check readsocketset if(FD_ISSET(sock, &readsocketset)) client_read_data((session*)cur->data); //check writesocketset //check errorset cur = cur->next; } //TODO: //parse the messages //add parsed message to the queue //send messages on the queue (Should this be done here?) } printf("Exiting..\n"); //free memory llist_node *cur = client_sessions.head; while(cur != NULL) { session *sess = (session*)cur->data; session_end(sess); free(sess); cur = cur->next; } llist_free(&client_sessions); close(sock); pthread_exit(NULL); }