dl_node *dllist_merge_merge(dl_node *left, dl_node *right, int(*cmp)(const void *, const void *)) { dl_node *result = NULL, *next = NULL; while (left != NULL && right != NULL) { if (cmp(left->item, right->item) < 0) { next = left->next; dllist_append(&result, left); left = next; } else { next = right->next; dllist_append(&result, right); right = next; } } while (left != NULL) { next = left->next; dllist_append(&result, left); left = next; } while (right != NULL) { next = right->next; dllist_append(&result, right); right = next; } return result; }
dl_node *dllist_merge_sort(dl_node **llist, int(*cmp)(const void *, const void *)) { dl_node *m, *left = NULL, *right = NULL, *next = NULL; int i, size; // Base-case if (*llist == NULL || (*llist)->next == NULL) return *llist; // Count until the end for (size = 0, m = *llist; m != NULL; size++, m = m->next); // Split items for (i = 0, m = *llist; m != NULL; i++, m = next) { next = m->next; if (i < (size) / 2) { dllist_append(&left, m); } else { dllist_append(&right, m); } } left = dllist_merge_sort(&left, cmp); right = dllist_merge_sort(&right, cmp); return dllist_merge_merge(left, right, cmp); }
/* Add a critter to the list */ void add_critter (struct Critter * newcritter) { int kill=0; /* Enforce hostile critter limits */ if(newcritter->gfx == soldier_gfx && game_settings.soldiers>0 && newcritter->owner>=0) { if(++soldier_count[newcritter->owner] >= game_settings.soldiers) kill=1; } else if(newcritter->gfx == helicopter_gfx && game_settings.helicopters>0 && newcritter->owner>=0) { if(++helicopter_count[newcritter->owner] >= game_settings.helicopters) kill=1; } if(kill) { struct dllist *ptr = critter_list; while(ptr) { struct Critter *c = ptr->data; if(c->gfx == newcritter->gfx && c->owner == newcritter->owner) { kill_critter(ptr); break; } ptr=ptr->next; } } /* Add critter to the list */ if (critter_list) dllist_append(critter_list,newcritter); else critter_list=dllist_append(critter_list,newcritter); }
/* Add a new level special */ void add_special (struct SpecialObj *special) { if(special) { if(special_list) dllist_append(special_list,special); else special_list=dllist_append(special_list,special); } }
void music_add_song (const char *filename) { #if HAVE_LIBSDL_MIXER if(playlist) dllist_append(playlist,strdup(filename)); else playlist = dllist_append(NULL,strdup(filename)); #endif }
void ptimer_start(ptimer_table_t *table, ptimer_t *timer, u32 timeval) { u32 slot; if(table == NULL || timer == NULL) return; if(timer->flags & PTIMER_FLAG_RUNNING) { /* avoid timer is started multiple times */ ZLOG_WARN("timer is running, ignore it: 0x%p timeval=%d\n", timer, timeval); return; } timer->flags |= PTIMER_FLAG_RUNNING; timer->duration = timeval; if(timeval >= table->allslots) { timer->remainder = timeval - table->allslots + 1; slot = (table->curslot + table->allslots - 1) & (table->allslots - 1); } else { timer->remainder = 0; /* find register slot */ slot = (table->curslot + timeval) & (table->allslots - 1); } dllist_append(&table->table[slot], (dllist_node_t *)timer); // ZLOG_DEBUG("start timer: 0x%p timeval=%u, curslot=%u target_slot=%u\n", timer, timeval, table->curslot, slot); }
int add_task(char* filename, char* string){ tasklist* list = tasklist_new(); Task* task = task_new(); FILE* file = fopen(filename, "r"); if (file == NULL){ file = fopen(filename, "w+"); if (file == NULL){ perror("Add task"); goto error; } } if (tasklist_read(list, file) != 0){ puts("ERROR: could not read tasklist."); goto error; } task_append(task, string); dllist_append(list, task); freopen(filename, "r+", file); if (tasklist_dump(list, file) != 0){ puts("ERROR: could not write tasklist."); goto error; } printf("Added: %s\n", string); tasklist_destroy(list); return 0; error: tasklist_destroy(list); return 1; }
/* Create the input settings menu */ static struct Menu *make_input_menu(struct Menu *parent) { static const char *names[]={"Player 1 controller","Player 2 controller", "Player 3 controller", "Player 4 controller"}; struct Menu *m; int r; /* Create menu */ m = create_menu(0,parent,NULL,NULL,NULL,0); add_caption(m,"Input settings"); for(r=0;r<4;r++) { struct MenuValue sub; struct MenuItem *item; struct MenuIcon *icon; sub.value = (int*)make_controller_menu(m,r,names[r]); item=add_menu_item(m,MNU_ITEM_SUBMENU,r+1,menu_txt_label(names[r]),sub); icon = menu_icon_draw(draw_input_icon); item->icons = dllist_append(NULL,icon); } add_ok(m); return m; }
/* Get a list of level thumbnails */ static struct dllist *get_level_thumbnails(void) { struct dllist *levels=game_settings.levels; struct dllist *thumbnails=NULL; while(levels->prev) levels = levels->prev; while(levels) { struct LevelThumbnail *level = malloc(sizeof(struct LevelThumbnail)); level->file = levels->data; level->name = renderstring(Smallfont, level->file->settings->mainblock.name, font_color_cyan); level->thumbnail = level->file->settings->thumbnail; if(level->thumbnail && level->thumbnail->h != THUMBNAIL_HEIGHT) { fprintf(stderr,"Level \"%s\" thumbnail height is not 120 (%d)!\n", level->file->settings->mainblock.name,level->thumbnail->h); } thumbnails = dllist_append(thumbnails,level); levels=levels->next; } while(thumbnails->prev) thumbnails=thumbnails->prev; return thumbnails; }
const ActorClass *getReceiverClass(int tokenSize, tokenFn *functions) { ensureInitialized(); //Detect need for serialization with if such function is provided int needSerialization = functions->serialize != NULL; /* * Linear search should be fine: this list is likely to be small, and * this is only done during network construction time, not at runtime. */ dllist_element_t *elem = dllist_first(&receiver_classes); while (elem) { struct extended_class *xclass = (struct extended_class *) elem; if(needSerialization) { //If we already have a class pointing to the same serialization function take it if (xclass->portDescription.functions->serialize == functions->serialize) { return &xclass->actorClass; } } else { if (xclass->portDescription.tokenSize == tokenSize) { return &xclass->actorClass; } } elem = dllist_next(&receiver_classes, elem); } /* no class found -- we need to create one */ struct extended_class *xclass = calloc(1, sizeof(struct extended_class)); /* make up a name */ if(needSerialization) snprintf(xclass->className, GENERATED_CLASS_NAME_MAX, "_receiver_%dB%8p", tokenSize, functions->serialize); else snprintf(xclass->className, GENERATED_CLASS_NAME_MAX, "_receiver_%dB", tokenSize); xclass->portDescription.name = "out"; xclass->portDescription.tokenSize = tokenSize; if(needSerialization) { xclass->portDescription.functions = calloc(1,sizeof(tokenFn)); memcpy(xclass->portDescription.functions, functions, sizeof(tokenFn)); } xclass->actorClass.majorVersion = ACTORS_RTS_MAJOR; xclass->actorClass.minorVersion = ACTORS_RTS_MINOR; xclass->actorClass.name = xclass->className; xclass->actorClass.sizeActorInstance = sizeof(ActorInstance_art_SocketReceiver); xclass->actorClass.numInputPorts = 0; xclass->actorClass.numOutputPorts = 1; xclass->actorClass.outputPortDescriptions = &xclass->portDescription; xclass->actorClass.action_scheduler = &receiver_action_scheduler; xclass->actorClass.constructor = &receiver_constructor; xclass->actorClass.destructor = &receiver_destructor; dllist_append(&receiver_classes, &xclass->elem); return &xclass->actorClass; }
/* the sink set the rlc um rx -> deliv function pointer to this sink function */ void sink(struct rlc_entity_um_rx *umrx, rlc_sdu_t* sdu) { /* 1. update this packet’s end to end delay (received time = the current simulation time) 2. put this packet to the received packet list @g_sink_packet_list */ /* 1. */ packet_t *pktt = sdu->pktt; pktt->rx_deliver_timestamp = g_time_elasped_in_us; /* 2. */ dllist_append(&g_sink_packet_list, &(pktt->node)); ZLOG_DEBUG("pktt->rx_deliver_timestamp = %d\n", g_time_elasped_in_us); }
void init_workers(int nthreads, sxe_thread_f handler) { pthread_attr_t attr; int i; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); for (i=0; i < nthreads; i++) { eq_worker_t eqw = eq_make_worker(); /* value taken from SOUND_MAX_AUDIO_FRAME_SIZE */ resize_worker_scratch(eqw, 48000*6*sizeof(uint32_t)); xthread_create( &eq_worker_thread(eqw), &attr, handler, eqw); dllist_append(workers, eqw); } pthread_attr_destroy(&attr); return; }
void ptimer_start_remainder(ptimer_table_t *table, ptimer_t *timer, u32 timeval) { u32 slot; if(timeval >= table->allslots) { timer->remainder = timeval - table->allslots + 1; slot = (table->curslot + table->allslots - 1) & (table->allslots - 1); } else { timer->remainder = 0; /* find register slot */ slot = (table->curslot + timeval) & (table->allslots - 1); } dllist_append(&table->table[slot], (dllist_node_t *)timer); // ZLOG_DEBUG("start timer remainder: 0x%p timeval=%u, curslot=%u target_slot=%u\n", timer, timeval, table->curslot, slot); }
void output_rx_throughput(packet_t *pktt) { static dllist_node_t rx_tpt_list = { .prev = &rx_tpt_list, .next = &rx_tpt_list }; if (pktt && pktt->ptt != RX_OK) return; if (pktt == NULL) { /* output the result */ while (!DLLIST_EMPTY(&rx_tpt_list)) { rx_throughput_t *ttt = (rx_throughput_t*) DLLIST_HEAD(&rx_tpt_list); ZLOG_INFO("rx tpt: at [%d, %d)s, throughput %d bps\n", ttt->time, ttt->time + 1, ttt->throughput /* bps */); dllist_remove(&rx_tpt_list, &(ttt->node)); free(ttt); } return; } /* pktt != NULL, calcuate the rx *delivery* throughput */ u32 remainder = pktt->rx_deliver_timestamp % (u32) MS2US(S2MS(1)); u32 integer = (pktt->rx_deliver_timestamp - remainder) / MS2US(S2MS(1)); rx_throughput_t * ttt = (rx_throughput_t*) DLLIST_TAIL(&rx_tpt_list); if (DLLIST_IS_HEAD(&rx_tpt_list, ttt) || ttt->time != integer) { /* new one */ rx_throughput_t *new_ttt = (rx_throughput_t*) malloc(sizeof(rx_throughput_t)); assert(new_ttt); new_ttt->time = integer; new_ttt->throughput = pktt->packet_size; dllist_append(&rx_tpt_list, &(new_ttt->node)); } else { /* already existed, update it */ ttt->throughput += (pktt->packet_size * OCTET); } }
/* @arg1 in pointer to the simu_paras_t struct @arg2 in pointer to the packet_t struct */ void pkt_rx_end_event(void *timer, void* arg1, void* arg2) { simu_paras_t *pspt = (simu_paras_t*) arg1; packet_t *pktt = (packet_t*) arg2; assert(pktt); ZLOG_DEBUG("rx, sn = %u\n", pktt->sequence_no); pktt->rx_end_timestamp = g_time_elasped_in_us; if (BER(pspt) == DISCARD) { /* mark this packet as corrupted */ // ZLOG_INFO("mark this packet as corrupted\n"); pktt->ptt = RX_ERR; /* add this packet to the packet list */ dllist_append(&g_sink_packet_list, &(pktt->node)); /* free the mac pdu buffer */ mac_free_pdu(NULL, pktt->mac_pdu); } else { /* 1. update this packet's statistics */ pktt->ptt = RX_OK; /* 2. call mac_process_pdu to handle this packet */ /* 3. mac pdu is freed in the mac_process_pdu */ pspt->rlc_rx.um_rx.umrx.cur_pktt = pktt; mac_process_pdu(&(pspt->rlc_rx.um_rx.umrx), pktt->mac_pdu, pktt->mac_pdu_size); } /* ZLOG_DEBUG("going to be finished!\n"); g_is_finished = FINISHED; */ FASTFREE(pspt->g_mem_ptimer_t, timer); }
/* Initialize */ void init_audio () { #if HAVE_LIBSDL_MIXER Uint16 audio_format; LDAT *ldat; int w; /* Initialize SDL_mixer library */ audio_format = MIX_DEFAULT_FORMAT; if (Mix_OpenAudio (luola_options.audio_rate, audio_format, 2, luola_options.audio_chunks) < 0) { fprintf (stderr,"Cannot open audio: %s\n", SDL_GetError ()); audio_available = 0; return; } else { audio_available = 1; Mix_QuerySpec (&luola_options.audio_rate, &audio_format, NULL); } /* Continue to the next song if it ends */ Mix_HookMusicFinished (playlist_forward); /* Load samples */ ldat = ldat_open_file(getfullpath(DATA_DIRECTORY,"sfx.ldat")); if(!ldat) { fprintf(stderr,"Can't load sound effects!"); } else { for (w = 0; w < SAMPLE_COUNT; w++) { samples[w] = Mix_LoadWAV_RW(ldat_get_item(ldat,"SFX",w),0); if (samples[w] == NULL) { fprintf (stderr,"Couldn't get SFX %d\n", w); } } } ldat_free(ldat); /* Load playlist */ playlist = NULL; { FILE *fp; char tmps[512]; char *line = NULL; fp = fopen (getfullpath (HOME_DIRECTORY, "battle.pls"), "r"); if (!fp) { fprintf (stderr,"No playlist file battle.pls\n"); return; } for (; fgets (tmps, sizeof (tmps) - 1, fp); free (line)) { line = strip_white_space (tmps); if (line == NULL || strlen (line) == 0) continue; if (line[0] == '#') continue; if(playlist) dllist_append(playlist,strdup(line)); else playlist = dllist_append(NULL,strdup(line)); } fclose (fp); playlist_original = NULL; /* Load the first song */ load_music(); } #endif }
void output_tx_throughput(packet_t *pktt) { /* unit: second, based on the pktt->tx_begin_timestamp, the pktt->tx_end_timestamp */ static dllist_node_t tx_tpt_list = { .prev = &tx_tpt_list, .next = &tx_tpt_list }; if (pktt == NULL) { /* output the result */ while (!DLLIST_EMPTY(&tx_tpt_list)) { tx_throughput_t *ttt = (tx_throughput_t*) DLLIST_HEAD(&tx_tpt_list); ZLOG_INFO("tx tpt: at [%d, %d)s, throughput %d bps\n", ttt->time, ttt->time + 1, ttt->throughput /* bps */); dllist_remove(&tx_tpt_list, &(ttt->node)); free(ttt); } return; } /* pktt != NULL, calcuate the tx throughput */ u32 remainder_begin = pktt->tx_begin_timestamp % (u32) MS2US(S2MS(1)); u32 remainder_end = pktt->tx_end_timestamp % (u32) (MS2US(S2MS(1))); u32 integer_begin = (pktt->tx_begin_timestamp - remainder_begin) / MS2US(S2MS(1)); u32 integer_end = (pktt->tx_end_timestamp - remainder_end) / MS2US(S2MS(1)); tx_throughput_t * ttt = (tx_throughput_t*) DLLIST_TAIL(&tx_tpt_list); if ( integer_begin == integer_end ) { /* in the same time range */ /* if have, get it */ if (DLLIST_IS_HEAD(&tx_tpt_list, ttt) || ttt->time != integer_begin) { /* new one */ tx_throughput_t *new_ttt = (tx_throughput_t*) malloc(sizeof(tx_throughput_t)); assert(new_ttt); new_ttt->time = integer_begin; new_ttt->throughput = pktt->mac_pdu_size * OCTET; dllist_append(&tx_tpt_list, &(new_ttt->node)); } else { /* already existed, update it */ ttt->throughput += (pktt->mac_pdu_size * OCTET); } } else { /* not in the same time range, split it based on the pktt->mac_pdu_size */ assert(integer_end - integer_begin == 1); u32 total = pktt->tx_end_timestamp - pktt->tx_begin_timestamp; if (DLLIST_IS_HEAD(&tx_tpt_list, ttt)) { ttt = (tx_throughput_t*) malloc(sizeof(tx_throughput_t)); dllist_append(&tx_tpt_list, &(ttt->node)); } u32 part = (pktt->mac_pdu_size * OCTET) * remainder_end / total; tx_throughput_t *new_ttt = (tx_throughput_t*) malloc(sizeof(tx_throughput_t)); assert(new_ttt); new_ttt->time = integer_end; new_ttt->throughput = (pktt->mac_pdu_size * OCTET) * remainder_end / total; dllist_append(&tx_tpt_list, &(new_ttt->node)); ttt->time = integer_begin; ttt->throughput += (pktt->mac_pdu_size * OCTET) * (1 - remainder_end / total); ZLOG_DEBUG("begin %d, remainder %d, tpt: %d, end %d, remainder %d, tpt: %d\n", integer_begin, remainder_begin, pktt->mac_pdu_size * OCTET - part, integer_end, remainder_end, part); } }