static void write_buf_to_client(t_player *client) { t_network_buf *buf; void *ptr; ssize_t ssize; buf = (t_network_buf *)client->list_buffer_out->data; if (buf) { if (buf->buffer == NULL) { write(client->fd, CMD_KO, sizeof(CMD_KO)); list_pop_front(&(client->list_buffer_out)); return ; } ptr = buf->buffer + (size_t)buf->ptr; ssize = write(client->fd, ptr, buf->size - (size_t)buf->ptr); if (ssize > 0) { buf->ptr += ssize; if ((size_t)buf->ptr >= buf->size) { free(buf->buffer); list_pop_front(&(client->list_buffer_out)); } } } }
/** * @brief tests the push/pop functions of the list module * @return 0 if success * @return 1 if failture */ int test_list_push_pop() { list_t *testList = list_create(); int a = 12345; int b = 98765; int c = 54321; int d = 56789; int *resa, *resb, *resc, *resd; list_push_front(testList, &b); list_push_front(testList, &a); list_push_back(testList, &c); list_push_back(testList, &d); resa = list_pop_front(testList); resb = list_pop_front(testList); resd = list_pop_back(testList); resc = list_pop_back(testList); if(*resa != a || *resb != b || *resc != c || *resd != d) { printf("list push and pop front and back: failture\n"); return 1; } list_destroy(testList); return 0; }
/* Pops all nodes in (list) If (fptr) is set, it is called on every node's data to free it */ void list_clear(t_list *list) { if (!list->f_free) while (list->size > 0) list_pop_front(list); else while (list->size > 0) { list->f_free(list->head->data); list_pop_front(list); } }
/** * DESTRUCTOR * * Member Memory to Destruct: * - [1] Active Duty List [C expanding List] * - [1.1] All Members inside Column * - [1.2] List Duct-tape elements * - [1.3] List Root * - [2] Benched List [C expanding List] * - [2.1] All Members inside Column * - [2.2] List Duct-tape elements * - [2.3] List Root */ Roster::~Roster() { //- Log Imminent Deallocation ------------------------= // // TODO [C++11] - Switch to std::string stream char* s = (char*) malloc(sizeof(char) * 32); sprintf(s, "Roster::[%lub]", sizeof(this->team)); log_memory(s, false); // Don't free 's' yet, there's more logging to do. //- [1] Active Duty List -----------------------------= // // Log // --- // |> TODO [C++11] - Switch to std::string stream sprintf(s, "List::[%lub]", sizeof(this->team)); log_memory(s, false); // // Iterative Pop + Delete while(! list_empty(this->activeDuty)) { // Pop From List struct list_elem * e = list_pop_front(this->activeDuty); PieceDt * tape = list_entry(e, PieceDt, elem); // // Deallocate delete(tape->piece); // [1.1] free(tape); // [1.2] } // Delete List free(this->activeDuty); // [1.3] //- [2] Benched List ---------------------------------= // // Log // --- // |> TODO [C++11] - Switch to std::string stream sprintf(s, "List::[%lub]", sizeof(this->team)); log_memory(s, false); free(s); // now we're done. // // Iterative Pop + Delete while(! list_empty(this->benched)) { // Pop From List struct list_elem * e = list_pop_front(this->benched); PieceDt * tape = list_entry(e, PieceDt, elem); // // Deallocate delete(tape->piece); // [2.1] free(tape); // [2.2] } // Delete List free(this->benched); // [2.3] }
void list_pop_node(t_list *list, t_lnode *node) { t_lnode *w; if (list->size < 3) { if (list->head == node) list_pop_front(list); else if (list->tail == node) list_pop_back(list); } else { for (w = list->head; w != NULL; w = w->next) { if (w == node) { node->prev->next = node->next; node->next->prev = node->prev; list->size--; free(node); return ; } } } }
/** * @brief tests the is_empty function of the list module * @return 0 if success * @return 1 if failture */ int test_list_is_empty() { list_t *testList = list_create(); if(!list_is_empty(testList)) { printf("list is empty: failture\n"); return 1; } int i; for(i = 0; i < FILL_TEST_NUMBER; i++) list_push_front(testList, testList); for(i = 0; i < FILL_TEST_NUMBER; i++) list_pop_front(testList); if(!list_is_empty(testList)) { printf("list is empty: failture\n"); return 1; } list_destroy(testList); return 0; }
int main() { List *list = list_create(sizeof(int)); printf("List: "); int data = 1; list_push_back(list, &data); data = 3; list_push_back(list, &data); data = 5; list_push_back(list, &data); data = 0; list_push_back(list, &data); list_print(list, intPrinter); printf("\r\nRevd: "); list_reverse(list); list_print(list, intPrinter); data = -1; list_push_front(list, &data); printf("\r\nFron: "); list_print(list, intPrinter); list_pop_back(list, &data); printf("\r\nPopb: "); list_print(list, intPrinter); list_pop_front(list, &data); printf("\r\nPopf: "); list_print(list, intPrinter); printf("\r\n"); return 0; }
void sibling_ctrl_set_addresses(struct in6_addr * sibling_ctrl) { struct ctrl_client * ctrl_client; struct route_ipv6 * route_iter; LIST_FOR_EACH(ctrl_client, struct ctrl_client, node, &ctrl_clients) { struct list * ctrl_addrs = get_ctrl_addrs_for_hostnum(ctrl_client->hostnum); LIST_FOR_EACH(route_iter, struct route_ipv6, node, ctrl_addrs) { char s_addr[INET6_ADDRSTRLEN+1]; inet_ntop(AF_INET6, &route_iter->p->prefix, s_addr, INET6_ADDRSTRLEN+1); zlog_debug("done getting ctrl addr %s for hostnum: %d ", s_addr, ctrl_client->hostnum); struct in6_addr * ctrl_addr = calloc(1, sizeof(struct in6_addr)); memcpy(ctrl_addr, &route_iter->p->prefix, sizeof(struct in6_addr)); sibling_ctrl_set_address(ctrl_client, ctrl_addr, sibling_ctrl); } // free the list, no longer needed while(!list_empty(ctrl_addrs)) { struct list * addr_to_remove = list_pop_front(ctrl_addrs); struct route_ipv6 * route_to_remove = CONTAINER_OF(addr_to_remove, struct route_ipv6, node); free(route_to_remove->p); free(route_to_remove->gate); } free(ctrl_addrs); }
/* ** Adds the teams specified in the parser to the game. */ static void add_teams(game_t *gm, list_t *teams) { while (teams->l_size) { game_add_team(gm, list_get_front(teams)); list_pop_front(teams); } }
/* Checks the sleep_list's head element and if it needs * to be awoken, pops it from the sleep_list and unblocks the thread */ static void check_sleep_list (struct list *sleep_list) { // Ensures the sleep_list is not empty, if (!list_empty(&sleep_list)) { struct list_elem *current_elem = list_begin(sleep_list); while(current_elem != list_end(sleep_list)) { struct thread *current_thread = list_entry(current_elem, struct thread, elem); if(current_thread->awake_tick <= ticks) { list_pop_front(sleep_list); if(current_thread->status == THREAD_BLOCKED) { thread_unblock(current_thread); } current_elem = list_begin(sleep_list); } else { break; } } } }
void list_pop_data(t_list *list, void *data) { t_lnode *w; if (list->size < 3) { if (list->head && list->head->data == data) list_pop_front(list); else if (list->tail && list->tail->data == data) list_pop_back(list); } else { for (w = list->head; w != NULL; w = w->next) { if (w->data == data) { w->prev->next = w->next; w->next->prev = w->prev; list->size--; free(w); return ; } } } }
void* frame_victim (enum palloc_flags flag) { lock_acquire(&frame_table_lock); struct list_elem *e= list_begin(&frame_table); //list_ victim_frame=list_pop_front(&frame_table); if(victim_frame ==NULL) { } else { victim_frame->page->valid=false; pagedir_clear_page(victim_frame->thread->pagedir, victim_frame->page->vaddr); palloc_free_page(victim_frame->frame); return palloc_get_page(flag); } lock_release(&frame_table_lock); }
static inline DISPLAY_INFO* get_popup_skelton(NOTIFICATION_INFO* const ni) { DISPLAY_INFO* const di = (DISPLAY_INFO*) list_pop_front(&popup_collections); if (di) { di->ni = ni; return di; } return reset_display_info(create_popup_skelton(), ni); }
int packet_new(struct packet_pool* pool, struct pcap_pkthdr* header, const unsigned char* data) { struct packet* ret = NULL; pthread_mutex_lock(&pool->free_lock); struct list_element_t* e = list_pop_front(pool->free_list); pthread_mutex_unlock(&pool->free_lock); pool->packets_seen++; if (!(pool->packets_seen % 1000000)) { msg(MSG_STATS, "Seen: %llu, Used: %llu, Free: %llu", pool->packets_seen, pool->used_list->size, pool->free_list->size); } if (!e) { pool->packets_lost++; return -1; } ret = e->data; memcpy(&ret->header, header, sizeof(*header)); memcpy(ret->data, data, header->caplen); uint16_t et = ntohs(ETHERNET(data)->ether_type); if (!(et == ETHERTYPE_IP || et == ETHERTYPE_IPV6 || et == ETHERTYPE_VLAN)) { ret->is_ip = ret->is_ip6 = 0; ret->ip = NULL; ret->ip6 = NULL; } uint8_t offset = et == ETHERTYPE_VLAN?4:0; // ethernetheader is shifted by four bytes if vlan is available // we don't know whether we received ip or ipv6. So lets try: if ((IP(data + offset))->ip_v == 4 || et == ETHERTYPE_IP) { ret->is_ip6 = 0; ret->is_ip = 1; ret->ip = IP(ret->data); ret->ip6 = NULL; //msg(MSG_ERROR, "Found IPv4 packet"); } else if ((IP(data + offset))->ip_v == 6 || et == ETHERTYPE_IPV6) { ret->is_ip6 = 1; ret->is_ip = 0; ret->ip = NULL; ret->ip6 = IP6(ret->data); } else { //msg(MSG_ERROR, "Well. Something is weird here!: Ethertype: %d, IP vesrsion: %d", et, (IP(data + offset))->ip_v); } // only handle packets if its connection is still active ret->connection = connection_get(ret); // TODO: we should discard the packet earlier, at best before copying the packet content if (ret->connection && ret->connection->active) { pthread_mutex_lock(&pool->used_lock); list_push_back(pool->used_list, e); pthread_mutex_unlock(&pool->used_lock); } else { packet_free(pool, ret); } return 0; }
void sysexit(int status) { // Print Process Termination Message // File Name char* name = thread_current()->name; char* token, *save_ptr; token = strtok_r(name, " ", &save_ptr); putbuf (token, strlen(token)); char* str1 = ": exit("; putbuf (str1, strlen(str1)); // ExitStatus char strstatus[32]; snprintf(strstatus, 32, "%d", status); putbuf (strstatus, strlen(strstatus)); char* str2 = ")\n"; putbuf (str2, strlen(str2)); // EXIT Child Processes if(thread_current()->numchild > 0) { struct list_elem * e; while (!list_empty(&thread_current()->child_list)) { e = list_pop_front(&thread_current()->child_list); struct childproc * childitem = list_entry (e, struct childproc, elem); if(!exit_remove(childitem->childid)) { list_push_back(&ignore_list, &childitem->elem); } else { free(childitem); } } } // Save exit status struct exitstatus * es = (struct exitstatus *) malloc(sizeof(struct exitstatus)); if(es != NULL && !ignore_remove(thread_current()->tid)) { es->avail = true; es->status = status; es->childid = thread_current()->tid; list_push_back(&exit_list, &es->elem); struct list_elem * e; for (e = list_begin (&waitproc_list); e != list_end (&waitproc_list); e = list_next (e)) { struct waitproc * item = list_entry (e, struct waitproc, elem); sema_up(&item->sema); } }
static void remove_files_atexit(void) { /* we do pop front in case this exit function is called more than once */ while ( files_to_remove ) { remove( object_str( files_to_remove->value ) ); files_to_remove = list_pop_front( files_to_remove ); } }
struct packet* packet_get(struct packet_pool* pool) { pthread_mutex_lock(&pool->used_lock); struct list_element_t* e = list_pop_front(pool->used_list); pthread_mutex_unlock(&pool->used_lock); if (!e) { return NULL; } return e->data; }
/** * Delete the element at the begin of queue. */ void queue_pop(queue_t* pque_queue) { assert(pque_queue != NULL); #ifdef CSTL_QUEUE_LIST_SEQUENCE list_pop_front(&pque_queue->_t_sequence); #else deque_pop_front(&pque_queue->_t_sequence); #endif }
void sl_destroy(struct SynchList *sl) { struct list_elem *e; struct SL_element *sl_elem; while(!list_empty(&sl->sl_list)){ e = list_pop_front(&sl->sl_list); sl_elem = list_entry(e, struct SL_element, elem); free(sl_elem); } }
static void write_handler(nl_event_t *ev) { nl_socket_t *sock; nl_stream_t *s; nl_buf_t *buf; int rc; sock = ev->data; log_trace("#%d write_handler", sock->fd); s = sock->data; while (!list_empty(s->tosend)) { buf = (nl_buf_t *)list_front(s->tosend); rc = nl_send(sock, buf->buf, buf->len); if (rc >= 0) { if ((size_t)rc < buf->len) { /* accurate pending bytes */ buf->len -= rc; memmove(buf->buf, buf->buf + rc, buf->len); if (s->cbs.on_sent) { s->cbs.on_sent(s, rc); } } else { /* accurate pending bytes */ list_pop_front(s->tosend); if (s->cbs.on_sent) { s->cbs.on_sent(s, rc); } free(buf->buf); } } else { /* rc < 0 */ if (!sock->error) { return; } else { s->error = 1; if (s->closing_ev.timer_set) { nl_event_del_timer(&s->closing_ev); } nl_stream_close(s); return; } } } /* tosend is empty */ nl_event_del(&s->sock.wev); if (s->closing_ev.timer_set) { nl_event_del_timer(&s->closing_ev); nl_stream_close(s); } }
static int GetTimerID() { st_timer *pTimer = (st_timer*)list_begin(&g_listTimerFree); if (list_empty(&g_listTimerFree)) { LOG_WRITE_POS(LOG_ERR, "ID using transfinite.\n"); return TIMER_ERROR; } list_pop_front(&g_listTimerFree); return pTimer->id; }
static void delete_actions(t_list *actions) { t_actions *action; while (!list_is_empty(actions)) { action = (t_actions *)list_get_front(actions); freetab(action->av); free(action); list_pop_front(&actions); } }
static void free_incantation(t_server *infos) { t_incantation *incantation; while (infos->incantation_list != NULL) { incantation = (t_incantation *)infos->incantation_list->data; free(incantation->player); list_pop_front(&infos->incantation_list); } infos->incantation_list = NULL; }
/*! Memory ummap according the given mapid. */ void munmap(mapid_t mapping){ uint32_t f_size, write_bytes, page_write_size, pws2; off_t ofs = 0; struct list_elem *e; struct supp_table* st; /* First find the mmap struct according to the given mapid. */ struct mmap_elem *me = find_mmap_elem(mapping); struct thread* t = thread_current(); /* Get the file length. And set write_bytes as file length.*/ f_size = file_length(me->file); write_bytes = f_size; /* Freeing all the pages in this mmap struct. */ while (!list_empty(&me->s_table)) { e = list_pop_front(&me->s_table); st = list_entry(e, struct supp_table, map_elem); if (st->fr) { /* Set up how many bytes is going to be freed in this page. */ if (write_bytes >= PGSIZE) page_write_size = PGSIZE; else page_write_size = write_bytes; /* If the page is dirty, then write back the data to the file. */ if (pagedir_is_dirty(t->pagedir, st->upage)){ lock_acquire(&filesys_lock); pws2 = file_write_at(st->file, st->fr->physical_addr, st->read_bytes, st->ofs); lock_release(&filesys_lock); ASSERT(pws2 == page_write_size); } /* Update the offset for file writing.*/ ofs += page_write_size; /* Update remaining write_bytes.*/ write_bytes -= page_write_size; } /* Destroy the freed supplemental page entry.*/ spte_destructor_func(&(st->elem), NULL); } /* Close the file. */ file_close(me->file); /* Remove the mmap struct from the mmap list of this process. */ list_remove(&(me->elem)); /* Free the memory of this struct. */ free(me); }
int session_destroy (sid_t sid) { struct list_elem *e; session_t * session = NULL; link_t * link = NULL; pthread_mutex_lock (&session_mutex); for (e = list_begin (&session_list); e != list_end (&session_list); e = list_next (e)) { session_t *s = list_entry (e, session_t, elem); if (s->sid == sid) { list_remove (e); session = s; } } pthread_mutex_unlock (&session_mutex); if (session == NULL) { return 0; } while (!list_empty (&session->link_list)) { e = list_pop_front (&session->link_list); link = list_entry (e, link_t, elem); pthread_mutex_lock (&link->mutex); if (link->path == NULL) pthread_cond_broadcast (&link->cond_set); while (link->waiter_cnt > 0) { pthread_mutex_unlock (&link->mutex); //pthread_yield (); pthread_mutex_lock (&link->mutex); } pthread_mutex_unlock (&link->mutex); free (link->path); free (link); } free (session); return 0; }
static uint8_t * get_frame (void) { void *result = NULL; if (!list_is_empty (&free_frames)) { struct list_elem *e = list_pop_front (&free_frames); list_push_front (&free_entry_refs, e); SWAP (result, list_entry (e, struct frame_ref, elem)->data); --counter; } return result; }
void test_ppage(void) { struct my_ppage * ppage_list = NULL, * my_ppage; count_t num_alloc = 0, num_free = 0; count_t totalpg, usedpg; memory_ppages_status(&totalpg, &usedpg); printf("Total pages: %d, used pages initial: %d", totalpg, usedpg); while ((my_ppage = (struct my_ppage*)memory_new_ppage()) != NULL) { int i; bochs_printf("\nCould allocate %d pages", num_alloc); num_alloc++; for (i = 0; i < MY_PPAGE_SIZE; i++) my_ppage->data[i] = (u32)my_ppage; list_push_back(my_ppage, ppage_list); } printf("\nCould allocate %d pages", num_alloc); while ((my_ppage = list_pop_front(ppage_list)) != NULL) { int i; for (i = 0; i < MY_PPAGE_SIZE; i++) { if (my_ppage->data[i] != (u32)my_ppage) { printf("\nPage overwriten"); return; } } if (memory_decref_ppage((paddr)my_ppage) != EPPAGEREF) { printf("\nCannot release page"); return; } num_free++; bochs_printf("\nCould release %d pages", num_free); } printf("\nCould free %d pages", num_free); printf("\nCould allocate %d bytes and could free %d bytes", num_alloc << PAGE_SHIFT, num_free << PAGE_SHIFT); assert(num_alloc == num_free); }
void shedule() { proc_t *tmp; int i = 0; do { tmp = list_pop_front(procs_list); list_push_back(procs_list, tmp); i++; } while (tmp->state != PS_RUNABLE && i < procs_list->items_count); if (tmp->state != PS_RUNABLE) current_proc = idle_proc; else current_proc = tmp; }
void *sl_remove(struct SynchList *sl) { struct list_elem *e; void *item; lock_acquire(&sl->sl_lock); // enforce mutual exclusion while(list_empty(&sl->sl_list)){ cond_wait(&sl->sl_empty, &sl->sl_lock); // wait until list isn't empty } e = list_pop_front(&sl->sl_list); struct SL_element *sl_elem = list_entry(e, struct SL_element, elem); item = sl_elem->item; free(sl_elem); lock_release(&sl->sl_lock); return item; }
bool _solver_bfs_move(void *data, const maze *m, vec2 *pos) { bfs_data bfs = *((bfs_data *) data); list *neighbors; vec2 neigh; set_insert(bfs.visited, pos); neighbors = maze_get_neighbors(m, *pos); while (!list_is_empty(neighbors)) { list_pop_front(neighbors, &neigh); if (!map_contains_key(bfs.parent, &neigh) && !set_contains(bfs.visited, &neigh)) { map_put(bfs.parent, &neigh, pos); list_push_back(bfs.queue, &neigh); } } list_destroy(neighbors); if (list_is_empty(bfs.queue)) { return false; } list_pop_front(bfs.queue, pos); return true; }