int slist_append(slist_t *list,void *Value) { slist_node_t *temp_p=NULL; if (Value == NULL) { // if no have Value return 0; } temp_p=(slist_node_t *)calloc(1,sizeof(slist_node_t)); if(temp_p==NULL) { return -1; } slist_data(temp_p) = Value; slist_next(temp_p)=NULL; if (slist_head(list)==NULL) { slist_head(list) = temp_p; slist_tail(list) = temp_p; slist_size(list)++; return 0; } slist_next(slist_tail(list)) = temp_p; slist_tail(list) = temp_p; slist_size(list)++; return 0; }
int slist_prepend(slist_t *list,void *Value) { if (Value == NULL) { // if no have Value return 0; } slist_node_t *temp_p; temp_p=(slist_node_t *)calloc(1,sizeof(slist_node_t)); if(temp_p==NULL) { return -1; } slist_data(temp_p)=Value; temp_p->next = NULL; if (slist_head(list)==NULL) // the list is empty { slist_head(list) = temp_p; slist_tail(list) = temp_p; slist_next(temp_p) = NULL; slist_size(list) = 1; return 0; } slist_next(temp_p) = slist_head(list); slist_head(list) = temp_p; if (slist_tail(list)==NULL) { slist_tail(list)=temp_p; } slist_size(list)++; return 0; }
int is_game_board_empty(Game *game) { if (slist_size(game->moves) != 0 || slist_size(game->placed_black_stones) != 0 || slist_size(game->placed_white_stones) != 0) return 0; else return 1; }
void test_slist_remove_data_from_existing(void) { unsigned long *val; unsigned long old_size; old_size = slist_size(test_slist); val = NULL; val = slist_index(test_slist, 10); assert_true(val != NULL); assert_true(slist_remove_data(test_slist, val) == 0); assert_true((old_size - 1) == slist_size(test_slist)); }
void test_slist_insert_at_back(void) { unsigned long *val; unsigned long old_size; old_size = slist_size(test_slist); val = make_ulong_ptr(7777); assert_true(slist_insert(test_slist, old_size, val) == 0); /* Verify */ val = NULL; val = slist_index(test_slist, old_size); assert_ulong_equal(7777, *val); assert_true((old_size + 1) == slist_size(test_slist)); }
void test_slist_insert_at_front(void) { unsigned long *val; unsigned long old_size; old_size = slist_size(test_slist); val = make_ulong_ptr(8888); assert_true(slist_insert(test_slist, 0, val) == 0); /* Verify */ val = NULL; val = slist_index(test_slist, 0); assert_ulong_equal(8888, *val); assert_true((old_size + 1) == slist_size(test_slist)); }
void test_slist_insert_in_middle(void) { unsigned long *val; unsigned long old_size; old_size = slist_size(test_slist); val = make_ulong_ptr(6666); assert_true(slist_insert(test_slist, old_size / 2, val) == 0); /* Verify */ val = NULL; val = slist_index(test_slist, old_size / 2); assert_ulong_equal(6666, *val); assert_true((old_size + 1) == slist_size(test_slist)); }
void test_slist_prepend_to_existing(void) { unsigned long *val; unsigned long old_size; old_size = slist_size(test_slist); val = make_ulong_ptr(7777); assert_true(slist_prepend(test_slist, val) == 0); /* Verify */ val = NULL; val = slist_index(test_slist, 0); assert_ulong_equal(7777, *val); assert_true((old_size + 1) == slist_size(test_slist)); }
void test_slist_remove_index_from_existing(void) { unsigned long *val; unsigned long old_size; old_size = slist_size(test_slist); /* Verify */ val = NULL; val = slist_remove_index(test_slist, 0); assert_ulong_equal(0, *val); assert_true((old_size - 1) == slist_size(test_slist)); free(val); }
inline static int calc_whoosh_err(struct slist *list, struct mq_stats_msg *m, int decim8) { uint32_t whoosh_sum = 0, whoosh_max = 0, whoosh_sum2 = 0; struct slist *ln; int size = slist_size(list); ln = slist_idx(list, size - decim8); for (int i = decim8; i > 0; i--) { struct sample *s = ln->s; whoosh_sum += s->whoosh_error_ns; whoosh_sum2 += (s->whoosh_error_ns * s->whoosh_error_ns); whoosh_max = (whoosh_max > s->whoosh_error_ns) ? whoosh_max : s->whoosh_error_ns; ln = ln->next; } m->max_whoosh = whoosh_max; m->mean_whoosh = (uint64_t)ceill((double)whoosh_sum / (double)decim8); double variance = (long double)whoosh_sum2 / (double)decim8; m->sd_whoosh = (uint64_t)ceill(sqrtl(variance)); if ((decim8 == decs[DECIMATIONS_COUNT - 1]) && ((whoosh_max >= 0.1 * m->interval_ns) || (m->sd_whoosh >= m->interval_ns))) { syslog(LOG_INFO, "sampling jitter! mean: %10" PRId32 " max: %10" PRId32 " sd: %10" PRId32 "\n", m->mean_whoosh, m->max_whoosh, m->sd_whoosh); } return 0; }
void test_slist_remove_data_from_existing_until_empty(void) { unsigned long *val; unsigned long i; val = NULL; i = slist_size(test_slist); while(i > 0) { val = slist_index(test_slist, --i); assert_true(val != NULL); assert_true(slist_remove_data(test_slist, val) == 0); assert_true(i == slist_size(test_slist)); } assert_true(slist_is_empty(test_slist)); }
int slist_insert_next(singly_list *list, singly_list_element *element, const void *data) { singly_list_element *new_element; /* Allocate memory for new element */ if((new_element = (singly_list_element *)malloc(sizeof(singly_list_element))) == NULL) return -1; /* Insert element */ new_element->data = (void *)data; if(element == NULL) { /* Insert at head */ if(slist_size(list) == 0) list->tail = new_element; new_element->next = list->head; list->head = new_element; } else { /* Insert not at head */ if(element->next == NULL) list->tail = new_element; new_element->next = element->next; element->next = new_element; } /* Update list size */ list->size++; return 0; }
void test_slist_prepend_to_empty(void) { unsigned long *val = NULL; assert_true(test_slist == NULL); test_slist = slist_create(); assert_true(test_slist != NULL); assert_true(slist_is_empty(test_slist)); val = make_ulong_ptr(9999); assert_true(val != NULL); assert_true(slist_prepend(test_slist, val) == 0); /* Verify */ val = NULL; val = slist_index(test_slist, 0); assert_true(val != NULL); assert_ulong_equal(9999, *val); assert_true(slist_size(test_slist) == 1); slist_free_all(test_slist, NULL); test_slist = NULL; }
/** * Reset the size of slist elements, and filled element is from variable argument slist. */ void _slist_resize_elem_varg(slist_t* pslist_slist, size_t t_resize, va_list val_elemlist) { assert(pslist_slist != NULL); assert(_slist_is_inited(pslist_slist)); if (t_resize <= slist_size(pslist_slist)) { slist_iterator_t it_pos = iterator_advance(slist_begin(pslist_slist), t_resize); slist_erase_range(pslist_slist, it_pos, slist_end(pslist_slist)); } else { slist_iterator_t it_pos; _slistnode_t* pt_node = NULL; _slistnode_t* pt_varg = NULL; size_t t_size = slist_size(pslist_slist); size_t i = 0; bool_t b_result = false; if (!slist_empty(pslist_slist)) { it_pos = slist_previous(pslist_slist, slist_end(pslist_slist)); } else { _SLIST_ITERATOR_COREPOS(it_pos) = (_byte_t*)&pslist_slist->_t_head; } /* get varg value only once */ pt_varg = _alloc_allocate(&pslist_slist->_t_allocator, _SLIST_NODE_SIZE(_GET_SLIST_TYPE_SIZE(pslist_slist)), 1); assert(pt_varg != NULL); _slist_get_varg_value_auxiliary(pslist_slist, val_elemlist, pt_varg); for (i = 0; i < t_resize - t_size; ++i) { pt_node = _alloc_allocate(&pslist_slist->_t_allocator, _SLIST_NODE_SIZE(_GET_SLIST_TYPE_SIZE(pslist_slist)), 1); assert(pt_node != NULL); _slist_init_node_auxiliary(pslist_slist, pt_node); /* copy value from varg */ b_result = _GET_SLIST_TYPE_SIZE(pslist_slist); _GET_SLIST_TYPE_COPY_FUNCTION(pslist_slist)(pt_node->_pby_data, pt_varg->_pby_data, &b_result); assert(b_result); pt_node->_pt_next = ((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_pos))->_pt_next; ((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_pos))->_pt_next = pt_node; pt_node = NULL; } _slist_destroy_varg_value_auxiliary(pslist_slist, pt_varg); _alloc_deallocate(&pslist_slist->_t_allocator, pt_varg, _SLIST_NODE_SIZE(_GET_SLIST_TYPE_SIZE(pslist_slist)), 1); } }
void test_slist_reverse_existing(void) { unsigned long *val; unsigned long i, val_check; val = NULL; val_check = *(unsigned long *)slist_index(test_slist, slist_size(test_slist) - 1); assert_true(slist_reverse(test_slist) == 0); for(i = 0; i < slist_size(test_slist); i++, val_check--) { val = slist_index(test_slist, i); assert_true(val != NULL); assert_ulong_equal(*val, val_check); } }
inline static int calc_txrx_minmaxmean(struct slist *list, struct mq_stats_msg *m, int decim8) { uint64_t rxb_sum = 0, txb_sum = 0, rxp_sum = 0, txp_sum = 0; uint64_t rxb_max = 0, txb_max = 0; uint32_t rxp_max = 0, txp_max = 0; uint64_t rxb_min = UINT64_MAX, txb_min = UINT64_MAX; uint32_t rxp_min = UINT32_MAX, txp_min = UINT32_MAX; struct slist *ln; int size = slist_size(list); ln = slist_idx(list, size - decim8); for (int i = decim8; i > 0; i--) { struct sample *s = ln->s; rxb_sum += s->rx_bytes_delta; txb_sum += s->tx_bytes_delta; rxp_sum += s->rx_packets_delta; txp_sum += s->tx_packets_delta; rxb_max = (rxb_max > s->rx_bytes_delta) ? rxb_max : s->rx_bytes_delta; txb_max = (txb_max > s->tx_bytes_delta) ? txb_max : s->tx_bytes_delta; rxp_max = (rxp_max > s->rx_packets_delta) ? rxp_max : s->rx_packets_delta; txp_max = (txp_max > s->tx_packets_delta) ? txp_max : s->tx_packets_delta; rxb_min = (rxb_min < s->rx_bytes_delta) ? rxb_min : s->rx_bytes_delta; txb_min = (txb_min < s->tx_bytes_delta) ? txb_min : s->tx_bytes_delta; rxp_min = (rxp_min < s->rx_packets_delta) ? rxp_min : s->rx_packets_delta; txp_min = (txp_min < s->tx_packets_delta) ? txp_min : s->tx_packets_delta; ln = ln->next; } m->min_rx_bytes = rxb_min; m->max_rx_bytes = rxb_max; m->mean_rx_bytes = 1000 * rxb_sum / decim8; m->min_tx_bytes = txb_min; m->max_tx_bytes = txb_max; m->mean_tx_bytes = 1000 * txb_sum / decim8; m->min_rx_packets = rxp_min; m->max_rx_packets = rxp_max; m->mean_rx_packets = 1000 * rxp_sum / decim8; m->min_tx_packets = txp_min; m->max_tx_packets = txp_max; m->mean_tx_packets = 1000 * txp_sum / decim8; return 0; }
int main(int argc, char *argv[]) { struct slist *list; list = slist_new(); assert(list); assert(0 == slist_size(list)); assert(list == slist_idx(list, 0)); assert(NULL == slist_idx(list, -1)); assert(NULL == slist_idx(list, 1)); /* Build an array and set canary values */ struct slist *nodes1[NODECNT]; for (int i = 0; i < NODECNT; i++) { nodes1[i] = malloc(sizeof(struct slist)); nodes1[i]->s = malloc(sizeof(struct sample)); nodes1[i]->s->rx_bytes = i; } /* Then add array elements to a list */ for (int i = 0; i < NODECNT; i++) { slist_push(list, nodes1[i]); assert(1 + i == slist_size(list)); } /* check slist_size */ assert(NODECNT == slist_size(list)); /* check the list_idx function */ for (int i = 0; i < NODECNT; i++) { struct slist *n = slist_idx(list, i); assert(n); assert(n->s->rx_bytes == i); } /* check slist_clear */ slist_clear(list); assert(0 == slist_size(list)); }
/* Complexity: O(1) */ void* queue_back(Queue *queue) { assert(queue != NULL); if(slist_is_empty((SList *)queue)) { return NULL; } return slist_index((SList *)queue, slist_size((SList *)queue) - 1); }
void test_slist_create(void) { test_slist = slist_create(); assert_true(test_slist != NULL); assert_true(slist_size(test_slist) == 0); assert_true(slist_is_empty(test_slist)); slist_free(test_slist); test_slist = NULL; }
int test_base_slist(const char* param) { struct slist_t* sl = slist_create(); if (!sl) { fprintf(stderr, "slist create fail\n"); return -1; } rand_seed(time(NULL)); int loop = param ? atoi(param) : 32; int data[loop]; for (int i = 0; i < loop; ++ i) { data[i] = (int)(rand_gen()) % loop; int res = slist_push_front(sl, &data[i]); CHECK(sl, res == 0, "slist push front fail"); res = slist_push_back(sl, &data[i]); CHECK(sl, res == 0, "slist push back fail"); } CHECK(sl, slist_size(sl) == 2 * loop, "slist size fail"); for (int i = loop - 1; i >= 0; -- i) { int res = slist_find(sl, &data[i]); CHECK(sl, res == 0, "slist find fail"); void* p = slist_pop_front(sl); CHECK(sl, p == &data[i], "slist pop front fail"); p = slist_pop_back(sl); CHECK(sl, p == &data[i], "slist pop back fail"); res = slist_find(sl, &data[i]); CHECK(sl, res < 0, "slist find fail"); } CHECK(sl, slist_size(sl) == 0, "slist size fail"); slist_release(sl); return 0; }
int slist_append_list(slist_t* listOne, slist_t* listSec) { if (slist_tail(listSec) == NULL && slist_head(listOne) == NULL) { // both of list is NULL return 0; } if (slist_head(listSec) == NULL) { // list two is NULL return 0; } if(!listOne || !listSec) {return -1;} if(slist_size(listSec) == 0) {return 0;} slist_node_t* p = slist_head(listSec); while(p) { if( slist_append(listOne, p->data) == -1 ) {return -1;} p = slist_next(p); } slist_size(listOne) += slist_size(listSec); /* slist_destroy(l2, SLIST_LEAVE_DATA);*/ return 0; }
static int frames_to_sample_list() { int new_samples = 0; pthread_mutex_lock(&unsent_frame_count_mutex); while (g_unsent_frame_count > 0) { for (int i = 0; i < SAMPLES_PER_FRAME; i++) { struct sample *s = malloc(sizeof(struct sample)); assert(s); memcpy(s, &(g_raw_samples->samples[i]), sizeof(struct sample)); struct slist *ln = malloc(sizeof(struct slist)); assert(ln); ln->s = s; slist_push(sample_list, ln); g_sample_count++; new_samples++; } g_unsent_frame_count--; } pthread_mutex_unlock(&unsent_frame_count_mutex); int overflow = (slist_size(sample_list) > MAX_LIST_LEN) ? slist_size(sample_list) - MAX_LIST_LEN : 0; while (overflow--) { struct slist *ln = slist_pop(sample_list); assert(ln); assert(ln->s); free(ln->s); free(ln); } if (g_sample_count > MAX_LIST_LEN) { g_sample_count -= MAX_LIST_LEN; } return new_samples; }
void *slist_pop_first(slist_t *list) { if (slist_head(list)==NULL) { return NULL; } slist_node_t *temp_p=NULL; void *PopValue; temp_p = slist_head(list); PopValue= slist_data(temp_p); slist_head(list) =slist_next(temp_p); free(temp_p); int countSize = slist_size(list) -1; if (countSize==0) // if the list have one node { slist_tail(list)=NULL; slist_head(list)=NULL; slist_size(list)=0; } return(PopValue); // return the Value of head list }
inline static struct minmaxmean calc_min_max_mean_gap(struct slist *list, int decim8, int rxtx) { struct slist *ln; int32_t size = slist_size(list); int32_t gap_lengths[MAX_LIST_LEN] = { 0 }; int32_t gap_idx = 0; int found_gap = 0; int32_t min_gap = decim8 + 1; int32_t max_gap = 0; int32_t sum_gap = 0; int32_t mean_gap = 0; assert(size >= decim8); assert(decim8 > 0); ln = slist_idx(list, size - decim8); for (int i = decim8; i > 0; i--) { struct sample *s = ln->s; if (((RX == rxtx) && (0 == s->rx_packets_delta)) || ((TX == rxtx) && (0 == s->tx_packets_delta))) { found_gap = 1; gap_lengths[gap_idx]++; max_gap = (max_gap > gap_lengths[gap_idx]) ? max_gap : gap_lengths[gap_idx]; } else if (found_gap) { found_gap = 0; gap_idx++; } ln = ln->next; } assert(gap_idx <= decim8); assert(max_gap <= decim8); for (int i = 0; i <= gap_idx; i++) { sum_gap += gap_lengths[i]; if (min_gap > gap_lengths[i]) { min_gap = gap_lengths[i]; } } assert(sum_gap <= decim8); assert((min_gap <= decim8) || (sum_gap == 0)); /* gap is the last index into the gap_lengths, so +1 for count. */ mean_gap = roundl((1000.0 * sum_gap) / (gap_idx + 1)); assert(1000 * min_gap <= mean_gap); return (struct minmaxmean){min_gap, max_gap, mean_gap}; }
void print(slist_t *list) { if(slist_size(list) == 0) return; slist_node_t *p = slist_head(list); while( p != NULL) { pm_labeled_edge_t* edge = (pm_labeled_edge_t*)slist_data(p); pm_state_t* nextState = edge->state; if (slist_size(nextState->_transitions) == 0) printf("s: %d , l: %c -> " , nextState->id , edge->label); else { printf("s: %d , l: %c -> " , nextState->id , edge->label); print(nextState->_transitions); } p = slist_next(p); } }
int slist_remove_next(singly_list *list, singly_list_element *element, void **data) { singly_list_element *old_element; /* Avoid remove from empty list */ if(slist_size(list) == 0) return -1; /* Remove element */ if(element == NULL) { /* Remove from head */ *data = list->head->data; old_element = list->head; list->head = list->head->next; if(slist_size(list) == 1) list->tail = NULL; } else { /* Remove not from head */ if(element->next == NULL) return -1; *data = element->next->data; old_element = element->next; element->next = element->next->next; if(element->next == NULL) list->tail = element; } /* Free old element storage */ free(old_element); /* Update list size */ list->size--; return 0; }
static curlc_t* _curlp_client_alloc(curlp_t* cp) { if (!cp || !cp->free_list) return NULL; if (slist_size(cp->free_list) > 0) { curlc_t* cc = slist_pop_front(cp->free_list); return cc; } cp->size ++; curlc_t* cc = curlc_create(); if (cc) { hash_insert(cp->clients, cc); } return cc; }
void test_slist_index(void) { unsigned long *val; /* Index the front */ val = slist_index(test_slist, 0); assert_ulong_equal(0, *val); /* Index the back */ val = slist_index(test_slist, slist_size(test_slist) - 1); assert_ulong_equal(999, *val); /* Index some random place */ val = slist_index(test_slist, 47); assert_ulong_equal(47, *val); }
void slist_destroy(singly_list *list) { void *data; /* Remove elements */ while(slist_size(list) > 0) { if(slist_remove_next(list, NULL, (void **)&data) == 0 && list->destroy != NULL) { /* Call function for free allocated data */ list->destroy(data); } } /* Clear structure */ memset(list, 0, sizeof(list)); return; }
void slist_destroy(slist_t *list_p ,slist_destroy_t flag) { if (list_p==NULL) { return; } slist_node_t *temp_p; temp_p = slist_head(list_p); while(temp_p!= NULL) { slist_head(list_p) = slist_next(temp_p); if (flag==SLIST_FREE_DATA) { free(slist_data(temp_p)); } free(temp_p); temp_p = slist_head(list_p); } free(temp_p); slist_size(list_p) = 0; return; }