char *test_new() { val1 = darray_new(array); mu_assert(val1!=NULL, "failed to make a new element"); val2 = darray_new(array); mu_assert(val2!=NULL, "failed to make a new element"); return NULL; }
static int simple_set_session( ssl_context *ssl ) { time_t t = THE_CURRENT_TIME_IS; int i = 0; ssl_session *cur = NULL; int make_new = 1; check(setup_ssl_session_cache() == 0, "Failed to initialize SSL session cache."); for(i = 0; i < darray_end(SSL_SESSION_CACHE); i++) { cur = darray_get(SSL_SESSION_CACHE, i); if( ssl->timeout != 0 && t - cur->start > ssl->timeout ) { make_new = 0; break; /* expired, reuse this slot */ } if( memcmp( ssl->session->id, cur->id, cur->length ) == 0 ) { make_new = 0; break; /* client reconnected */ } } if(make_new) { cur = (ssl_session *) darray_new(SSL_SESSION_CACHE); check_mem(cur); darray_push(SSL_SESSION_CACHE, cur); } *cur = *ssl->session; return 0; error: return 1; }
tree_ptr tree_new_ternary(tree_ptr cond, tree_ptr t1, tree_ptr t2) { tree_ptr t = tree_new(eval_ternary); t->child = darray_new(); darray_append(t->child, cond); darray_append(t->child, t1); darray_append(t->child, t2); return t; }
tree_ptr tree_new_assign(tree_ptr l, tree_ptr r) { // TODO: Check l's type. tree_ptr t = tree_new(eval_assign); t->child = darray_new(); darray_append(t->child, l); darray_append(t->child, r); return t; }
tree_ptr tree_new_define(tree_ptr id, tree_ptr parm, tree_ptr body) { tree_ptr t = tree_new(eval_define); t->child = darray_new(); darray_append(t->child, id); darray_append(t->child, parm); darray_append(t->child, body); return t; }
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; }
int Filter_add(StateEvent state, filter_cb cb, bstring load_path, tns_value_t *config) { darray_t *filters = Filter_lookup_create(state); check(filters != NULL, "Invalid filter state: %d given for filter %s", state, bdata(load_path)); Filter *filter = darray_new(filters); check_mem(filter); filter->state = state; filter->cb = cb; filter->load_path = bstrcpy(load_path); filter->config = config; darray_attach(filters, filter); darray_push(filters, filter); return 0; error: return -1; }
int Register_connect(int fd, Connection* data) { check(fd < MAX_REGISTERED_FDS, "FD given to register is greater than max."); check(data != NULL, "data can't be NULL"); Registration *reg = darray_get(REGISTRATIONS, fd); if(reg == NULL) { reg = darray_new(REGISTRATIONS); check(reg != NULL, "Failed to allocate a new registration."); // we only set this here since they stay in list forever rather than recycle darray_set(REGISTRATIONS, fd, reg); darray_attach(REGISTRATIONS, reg); } if(Register_valid(reg)) { // force them to exit int rc = Register_disconnect(fd); check(rc != -1, "Weird error trying to disconnect. Tell Zed."); tasksignal(reg->task, SIGINT); } reg->data = data; reg->last_ping = THE_CURRENT_TIME_IS; reg->fd = fd; reg->task = taskself(); reg->id = UINT32_MAX; // start off with an invalid conn_id // keep track of the number of registered things we're tracking NUM_REG_FD++; return 0; error: return -1; }
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(); }
tree_ptr tree_new_list(tree_ptr first) { tree_ptr t = tree_new(eval_list); t->child = darray_new(); darray_append(t->child, first); return t; }
tree_ptr tree_new_empty_parms() { tree_ptr t = tree_new(eval_err); t->child = darray_new(); return t; }
tree_ptr tree_new_empty_stmt_list() { tree_ptr t = tree_new(eval_err); t->child = darray_new(); return t; }
* Note: the option name in config file must be the same as in * tcmu_config. * * 3, You should add your own set method in: * static void tcmu_conf_set_options(struct tcmu_config *cfg) * { * TCMU_PARSE_CFG_INT(cfg, log_level); * TCMU_CONF_CHECK_LOG_LEVEL(log_level); * } * * Note: For now, if the options have been changed in config file, the * system config reload thread daemon will try to update them for all the * tcmu-runner, consumer and tcmu-synthesizer daemons. */ static darray(struct tcmu_conf_option) tcmu_options = darray_new(); static struct tcmu_conf_option * tcmu_get_option(const char *key) { struct tcmu_conf_option *option; darray_foreach(option, tcmu_options) { if (!strcmp(option->key, key)) return option; } return NULL; } #define TCMU_PARSE_CFG_INT(cfg, key) \ do { \
static void _scws_msegment(scws_t s, int end, int zlen) { word_t **wmap, query; struct scws_zchar *zmap; unsigned char *txt; rule_item_t r1; int i, j, k, ch, clen, start; pool_t p; /* pool used to management some dynamic memory */ p = pool_new(); /* create wmap & zmap */ wmap = s->wmap = (word_t **) darray_new(zlen, zlen, sizeof(word_t)); zmap = s->zmap = (struct scws_zchar *) pmalloc(p, zlen * sizeof(struct scws_zchar)); txt = s->txt; start = s->off; s->zis = -1; for (i = 0; start < end; i++) { ch = txt[start]; clen = SCWS_CHARLEN(ch); if (clen == 1) { while (start++ < end) { ch = txt[start]; if (start == end || SCWS_CHARLEN(txt[start]) > 1) break; clen++; } wmap[i][i] = (word_t) pmalloc_z(p, sizeof(word_st)); wmap[i][i]->tf = 0.5; wmap[i][i]->flag |= SCWS_ZFLAG_ENGLISH; strcpy(wmap[i][i]->attr, SCWS_IS_ALPHA(txt[start-1]) ? attr_en : attr_un); } else { query = xdict_query(s->d, txt + start, clen); wmap[i][i] = (word_t) pmalloc(p, sizeof(word_st)); if (query == NULL) { wmap[i][i]->tf = 0.5; wmap[i][i]->idf = 0.0; wmap[i][i]->flag = 0; strcpy(wmap[i][i]->attr, attr_un); } else { ch = query->flag; query->flag = SCWS_WORD_FULL; memcpy(wmap[i][i], query, sizeof(word_st)); if (query->attr[0] == '#') wmap[i][i]->flag |= SCWS_ZFLAG_SYMBOL; if (ch & SCWS_WORD_MALLOCED) free(query); } start += clen; } zmap[i].start = start - clen; zmap[i].end = start; } /* fixed real zlength */ zlen = i; /* create word query table */ for (i = 0; i < zlen; i++) { k = 0; for (j = i+1; j < zlen; j++) { query = xdict_query(s->d, txt + zmap[i].start, zmap[j].end - zmap[i].start); if (query == NULL) break; ch = query->flag; if ((ch & SCWS_WORD_FULL) && memcmp(query->attr, attr_na, 2)) { wmap[i][j] = (word_t) pmalloc(p, sizeof(word_st)); memcpy(wmap[i][j], query, sizeof(word_st)); wmap[i][i]->flag |= SCWS_ZFLAG_WHEAD; for (k = i+1; k <= j; k++) wmap[k][k]->flag |= SCWS_ZFLAG_WPART; } if (ch & SCWS_WORD_MALLOCED) free(query); if (!(ch & SCWS_WORD_PART)) break; } if (k--) { /* set nr2 to some short name */ if ((k == (i+1))) { if (!memcmp(wmap[i][k]->attr, attr_nr, 2)) wmap[i][i]->flag |= SCWS_ZFLAG_NR2; //if (wmap[i][k]->attr[0] == 'n') //wmap[i][i]->flag |= SCWS_ZFLAG_N2; } /* clean the PART flag for the last word */ if (k < j) wmap[i][k]->flag ^= SCWS_WORD_PART; } } if (s->r == NULL) goto do_segment; /* auto rule set for name & zone & chinese numeric */ /* one word auto rule check */ for (i = 0; i < zlen; i++) { if (SCWS_NO_RULE1(wmap[i][i]->flag)) continue; r1 = scws_rule_get(s->r, txt + zmap[i].start, zmap[i].end - zmap[i].start); if (r1 == NULL) continue; clen = r1->zmin > 0 ? r1->zmin : 1; if ((r1->flag & SCWS_ZRULE_PREFIX) && (i < (zlen - clen))) { /* prefix, check after (zmin~zmax) */ // 先检查 zmin 字内是否全部符合要求 // 再在 zmax 范围内取得符合要求的字 // int i, j, k, ch, clen, start; for (ch = 1; ch <= clen; ch++) { j = i + ch; ___ZRULE_CHECKER1___ ___ZRULE_CHECKER3___ } if (ch <= clen) continue; /* no limit znum or limit to a range */ j = i + ch; while (1) { if ((!r1->zmax && r1->zmin) || (r1->zmax && (clen >= r1->zmax))) break; ___ZRULE_CHECKER1___ ___ZRULE_CHECKER3___ clen++; j++; } // 注意原来2字人名,识别后仍为2字的情况 if (wmap[i][i]->flag & SCWS_ZFLAG_NR2) { if (clen == 1) continue; wmap[i][i+1]->flag |= SCWS_WORD_PART; } /* ok, got: i & clen */ k = i + clen; wmap[i][k] = (word_t) pmalloc(p, sizeof(word_st)); wmap[i][k]->tf = r1->tf; wmap[i][k]->idf = r1->idf; wmap[i][k]->flag = (SCWS_WORD_RULE|SCWS_WORD_FULL); strncpy(wmap[i][k]->attr, r1->attr, 2); wmap[i][i]->flag |= SCWS_ZFLAG_WHEAD; for (j = i+1; j <= k; j++) wmap[j][j]->flag |= SCWS_ZFLAG_WPART; if (!(wmap[i][i]->flag & SCWS_ZFLAG_WPART)) i = k; continue; } if ((r1->flag & SCWS_ZRULE_SUFFIX) && (i >= clen)) { /* suffix, check before */ for (ch = 1; ch <= clen; ch++) { j = i - ch; ___ZRULE_CHECKER2___ ___ZRULE_CHECKER3___ } if (ch <= clen) continue; /* no limit znum or limit to a range */ j = i - ch; while (1) { if ((!r1->zmax && r1->zmin) || (r1->zmax && (clen >= r1->zmax))) break; ___ZRULE_CHECKER2___ ___ZRULE_CHECKER3___ clen++; j--; } /* ok, got: i & clen (maybe clen=1 & [k][i] isset) */ k = i - clen; if (wmap[k][i] != NULL) continue; wmap[k][i] = (word_t) pmalloc(p, sizeof(word_st)); wmap[k][i]->tf = r1->tf; wmap[k][i]->idf = r1->idf; wmap[k][i]->flag = SCWS_WORD_FULL; strncpy(wmap[k][i]->attr, r1->attr, 2); wmap[k][k]->flag |= SCWS_ZFLAG_WHEAD; for (j = k+1; j <= i; j++) { wmap[j][j]->flag |= SCWS_ZFLAG_WPART; if ((j != i) && (wmap[k][j] != NULL)) wmap[k][j]->flag |= SCWS_WORD_PART; } continue; } } /* two words auto rule check (欧阳** , **西路) */ for (i = zlen - 2; i >= 0; i--) { /* with value ==> must be have SCWS_WORD_FULL, so needn't check it ag. */ if ((wmap[i][i+1] == NULL) || (wmap[i][i+1]->flag & SCWS_WORD_PART)) continue; k = i+1; r1 = scws_rule_get(s->r, txt + zmap[i].start, zmap[k].end - zmap[i].start); if (r1 == NULL) continue; clen = r1->zmin > 0 ? r1->zmin : 1; if ((r1->flag & SCWS_ZRULE_PREFIX) && (k < (zlen - clen))) { for (ch = 1; ch <= clen; ch++) { j = k + ch; ___ZRULE_CHECKER1___ ___ZRULE_CHECKER3___ } if (ch <= clen) continue; /* no limit znum or limit to a range */ j = k + ch; while (1) { if ((!r1->zmax && r1->zmin) || (r1->zmax && (clen >= r1->zmax))) break; ___ZRULE_CHECKER1___ ___ZRULE_CHECKER3___ clen++; j++; } /* ok, got: i & clen */ k = k + clen; wmap[i][k] = (word_t) pmalloc(p, sizeof(word_st)); wmap[i][k]->tf = r1->tf; wmap[i][k]->idf = r1->idf; wmap[i][k]->flag = SCWS_WORD_FULL; strncpy(wmap[i][k]->attr, r1->attr, 2); wmap[i][i+1]->flag |= SCWS_WORD_PART; for (j = i+2; j <= k; j++) wmap[j][j]->flag |= SCWS_ZFLAG_WPART; i--; continue; } if ((r1->flag & SCWS_ZRULE_SUFFIX) && (i >= clen)) { /* suffix, check before */ for (ch = 1; ch <= clen; ch++) { j = i - ch; ___ZRULE_CHECKER2___ ___ZRULE_CHECKER3___ } if (ch <= clen) continue; /* no limit znum or limit to a range */ j = i - ch; while (1) { if ((!r1->zmax && r1->zmin) || (r1->zmax && (clen >= r1->zmax))) break; ___ZRULE_CHECKER2___ ___ZRULE_CHECKER3___ clen++; j--; } /* ok, got: i & clen (maybe clen=1 & [k][i] isset) */ k = i - clen; i = i + 1; wmap[k][i] = (word_t) pmalloc(p, sizeof(word_st)); wmap[k][i]->tf = r1->tf; wmap[k][i]->idf = r1->idf; wmap[k][i]->flag = SCWS_WORD_FULL; strncpy(wmap[k][i]->attr, r1->attr, 2); wmap[k][k]->flag |= SCWS_ZFLAG_WHEAD; for (j = k+1; j <= i; j++) { wmap[j][j]->flag |= SCWS_ZFLAG_WPART; if (wmap[k][j] != NULL) wmap[k][j]->flag |= SCWS_WORD_PART; } i -= (clen+1); continue; } } /* real do the segment */ do_segment: /* find the easy break point */ for (i = 0, j = 0; i < zlen; i++) { if (wmap[i][i]->flag & SCWS_ZFLAG_WPART) continue; if (i > j) _scws_mseg_zone(s, j, i-1); j = i; if (!(wmap[i][i]->flag & SCWS_ZFLAG_WHEAD)) { _scws_mset_word(s, i, i); j++; } } /* the lastest zone */ if (i > j) _scws_mseg_zone(s, j, i-1); /* the last single for duality */ if ((s->mode & SCWS_DUALITY) && (s->zis >= 0) && !(s->zis & SCWS_ZIS_USED)) { i = s->zis; SCWS_PUT_RES(s->zmap[i].start, s->wmap[i][i]->idf, (s->zmap[i].end - s->zmap[i].start), s->wmap[i][i]->attr); } /* free the wmap & zmap */ pool_free(p); darray_free((void **) wmap); }
tree_ptr tree_new_funcall(void) { tree_ptr t = tree_new(eval_funcall); t->child = darray_new(); return t; }
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; }
#include <signal.h> #include <glib.h> #include <gio/gio.h> #include <libnl3/netlink/genl/genl.h> #include <libnl3/netlink/genl/mngt.h> #include <libnl3/netlink/genl/ctrl.h> #include <linux/target_core_user.h> #include "darray.h" #include "tcmu-runner.h" #define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0])) #define HANDLER_PATH "." darray(struct tcmu_handler) handlers = darray_new(); struct tcmu_thread { pthread_t thread_id; char dev_name[16]; /* e.g. "uio14" */ }; static darray(struct tcmu_thread) threads = darray_new(); static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = { [TCMU_ATTR_DEVICE] = { .type = NLA_STRING }, [TCMU_ATTR_MINOR] = { .type = NLA_U32 }, }; static int add_device(char *dev_name, char *cfgstring); static void remove_device(char *dev_name, char *cfgstring);
#include <poll.h> #include <libkmod.h> #include <linux/target_core_user.h> #include "darray.h" #include "tcmu-runner.h" #include "libtcmu.h" #include "tcmuhandler-generated.h" #include "version.h" #define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0])) static char *handler_path = DEFAULT_HANDLER_PATH; static bool debug = false; darray(struct tcmur_handler *) g_runner_handlers = darray_new(); struct tcmu_thread { pthread_t thread_id; struct tcmu_device *dev; }; static darray(struct tcmu_thread) g_threads = darray_new(); /* * Debug API implementation */ void dbgp(const char *fmt, ...) { if (debug) { va_list va;
int opt_replace_const(expr *prog) { if( !prog ) return 0; // Get current number of variables, used to estimate the // memory usage of each new constant added to the program vars *v = pgm_get_vars( expr_get_program(prog) ); unsigned nfloat = vars_get_count(v, vtFloat); unsigned nstring = vars_get_count(v, vtString); unsigned nvar = vars_get_total(v); // If not enough variables, exit if( nvar > 255 ) return 0; // Search all constant values in the program and store // the value and number of times repeated cvalue_list *lst = darray_new(cvalue,256); int num = update_cvalue(prog, lst); // If no constant values, exit. if( !num ) { darray_free(lst); return 0; } // Now, sort constant values by "usage gain" cvalue_list_sort(lst); // Extract all constant values that produce a gain: unsigned cs = 0, cn = 0; for(unsigned i=0; i<lst->len && nvar<256; i++) { cvalue *cv = lst->data + i; int bytes = cvalue_saved_bytes(cv); // Add one extra byte if the variable number is // more than 127: if( nvar > 127 ) bytes += cv->count; if( bytes > 0 ) continue; // Ok, we can replace the variable if( cv->str ) { char name[256]; sprintf(name, "__s%d", cs); cs++; nstring++; nvar++; info_print(expr_get_file_name(prog), 0, "replacing constant var %s$=\"%.*s\" (%d times, %d bytes)\n", name, cv->slen, cv->str, cv->count, bytes); // Creates the variable cv->vid = vars_new_var(v, name, vtString, expr_get_file_name(prog), 0); cv->status = 1; // Replace all instances of the constant value with the variables replace_cvalue(prog, cv); } else { char name[256]; if( cv->num < 100000 && cv->num == round(cv->num) ) { if( cv->num >= 0 ) sprintf(name, "__n%.0f", cv->num); else sprintf(name, "__n_%.0f", -cv->num); } else if( cv->num < 1000 && round(10000000 * cv->num) == 1000000 * round(10 * cv->num) ) { if( cv->num >= 0 ) sprintf(name, "__n%.0f_%.0f", trunc(cv->num), 10 * (cv->num - trunc(cv->num))); else sprintf(name, "__n_%.0f_%.0f", -trunc(cv->num), -10 * (cv->num - trunc(cv->num)) ); } else { sprintf(name, "__nd%d", cn); cn++; } nfloat++; nvar++; info_print(expr_get_file_name(prog), 0, "replacing constant var %s=%g (%d times, %d bytes)\n", name, cv->num, cv->count, bytes); cv->vid = vars_new_var(v, name, vtFloat, expr_get_file_name(prog), 0); cv->status = 1; // Replace all instances of the constant value with the variables replace_cvalue(prog, cv); } } // Sort again by absolute value, this tends to generate smaller code cvalue_list_sort_abs(lst); // Now, add all variable initializations to the program, first numeric, then strings: expr *init = 0, *last_stmt = 0, *dim = 0; for(unsigned i=0; i<lst->len; i++) { cvalue *cv = lst->data + i; if( cv->status == 1 && !cv->str ) { last_stmt = create_num_assign(prog->mngr, lst, last_stmt, cv->num, cv->vid); if( !init ) init = last_stmt; cv->status = 2; } } // Now, all DIM expressions for(unsigned i=0; i<lst->len; i++) { cvalue *cv = lst->data + i; if( cv->status == 1 && cv->str ) { dim = create_str_dim(prog->mngr, lst, dim, cv->slen, cv->vid); cv->status = 2; } } if( dim ) { last_stmt = expr_new_stmt(prog->mngr, last_stmt, dim, STMT_DIM); if( !init ) init = last_stmt; } // And all string assignments for(unsigned i=0; i<lst->len; i++) { cvalue *cv = lst->data + i; if( cv->status == 2 && cv->str ) { last_stmt = create_str_assign(prog->mngr, last_stmt, cv->str, cv->slen, cv->vid); if( !init ) init = last_stmt; cv->status = 2; } } add_to_prog(prog, init); darray_free(lst); return 0; }