/* * pgstrom_program_cache_reclaim * * it tries to reclaim the shared memory if highly memory presure. */ static bool pgstrom_program_cache_reclaim(int shift_min) { program_cache_entry *entry; while (!dlist_is_empty(&pgcache_head->lru_list)) { dlist_node *dnode = dlist_tail_node(&pgcache_head->lru_list); int shift; entry = dlist_container(program_cache_entry, lru_chain, dnode); PGCACHE_CHECK_ACTIVE(entry); /* remove from the list not to be reclaimed again */ dlist_delete(&entry->hash_chain); dlist_delete(&entry->lru_chain); memset(&entry->hash_chain, 0, sizeof(dlist_node)); memset(&entry->lru_chain, 0, sizeof(dlist_node)); if (--entry->refcnt == 0) { pgstrom_program_cache_free(entry); /* check whether the required size is allocatable */ for (shift = shift_min; shift <= PGCACHE_MAX_BITS; shift++) { if (!dlist_is_empty(&pgcache_head->free_list[shift])) return true; } } } return false; }
void dlist_destroy(kdlist_t* dlist) { kdlist_node_t* node = 0; kdlist_node_t* temp = 0; verify(dlist); dlist_for_each_safe(dlist, node, temp) { dlist_delete(dlist, node); }
Ret packet_transfer_reset(PacketTransfer *thiz) { return_val_if_fail(thiz != NULL, RET_INVALID_PARAMS); int i; int j; int size; /* 删除命令集合中的所有命令 */ for (i = 0; i < NODE_NUM; i++) { size = dlist_length(thiz->command_set[i]); for (j = 0; i < size; j++) { dlist_delete(thiz->command_set[i], j); } } /* 重置回应 */ memset(&thiz->response, 0, sizeof(thiz->response)); /* 关闭与上位机连接 */ if (thiz->uploader_socket != -1) { close(thiz->uploader_socket); } thiz->uploader_socket = -1; return RET_OK; }
int main() { struct DList* list = dlist_create(); dlist_print(list); printf("list length = %d\n", dlist_length(list)); dlist_append(list, 5); printf("list length = %d\n", dlist_length(list)); dlist_append(list, 7); printf("list length = %d\n", dlist_length(list)); dlist_append(list, 8); printf("list length = %d\n", dlist_length(list)); dlist_print(list); dlist_print_reverse(list); dlist_remove(list, 1); printf("list length = %d\n", dlist_length(list)); dlist_print(list); dlist_print_reverse(list); dlist_insert(list, 0, 5); printf("list length = %d\n", dlist_length(list)); dlist_print(list); dlist_print_reverse(list); dlist_delete(list); return 0; }
static int dlist_int_test() { printf("double list test ... MAX=%d\n", MAX); int arr[MAX]; int i; int sum = 0; int max = INT_MIN; printf("MAX = %d\n", MAX); struct dlist *list = dlist_init(); if (list) { /* * init arr */ for (i = 0; i < MAX; i++) { arr[i] = i; } /* * dlist_add/dlist_length test */ for (i = 0; i < MAX; i++) { dlist_add(list, arr + i); assert(dlist_length(list) == i + 1); } /* * dlist_serch test */ for (i = 0; i < MAX; i++) { assert(dlist_search(list, arr + i) == DLIST_RET_OK); } /* * dlist_printf test */ assert(dlist_printf(list, user_printf) == DLIST_RET_OK); assert(dlist_foreach(list, sum_cb, &sum) == DLIST_RET_OK); assert(dlist_foreach(list, max_cb, &max) == DLIST_RET_OK); /* * dlist_delete test */ for (i = MAX - 1; i >= 0; i--) { assert(dlist_length(list) == i + 1); assert(dlist_delete(list, arr + i) == DLIST_RET_OK); assert(dlist_length(list) == i); } /* * dlist_destroy test */ assert(dlist_destroy(list) == DLIST_RET_OK); } printf("sum = %d\n", sum); printf("max = %d\n", max); return 0; }
static void pgstrom_program_cache_free(program_cache_entry *entry) { int shift = entry->shift; Size offset; Assert(entry->refcnt == 0); Assert(!entry->hash_chain.next && !entry->hash_chain.prev); Assert(!entry->lru_chain.next && !entry->lru_chain.prev); offset = (uintptr_t)entry - (uintptr_t)pgcache_head->entry_begin; Assert((offset & ((1UL << shift) - 1)) == 0); /* try to merge buddy entry, if it is also free */ while (shift < PGCACHE_MAX_BITS) { program_cache_entry *buddy; offset = (uintptr_t) entry - (uintptr_t)pgcache_head->entry_begin; if ((offset & (1UL << shift)) == 0) buddy = (program_cache_entry *)((char *)entry + (1UL << shift)); else buddy = (program_cache_entry *)((char *)entry - (1UL << shift)); if (buddy >= pgcache_head->entry_end || /* out of range? */ buddy->shift != shift || /* same size? */ PGCACHE_ACTIVE_ENTRY(buddy)) /* and free entry? */ break; #ifdef NOT_USED /* Sanity check - buddy should be in free list */ do { dlist_head *free_list = pgcache_head->free_list + shift; dlist_iter iter; bool found = false; dlist_foreach (iter, free_list) { program_cache_entry *temp = dlist_container(program_cache_entry, hash_chain, iter.cur); if (temp == buddy) { found = true; break; } } Assert(found); } while(0); #endif /* OK, chunk and buddy can be merged */ PGCACHE_CHECK_FREE(buddy); dlist_delete(&buddy->hash_chain); /* remove from free_list */ memset(&buddy->hash_chain, 0, sizeof(dlist_node)); if (buddy < entry) entry = buddy; entry->shift = ++shift; PGCACHE_MAGIC_CODE(entry) = PGCACHE_MAGIC; }
static void test_int_dlist(void) { int i = 0; int n = 100; int data = 0; DList* dlist = dlist_create(NULL, NULL, NULL); for(i = 0; i < n; i++) { assert(dlist_append(dlist, (void*)i) == RET_OK); assert(dlist_length(dlist) == (i + 1)); assert(dlist_get_by_index(dlist, i, (void**)&data) == RET_OK); assert(data == i); assert(dlist_set_by_index(dlist, i, (void*)(2*i)) == RET_OK); assert(dlist_get_by_index(dlist, i, (void**)&data) == RET_OK); assert(data == 2*i); assert(dlist_set_by_index(dlist, i, (void*)i) == RET_OK); assert(dlist_find(dlist, cmp_int, (void*)i) == i); } for(i = 0; i < n; i++) { assert(dlist_get_by_index(dlist, 0, (void**)&data) == RET_OK); assert(data == (i)); assert(dlist_length(dlist) == (n-i)); assert(dlist_delete(dlist, 0) == RET_OK); assert(dlist_length(dlist) == (n-i-1)); if((i + 1) < n) { assert(dlist_get_by_index(dlist, 0, (void**)&data) == RET_OK); assert((int)data == (i+1)); } } assert(dlist_length(dlist) == 0); for(i = 0; i < n; i++) { assert(dlist_prepend(dlist, (void*)i) == RET_OK); assert(dlist_length(dlist) == (i + 1)); assert(dlist_get_by_index(dlist, 0, (void**)&data) == RET_OK); assert(data == i); assert(dlist_set_by_index(dlist, 0, (void*)(2*i)) == RET_OK); assert(dlist_get_by_index(dlist, 0, (void**)&data) == RET_OK); assert(data == 2*i); assert(dlist_set_by_index(dlist, 0, (void*)i) == RET_OK); } i = n - 1; assert(dlist_foreach(dlist, check_and_dec_int, &i) == RET_OK); dlist_destroy(dlist); return; }
static void* consumer(void* param) { int i = 0; DList* dlist = (DList*)param; for (i = 0; i < 3 * NR; i++) { usleep(20); assert(dlist_delete(dlist, 0) == RET_OK); } return NULL; }
static void* consumer(void* param) { int i = 0; DList* dlist = (DList*)param; usleep(200); for(i = 0; i < 200; i++) { usleep(20); assert(dlist_delete(dlist, 0) == RET_OK); printf("%s: consumer one member (%d)\n", __func__, i); } return NULL; }
static void test_invalid_params(void) { printf("===========Warning is normal begin==============\n"); assert(dlist_length(NULL) == 0); assert(dlist_prepend(NULL, 0) == RET_INVALID_PARAMS); assert(dlist_append(NULL, 0) == RET_INVALID_PARAMS); assert(dlist_delete(NULL, 0) == RET_INVALID_PARAMS); assert(dlist_insert(NULL, 0, 0) == RET_INVALID_PARAMS); assert(dlist_set_by_index(NULL, 0, 0) == RET_INVALID_PARAMS); assert(dlist_get_by_index(NULL, 0, NULL) == RET_INVALID_PARAMS); assert(dlist_find(NULL, NULL, NULL) < 0); assert(dlist_foreach(NULL, NULL, NULL) == RET_INVALID_PARAMS); printf("===========Warning is normal end==============\n"); return; }
static void handle_response(PacketTransfer *thiz) { /* 能够响应上位机命令,则点亮节点对应的红灯 */ if (thiz->response.type == NODE_PAYLOAD_TYPE_RESP_NODE_ID) { gather_board_led_red(thiz->gather_board, thiz->response.port_id + 1, LED_ON); } /* 节点上报数据包,则节点对应的绿灯闪烁一次 */ gather_board_led_green(thiz->gather_board, thiz->response.port_id + 1, LED_ON); /* 在命令集合中找到回应对应的命令项并删除 */ DList *command_list = thiz->command_set[thiz->response.port_id]; int command_type = node_type_convert_resp_to_cmd(thiz->response.type); int target_index = dlist_find(command_list, response_find_cb, &command_type); dlist_delete(command_list, target_index); }
Ret hash_table_delete(HashTable* thiz, DataCompareFunc cmp, void* data) { int index = 0; DList* dlist = NULL; return_val_if_fail(thiz != NULL && cmp != NULL, RET_INVALID_PARAMS); index = thiz->hash(data)%thiz->slot_nr; dlist = thiz->slots[index]; if(dlist != NULL) { index = dlist_find(dlist, cmp, data); return dlist_delete(dlist, index); } return RET_FAIL; }
int8_t attack_th_exit(struct attacks *attacks) { write_log(0," attack_th_exit -> attack_th.stop=%d attack_th.id=%d....\n",attacks->attack_th.stop, attacks->attack_th.id); if (attacks->attack_th.stop == 0) attacks->attack_th.id = 0; else attacks->attack_th.stop = 0; if (attacks->helper_th.id) { write_log(0," attack_th_exit: %d thread_destroy helper %d...\n", (int)pthread_self(), (int)attacks->helper_th.id); thread_destroy(&attacks->helper_th); } pthread_mutex_destroy(&attacks->helper_th.finished); if (attacks->data) { free(attacks->data); } if (attacks->params) { attack_free_params(attacks->params, attacks->nparams); free(attacks->params); } attacks->data = NULL; attacks->params = NULL; attacks->up = 0; dlist_delete(attacks->used_ints->list); if (attacks->used_ints) free(attacks->used_ints); write_log(0, " attack_th_exit: %d finished\n", (int) pthread_self()); return 0; }
int main(int argc, char* argv[]) { int i = 0; int num = 10; dlist* head = dlist_creat(); #ifdef TEST //#ifdef DLIST_CREAT_ARRAY //dlist_creat_array(); //#endif /*DLIST_CREAT_ARRAY*/ for (i = 0; i < num; i++) { /*dlist_ret ret = dlist_append(head, (void*)i); assert(ret == DLIST_RET_OK);*/ assert(dlist_append(head, (void*)i) == DLIST_RET_OK); /*dlist_append(head, (void*)i);*/ } /*for () { assert(dlist_prepend() == DLIST_RET_OK); } */ //dlist_insert(); // dlist_delete(); dlist_delete(head, 2); dlist_insert(head, 2 ,(void*)11); dlist_print(head, print_int); dlist_destroy(head); #endif /*TEST*/ }
int test_dlist() { puts("##########################################"); puts("starting double linked list tests"); puts("##########################################"); int value = 0; struct DList *dlist = dlist_create(); puts("empty double list created"); if (dlist_length(dlist) != 0) { printf("dlist_length of empty list should be zero\n"); return 0; } puts("dlist_length ok"); // Insert value 101 and test functions dlist_insert(dlist, 0, 101); if (dlist_length(dlist) != 1) { printf("dlist_length should be 1\n"); return 0; } if (dlist_get(dlist, 0, &value) == 0) { printf("Error in dlist_get (1)\n"); return 0; } if (value != 101) { printf("dlist_get should return value 101\n"); return 0; } // Insert value 202 and test functions dlist_insert(dlist, 0, 202); if (dlist_length(dlist) != 2) { printf("dlist_length should return 2\n"); return 0; } if (dlist_get(dlist, 0, &value) == 0) { printf("Error in dlist_length (2)\n"); return 0; } if (value != 202) { printf("dlist_get should return 202\n"); return 0; } puts("dlist_get ok"); // Test remove function if (dlist_remove(dlist, 1) == 0) { printf("Error in dlist_remove\n"); return 0; } if (dlist_length(dlist) != 1) { printf("dlist_length should return 1 (after remove)\n"); return 0; } if (dlist_remove(dlist, 1) != 0) { printf("Error in dlist_remove\n"); return 0; } if (dlist_length(dlist) != 1) { printf("dlist_length should return 1 (after remove)\n"); return 0; } if (dlist_remove(dlist, 0) == 0) { printf("Error in dlist_remove\n"); return 0; } if (dlist_length(dlist) != 0) { printf("dlist_length should return 0 (after remove)\n"); return 0; } if (dlist_remove(dlist, 0) != 0) { printf("Error in dlist_remove\n"); return 0; } if (dlist_length(dlist) != 0) { printf("dlist_length should return 0 (after remove)\n"); return 0; } puts("dlist_remove ok"); // test dlist_append() dlist_append(dlist, -5); dlist_append(dlist, 1); dlist_append(dlist, 15); if (dlist_length(dlist) != 3) { printf("dlist_length should return 0\n"); return 0; } if (dlist_get(dlist, 0, &value) != 1) { printf("Error in dlist_append\n"); return 0; } if (value != -5) { printf("dlist_get should return -5\n"); return 0; } if (dlist_get(dlist, 1, &value) != 1) { printf("Error in dlist_append\n"); return 0; } if (value != 1) { printf("dlist_get should return 1\n"); return 0; } if (dlist_get(dlist, 2, &value) != 1) { printf("Error in dlist_append\n"); return 0; } if (value != 15) { printf("dlist_get should return 15\n"); return 0; } puts("dlist_append ok"); // test dlist insert dlist_insert(dlist, -5, 0); if (dlist_length(dlist) != 4) { printf("dlist_length should return 4\n"); return 0; } if (dlist_get(dlist, 0, &value) != 1) { printf("Error in dlist_append\n"); return 0; } if (value != 0) { printf("dlist_get should return 0\n"); return 0; } dlist_insert(dlist, 1, 100); if (dlist_length(dlist) != 5) { printf("dlist_length should return 5\n"); return 0; } if (dlist_get(dlist, 1, &value) != 1) { printf("Error in dlist_append\n"); return 0; } if (value != 100) { printf("dlist_get should return 100\n"); return 0; } dlist_insert(dlist, 10, 500); if (dlist_length(dlist) != 6) { printf("dlist_length should return 6\n"); return 0; } if (dlist_get(dlist, 5, &value) != 1) { printf("Error in dlist_append\n"); return 0; } if (value != 500) { printf("dlist_get should return 500\n"); return 0; } puts("dlist_insert ok"); // test print and print reversed puts("print current dlist"); dlist_print(dlist); puts("printing reversed dlist"); dlist_print_reverse(dlist); puts("check print and print_reversed for yourself!"); puts("##########################################"); puts("all tests of double linked lists completed"); puts("##########################################"); puts("------------------------------------------"); dlist_delete(dlist); return 1; }
Ret queue_pop(Queue* thiz) { return_val_if_fail(thiz != NULL, RET_INVALID_PARAMS); return dlist_delete(thiz->dlist, 0); }
static Ret linear_container_dlist_delete(LinearContainer* thiz, size_t index) { PrivInfo* priv = (PrivInfo*)thiz->priv; return dlist_delete(priv->dlist, index); }
int main (void) { dlist list; int i, v; dlist_init (&list, sizeof(int)); printf ("Add 10 elements\n"); for (i = 1; i <= 10; i++) { dlist_insert (&list, &i, TRUE); } printf ("Reads 5 elements from the last one:"); dlist_rewind (&list, FALSE); for (i = 1; i <= 5; i++) { dlist_read (&list, &v); printf (" %02d", v); dlist_move (&list, FALSE); } printf ("\n"); printf ("List length: %02d\n", dlist_length (&list)); printf ("Deletes the current\n"); dlist_delete (&list, TRUE); printf ("Pos from first: %02d List length: %02d\n", dlist_get_pos (&list, TRUE), dlist_length (&list)); printf ("Reads 7 elements from the first one:"); dlist_rewind (&list, TRUE); for (i = 1; i <= 7; i++) { dlist_read (&list, &v); printf (" %02d", v); dlist_move (&list, TRUE); } printf ("\n"); printf ("Adds the element 50 before current position and read: "); v = 50; dlist_insert (&list, &v, FALSE); dlist_read (&list, &v); printf ("%02d\n", v); printf ("List length: %02d\n", dlist_length (&list)); printf ("Reads all elements from the first one:"); dlist_rewind (&list, TRUE); for (;;) { dlist_read (&list, &v); printf (" %02d", v); if (dlist_get_pos (&list, FALSE) == 1) break; dlist_move (&list, TRUE); } printf ("\n"); printf ("Sort: "); dlist_sort (&list, less_than); for (;;) { dlist_read (&list, &v); printf (" %02d", v); if (dlist_get_pos (&list, FALSE) == 1) break; dlist_move (&list, TRUE); } printf ("\n"); printf ("Delete all\n"); dlist_delete_all (&list); printf ("List length: %02d\n", dlist_length (&list)); exit (0); }