void Server_queue_cleanup() { if(darray_end(SERVER_QUEUE) < SERVER_ACTIVE) { // skip it, not enough to care about return; } // pop the last one off to make sure it's never deleted Server *cur_srv = darray_pop(SERVER_QUEUE); uint32_t too_old = time(NULL) - SERVER_TTL; int i = 0; // TODO: kind of a dumb way to do this since it reorders the list // go through all but the max we want to keep for(i = 0; i < darray_end(SERVER_QUEUE) - SERVER_ACTIVE; i++) { Server *srv = darray_get(SERVER_QUEUE, i); if(srv->created_on < too_old) { Server *replace = darray_pop(SERVER_QUEUE); darray_set(SERVER_QUEUE, i, replace); srv->listen_fd = -1; // don't close it Server_destroy(srv); } } // put the sacred server back on the end darray_push(SERVER_QUEUE, cur_srv); return; }
static void dispatch_data_thread_deinit(dispatch_data_thread *ddt) { if (ddt->el) { aeDeleteEventLoop(ddt->el); ddt->el = NULL; } if (ddt->datas) { dmtqueue_destroy(ddt->datas); ddt->datas = NULL; } if (ddt->abgs) { int i, j, k; /* Deinit connection context for each server */ for (i = 0; i < darray_n(ddt->abgs); i ++) { abtest_group *abg = darray_get(ddt->abgs, i); for (j = 0; j < darray_n(&abg->abtest_servers); j ++) { abtest_server *abs = darray_get(&abg->abtest_servers, j); while (darray_n(abs->conn_contexts) > 0) { conn_context *cc = darray_pop(abs->conn_contexts); dispatch_conn_context_deinit(cc); } } } abtest_groups_destroy(ddt->abgs); ddt->abgs = NULL; } }
int master_init(vr_conf *conf) { rstatus_t status; uint32_t j; sds *host, listen_str; vr_listen **vlisten; int threads_num; int filelimit; master.cbsul = NULL; pthread_mutex_init(&master.cbsullock, NULL); conf_server_get(CONFIG_SOPN_THREADS,&threads_num); filelimit = threads_num*2+CONFIG_MIN_RESERVED_FDS; vr_eventloop_init(&master.vel,filelimit); master.vel.thread.fun_run = master_thread_run; darray_init(&master.listens,darray_n(&cserver->binds),sizeof(vr_listen*)); for (j = 0; j < darray_n(&cserver->binds); j ++) { host = darray_get(&cserver->binds,j); listen_str = sdsdup(*host); listen_str = sdscatfmt(listen_str, ":%i", cserver->port); vlisten = darray_push(&master.listens); *vlisten = vr_listen_create(listen_str); if (*vlisten == NULL) { darray_pop(&master.listens); log_error("Create listen %s failed", listen_str); sdsfree(listen_str); return VR_ERROR; } sdsfree(listen_str); } for (j = 0; j < darray_n(&master.listens); j ++) { vlisten = darray_get(&master.listens, j); status = vr_listen_begin(*vlisten); if (status != VR_OK) { log_error("Begin listen to %s failed", (*vlisten)->name); return VR_ERROR; } } master.cbsul = dlistCreate(); if (master.cbsul == NULL) { log_error("Create list failed: out of memory"); return VR_ENOMEM; } setup_master(); return VR_OK; }
void vrt_dispatch_data_deinit(void) { if (dispatch_data_threads) { while (darray_n(dispatch_data_threads) > 0) { dispatch_data_thread *ddt = darray_pop(dispatch_data_threads); dispatch_data_thread_deinit(ddt); } darray_destroy(dispatch_data_threads); dispatch_data_threads = NULL; } }
void master_deinit(void) { vr_listen **vlisten; vr_eventloop_deinit(&master.vel); while (darray_n(&master.listens) > 0) { vlisten = darray_pop(&master.listens); vr_listen_destroy(*vlisten); } darray_deinit(&master.listens); }
char *test_push_pop() { int i = 0; for(i=0; i<1000; i++) { int *val = darray_new(array); *val = i*333; darray_push(array, val); } mu_assert(array->max==1300, "Wrong max size"); for(i=999; i>=0; i--) { int *val = darray_pop(array); mu_assert(val!=NULL, "Shouldn't get a NULL."); mu_assert(*val==i*333, "Wrong value."); free(val); } return NULL; }
void *hashmap_delete(Hashmap *map, void *key) { uint32_t hash = 0; DArray *bucket = hashmap_find_bucket(map, key, 0, &hash); if (!bucket) return NULL; int i = hashmap_get_node(map, hash, bucket, key); if (i == -1) return NULL; HashmapNode *node = darray_get(bucket, i); void *data = node->data; free(node); HashmapNode *ending = darray_pop(bucket); if (ending != node) { // looks like it's not the last one, swap it darray_set(bucket, i, ending); } return data; }
int main(void) { darray(long) arr = darray_new(); darray_char str = darray_new(); darray(long*) arrp = darray_new(); arrp.onFree = _arr_free_handler; #define reset(arr) do {darray_free(arr); darray_init(arr);} while(0) size_t i; trace("Generating amalgams (internal)"); generateAmalgams(); plan_tests(54); testLits(); testing(darray_pushptr); { int vMaxCount = 10;//ARRAY_SIZE(lotsOfNumbers); for (int k=0; k < vMaxCount; k++) { long* p = malloc(sizeof(long)); *p = lotsOfNumbers[k]; darray_push(arrp, p); } ok1(darray_size(arrp) == vMaxCount); ok1(darray_alloc(arrp) >= darray_size(arrp)); long **i; size_t j = 0; darray_foreach(i, arrp) { if (i - arrp.item != j) break; if (**i != (long)lotsOfNumbers[j]) break; j++; }; ok1(j == vMaxCount); darray_free_all(arrp); ok1(_free_count == vMaxCount); } testing(darray_push); { for (i=0; i < ARRAY_SIZE(lotsOfNumbers); i++) darray_push(arr, lotsOfNumbers[i]); ok1(darray_size(arr) == ARRAY_SIZE(lotsOfNumbers)); ok1(darray_alloc(arr) >= darray_size(arr)); ok1(!memcmp(arr.item, lotsOfNumbers, sizeof(lotsOfNumbers))); } testing(darray_insert); { darray_insert(arr, 0, 123456); ok1(darray_size(arr) == ARRAY_SIZE(lotsOfNumbers)+1); ok1(!memcmp(arr.item+1, lotsOfNumbers, sizeof(lotsOfNumbers))); ok1(darray_item(arr, 0) == 123456); darray_insert(arr, 15, 0x112233); ok1(darray_size(arr) == ARRAY_SIZE(lotsOfNumbers)+2); ok1(darray_item(arr, 15) == 0x112233); ok1(!memcmp(arr.item+1, lotsOfNumbers, 14*sizeof(long))); ok1(!memcmp(arr.item+16, &lotsOfNumbers[14], ARRAY_SIZE(lotsOfNumbers)-(15*sizeof(long)))); } testing(darray_del); { darray_del(arr, 15); darray_del(arr, 0); ok1(darray_size(arr) == ARRAY_SIZE(lotsOfNumbers)); ok1(!memcmp(arr.item, lotsOfNumbers, sizeof(lotsOfNumbers))); } reset(arr); testing(darray_prepend, darray_pop); { for (i = ARRAY_SIZE(lotsOfNumbers); i;) darray_prepend(arr, lotsOfNumbers[--i]); ok1(darray_size(arr) == ARRAY_SIZE(lotsOfNumbers)); ok1(darray_alloc(arr) >= darray_size(arr)); ok1(!memcmp(arr.item, lotsOfNumbers, sizeof(lotsOfNumbers))); for (i = ARRAY_SIZE(lotsOfNumbers); i;) { if (darray_pop(arr) != (long)lotsOfNumbers[--i]) { i++; break; } } ok1(i==0); ok1(darray_size(arr) == 0); } reset(arr); testing(darray_from_c, darray_foreach, darray_foreach_reverse); { long *i; size_t j; darray_from_c(arr, lotsOfNumbers); ok1(darray_size(arr) == ARRAY_SIZE(lotsOfNumbers)); ok1(darray_alloc(arr) >= darray_size(arr)); ok1(memcmp(arr.item, lotsOfNumbers, sizeof(lotsOfNumbers)) == 0); j = 0; darray_foreach(i, arr) { if (i - arr.item != j) break; if (*i != (long)lotsOfNumbers[j]) break; j++; }; ok1(j == ARRAY_SIZE(lotsOfNumbers)); j = 0; darray_foreach_reverse(i, arr) { if (i - arr.item != darray_size(arr)-j-1) break; if (*i != (long)lotsOfNumbers[darray_size(arr)-j-1]) break; j++; }; ok1(j == ARRAY_SIZE(lotsOfNumbers)); } reset(arr); testing(darray_append_string); { for (i=0; i < ARRAY_SIZE(lotsOfStrings); i++) darray_append_string(str, lotsOfStrings[i]); ok1(str.size == amalgams.stringsSize); ok1(str.alloc > str.size); ok1(str.item[str.size] == 0); ok1(!strcmp(str.item, amalgams.stringsF)); } reset(str); testing(darray_prepend_string); { for (i=0; i < ARRAY_SIZE(lotsOfStrings); i++) darray_prepend_string(str, lotsOfStrings[i]); ok1(str.size == amalgams.stringsSize); ok1(str.alloc > str.size); ok1(str.item[str.size] == 0); ok1(!strcmp(str.item, amalgams.stringsB)); } reset(str); testing(darray_from_string); { for (i=0; i < ARRAY_SIZE(lotsOfStrings); i++) { darray_from_string(str, lotsOfStrings[i]); if (str.size != strlen(lotsOfStrings[i])) break; if (str.alloc < strlen(lotsOfStrings[i])+1) break; if (strcmp(str.item, lotsOfStrings[i])) break; } ok1(i == ARRAY_SIZE(lotsOfStrings)); } reset(str); testing(darray_resize0); { size_t prevSize=0, size; for (i=0; i < ARRAY_SIZE(lotsOfNumbers); i++, prevSize=size) { size = lotsOfNumbers[i] & 0xFFFF; darray_resize0(arr, size); if (darray_size(arr) != size) break; if (darray_alloc(arr) < size) break; if (size>prevSize) { if (!isZeros(arr.item+prevSize, (size-prevSize)*sizeof(*arr.item))) break; } //fill the darray with lotsOfNumbers garbage memtile(arr.item, darray_size(arr)*sizeof(*arr.item), lotsOfNumbers, sizeof(lotsOfNumbers)); } ok1(i == ARRAY_SIZE(lotsOfNumbers)); } reset(arr); testing(darray_realloc); { size_t s,a; for (i=0; i < ARRAY_SIZE(lotsOfNumbers); i++) { arr.size = (s = lotsOfNumbers[i] >> 16); //give size a nonsense value to make sure darray_realloc doesn't care about it a = amalgams.stringsSize/sizeof(*arr.item)+2; darray_realloc(arr, a = lotsOfNumbers[i] % ((amalgams.stringsSize/sizeof(*arr.item))+1)); if (a*sizeof(*arr.item) > amalgams.stringsSize) break; if (darray_alloc(arr) != a) break; if (darray_size(arr) != s) break; memtile(arr.item, a*sizeof(*arr.item), amalgams.stringsF, a*sizeof(*arr.item)); if (memcmp(arr.item, amalgams.stringsF, a*sizeof(*arr.item))) break; } ok1(i == ARRAY_SIZE(lotsOfNumbers)); } reset(arr); testing(darray_growalloc); { size_t prevA, s, a; for (i=0; i < ARRAY_SIZE(lotsOfNumbers); i++) { arr.size = (s = lotsOfNumbers[i] >> 16); //give size a nonsense value to make sure darray_growalloc doesn't care about it a = amalgams.stringsSize/sizeof(*arr.item)+2; prevA = darray_alloc(arr); darray_growalloc(arr, a = lotsOfNumbers[i] % ((amalgams.stringsSize/sizeof(*arr.item))+1)); if (a*sizeof(*arr.item) > amalgams.stringsSize) break; if (darray_alloc(arr) < a) break; if (darray_alloc(arr) < prevA) break; if (darray_size(arr) != s) break; memtile(arr.item, a*sizeof(*arr.item), amalgams.stringsF, a*sizeof(*arr.item)); if (memcmp(arr.item, amalgams.stringsF, a*sizeof(*arr.item))) break; //clear the darray every now and then if (!(lotsOfNumbers[i] & 15)) { reset(arr); } } ok1(i == ARRAY_SIZE(lotsOfNumbers)); } reset(arr); testing(darray_make_room); { for (i=0; i < ARRAY_SIZE(lotsOfStrings); i++) { char *dest = darray_make_room(str, strlen(lotsOfStrings[i])); if (str.alloc < str.size+strlen(lotsOfStrings[i])) break; if (dest != str.item+str.size) break; memcpy(dest, lotsOfStrings[i], strlen(lotsOfStrings[i])); str.size += strlen(lotsOfStrings[i]); } ok1(i == ARRAY_SIZE(lotsOfStrings)); ok1(str.size == amalgams.stringsSize); darray_append(str, 0); ok1(!strcmp(str.item, amalgams.stringsF)); } reset(str); testing(darray_appends, darray_prepends, darray_pop_check); { darray(const char*) arr = darray_new(); const char *n[9] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight"}; #if HAVE_TYPEOF darray_appends(arr, n[5], n[6], n[7], n[8]); #else darray_appends_t(arr, const char *, n[5], n[6], n[7], n[8]); #endif ok1(darray_size(arr)==4 && darray_alloc(arr)>=4); #if HAVE_TYPEOF darray_prepends(arr, n[0], n[1], n[2], n[3], n[4]); #else darray_prepends_t(arr, const char *, n[0], n[1], n[2], n[3], n[4]); #endif ok1(darray_size(arr)==9 && darray_alloc(arr)>=9); ok1(arr.item[0]==n[0] && arr.item[1]==n[1] && arr.item[2]==n[2] && arr.item[3]==n[3] && arr.item[4]==n[4] && arr.item[5]==n[5] && arr.item[6]==n[6] && arr.item[7]==n[7] && arr.item[8]==n[8]); ok1(darray_pop_check(arr)==n[8] && darray_pop_check(arr)==n[7] && darray_pop_check(arr)==n[6] && darray_pop_check(arr)==n[5] && darray_pop_check(arr)==n[4] && darray_pop_check(arr)==n[3] && darray_pop_check(arr)==n[2] && darray_pop_check(arr)==n[1] && darray_pop_check(arr)==n[0]); ok1(darray_size(arr)==0); ok1(darray_pop_check(arr)==NULL && darray_pop_check(arr)==NULL && darray_pop_check(arr)==NULL); darray_free(arr); } trace("Freeing amalgams (internal)"); freeAmalgams(); return exit_status(); }
MemoryAllocation* MemoryAllocation_Pop(DArray *array) { return (MemoryAllocation *)darray_pop(array); }
char *test_darray_operations() { darray_t *array = darray_create(sizeof(int), 100); mu_assert(array != NULL, "darray_create failed."); mu_assert(array->contents != NULL, "contents are wrong in darray"); mu_assert(array->end == 0, "end isn't at the right spot"); mu_assert(array->element_size == sizeof(int), "element size is wrong."); mu_assert(array->max == 100, "wrong max length on initial size"); int *val1 = darray_new(array); mu_assert(val1 != NULL, "failed to make a new element"); int *val2 = darray_new(array); mu_assert(val2 != NULL, "failed to make a new element"); darray_set(array, 0, val1); darray_set(array, 1, val2); mu_assert(darray_get(array, 0) == val1, "Wrong first value."); mu_assert(darray_get(array, 1) == val2, "Wrong second value."); int *val_check = darray_remove(array, 0); mu_assert(val_check != NULL, "Should not get NULL."); mu_assert(*val_check == *val1, "Should get the first value."); mu_assert(darray_get(array, 0) == NULL, "Should be gone."); darray_free(val_check); val_check = darray_remove(array, 1); mu_assert(val_check != NULL, "Should not get NULL."); mu_assert(*val_check == *val2, "Should get the first value."); mu_assert(darray_get(array, 1) == NULL, "Should be gone."); darray_free(val_check); int old_max = array->max; darray_expand(array); mu_assert(array->max == old_max + array->expand_rate, "Wrong size after expand."); darray_contract(array); mu_assert(array->max == array->expand_rate + 1, "Should stay at the expand_rate at least."); darray_contract(array); mu_assert(array->max == array->expand_rate + 1, "Should stay at the expand_rate at least."); int i = 0; for(i = 0; i < 1000; i++) { int *val = darray_new(array); darray_attach(array, val); *val = i * 333; darray_push(array, val); } mu_assert(array->max == 1201, "Wrong max size."); for(i = 999; i > 0; i--) { int *val = darray_pop(array); mu_assert(val != NULL, "Shouldn't get a NULL."); mu_assert(*val == i * 333, "Wrong value."); darray_free(val); } darray_destroy(array); return NULL; }
static inline void analyzer_traversal_ctx_prev_parent(struct analyzer_traversal_ctx *ctx) { RF_ASSERT(darray_size(ctx->parent_nodes) != 0, "Tried to go beyond the root"); (void)darray_pop(ctx->parent_nodes); }