char *correct_str(correction_t *corr, correct_trie_t* table, const char *ident) { char **e1 = NULL; char **e2 = NULL; char *e1ident = NULL; char *e2ident = NULL; size_t e1rows = 0; size_t e2rows = 0; size_t *bits = NULL; /* needs to be allocated for free later */ if (correct_find(table, ident)) return correct_pool_claim(ident); if ((e1rows = correct_size(ident))) { if (vec_size(corr->edits) > 0) e1 = corr->edits[0]; else { e1 = correct_edit(ident, &bits); vec_push(corr->edits, e1); vec_push(corr->lens, bits); } if ((e1ident = correct_maximum(table, e1, e1rows))) return correct_pool_claim(e1ident); } e2 = correct_known(corr, table, e1, e1rows, &e2rows); if (e2rows && ((e2ident = correct_maximum(table, e2, e2rows)))) return correct_pool_claim(e2ident); return util_strdup(ident); }
code_t *code_init() { static lex_ctx_t empty_ctx = {0, 0, 0}; static prog_section_function_t empty_function = {0,0,0,0,0,0,0,{0,0,0,0,0,0,0,0}}; static prog_section_statement_t empty_statement = {0,{0},{0},{0}}; static prog_section_def_t empty_def = {0, 0, 0}; code_t *code = (code_t*)mem_a(sizeof(code_t)); int i = 0; memset(code, 0, sizeof(code_t)); code->entfields = 0; code->string_cache = util_htnew(OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS) ? 0x100 : 1024); /* * The way progs.dat is suppose to work is odd, there needs to be * some null (empty) statements, functions, and 28 globals */ for(; i < 28; i++) vec_push(code->globals, 0); vec_push(code->chars, '\0'); vec_push(code->functions, empty_function); code_push_statement(code, &empty_statement, empty_ctx); vec_push(code->defs, empty_def); vec_push(code->fields, empty_def); return code; }
void dyad_vwritef(dyad_Stream *stream, const char *fmt, va_list args) { char buf[512]; char *str; char f[] = "%_"; FILE *fp; int c; while (*fmt) { if (*fmt == '%') { fmt++; switch (*fmt) { case 'r': fp = va_arg(args, FILE*); if (fp == NULL) { str = "(null)"; goto writeStr; } while ((c = fgetc(fp)) != EOF) { vec_push(&stream->writeBuffer, c); } break; case 'c': vec_push(&stream->writeBuffer, va_arg(args, int)); break; case 's': str = va_arg(args, char*); if (str == NULL) str = "(null)"; writeStr: while (*str) { vec_push(&stream->writeBuffer, *str++); } break; case 'b': str = va_arg(args, char*); c = va_arg(args, int); while (c--) { vec_push(&stream->writeBuffer, *str++); } break; default: f[1] = *fmt; switch (*fmt) { case 'f': case 'g': sprintf(buf, f, va_arg(args, double)); break; case 'd': case 'i': sprintf(buf, f, va_arg(args, int)); break; case 'x': case 'X': sprintf(buf, f, va_arg(args, unsigned)); break; case 'p': sprintf(buf, f, va_arg(args, void*)); break; default : buf[0] = *fmt; buf[1] = '\0'; } str = buf; goto writeStr; } } else {
static void classify_args(Vector *ints, Vector *floats, Vector *rest, Vector *args) { SAVE; int ireg = 0, xreg = 0; int imax = 6, xmax = 8; for (int i = 0; i < vec_len(args); i++) { Node *v = vec_get(args, i); if (v->ty->kind == KIND_STRUCT) vec_push(rest, v); else if (is_flotype(v->ty)) vec_push((xreg++ < xmax) ? floats : rest, v); else vec_push((ireg++ < imax) ? ints : rest, v); } }
static void parse_opts(int argc, char *argv[]) { for (int i = 1; i < argc; i++) { char *arg = argv[i]; if (arg[0] == '-') { if (!strcmp(arg, "-o")) { if (++i >= argc) die("missing file name after '-o'"); else if (output) fprintf(stderr, "warning: output file overwritten\n"); output = argv[i]; } else if (!strcmp(arg, "-ast-dump")) { opts.ast_dump = true; } else if (!strcmp(arg, "-ir-dump")) { opts.ir_dump = true; } else if (!strcmp(arg, "-c")) { opts.c = true; } else if (!strcmp(arg, "-E")) { opts.E = true; } else if (!strcmp(arg, "-S")) { opts.S = true; } else if (!strcmp(arg, "-h") || !strcmp(arg, "--help") || !strcmp(arg, "-v") || !strcmp(arg, "--version")) { usage(); exit(EXIT_FAILURE); } else if (!strncmp(arg, "-I", 2) || !strncmp(arg, "-D", 2) || !strncmp(arg, "-U", 2)) { vec_push(opts.cpp_options, arg); } else if (!strncmp(arg, "-l", 2) || !strncmp(arg, "-L", 2)) { vec_push(opts.ld_options, arg); } else if (!strcmp(arg, "-Wall")) { opts.Wall = true; } else if (!strcmp(arg, "-Werror")) { opts.Werror = true; } else { fprintf(stderr, "warning: ignored unknown option: %s\n", arg); } } else { vec_push(inputs, arg); } } }
static qcint_t prog_enterfunction(qc_program_t *prog, prog_section_function_t *func) { qc_exec_stack_t st; size_t parampos; int32_t p; /* back up locals */ st.localsp = vec_size(prog->localstack); st.stmt = prog->statement; st.function = func; if (prog->xflags & VMXF_TRACE) { const char *str = prog_getstring(prog, func->name); vec_push(prog->function_stack, str); } #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS if (vec_size(prog->stack)) { prog_section_function_t *cur; cur = prog->stack[vec_size(prog->stack)-1].function; if (cur) { qcint_t *globals = &prog->globals[0] + cur->firstlocal; vec_append(prog->localstack, cur->locals, globals); } } #else { qcint_t *globals = &prog->globals[0] + func->firstlocal; vec_append(prog->localstack, func->locals, globals); } #endif /* copy parameters */ parampos = func->firstlocal; for (p = 0; p < func->nargs; ++p) { size_t s; for (s = 0; s < func->argsize[p]; ++s) { prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s]; ++parampos; } } vec_push(prog->stack, st); return func->entry; }
// parallel initialize the web structure from redis void* parallel_init(void* _parallel_init_info) { assert(_parallel_init_info != NULL); parallel_i_info* info = (parallel_i_info*) _parallel_init_info; assert(info->w != NULL); assert(info->c != NULL); web* w = info->w; redisContext **c = info->c; size_t n_threads = info->n_threads; size_t id = info->id; size_t size = info->size; size_t start = info->start; int *out_link_size = info->out_link_size; unsigned int i, j; int mod = (int)n_threads; redisReply *reply; for (i = start + id; i < size; i = i + mod) { char uid_str[100]; reply = redisCommand(c[id], "LRANGE uid:%s:linkfrom 0 -1", ltoa(i, uid_str)); if (reply->type == REDIS_REPLY_ARRAY) { int in_link_page; for (j = 0; j < reply->elements; ++j) { in_link_page = atoi(reply->element[j]->str); vec_push(w->webnodes[i]->in_links, in_link_page); ++out_link_size[in_link_page]; } } freeReplyObject(reply); } return NULL; }
int test() { vector * vec = (vector *) malloc(sizeof(struct vector)); vec_init(vec, 50); int i; for (i = 0; i < 14; i++) { vec_push(vec, i * 4 + 2); } vec_set(vec, 0, 1); printf("capacity = %zd, %zd\n", vec->capacity, vec->size); vec_set(vec, 39, 1); printf("capacity = %zd, %zd\n", vec->capacity, vec->size); vec_set(vec, 19, 2); printf("capacity = %zd, %zd\n", vec->capacity, vec->size); vec_set(vec, 45, 2); printf("capacity = %zd, %zd\n", vec->capacity, vec->size); printf("vec[3] = %d\n", vec_get(vec, 3)); printf("vec[6] = %d\n", vec_get(vec, 6)); printf("vec[666] = %d\n", vec_get(vec, 666)); for (size_t i = 0; i < vec->size; i++) { printf("vec[%zd] = %d\n", i, vec->data[i]); } vec_free(vec); return 0; }
ast_expression *fold_constgen_string(fold_t *fold, const char *str, bool translate) { hash_table_t *table = (translate) ? fold->imm_string_untranslate : fold->imm_string_dotranslate; ast_value *out = NULL; size_t hash = util_hthash(table, str); if ((out = (ast_value*)util_htgeth(table, str, hash))) return (ast_expression*)out; if (translate) { char name[32]; util_snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(fold->parser->translated++)); out = ast_value_new(parser_ctx(fold->parser), name, TYPE_STRING); out->expression.flags |= AST_FLAG_INCLUDE_DEF; /* def needs to be included for translatables */ } else out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_STRING); out->cvq = CV_CONST; out->hasvalue = true; out->isimm = true; out->constval.vstring = parser_strdup(str); vec_push(fold->imm_string, out); util_htseth(table, str, hash, out); return (ast_expression*)out; }
static bool decode_visit_dbpointer(const bson_iter_t *iter, const char *key, size_t v_collection_len, const char *v_collection, const bson_oid_t *v_oid, void *data) { decode_state *ds = data; ERL_NIF_TERM col, oid, out; if(!make_binary(ds->env, &col, v_collection, v_collection_len)) { return true; } if(v_oid) { if(!make_binary(ds->env, &oid, v_oid->bytes, 12)) { return true; } } else { oid = ds->st->atom_null; } out = enif_make_tuple4( ds->env, ds->st->atom_s_ref, col, ds->st->atom_s_oid, oid), vec_push(ds->vec, out); return false; }
uint32_t code_genstring(code_t *code, const char *str) { size_t hash; code_hash_entry_t existing; if (!str) return 0; if (!*str) { if (!code->string_cached_empty) { code->string_cached_empty = vec_size(code->chars); vec_push(code->chars, 0); } return code->string_cached_empty; } if (OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS)) { hash = ((unsigned char*)str)[strlen(str)-1]; CODE_HASH_ENTER(existing) = code_util_str_htgeth(code->string_cache, str, hash); } else { hash = util_hthash(code->string_cache, str); CODE_HASH_ENTER(existing) = util_htgeth(code->string_cache, str, hash); } if (CODE_HASH_ENTER(existing)) return CODE_HASH_LEAVE(existing); CODE_HASH_LEAVE(existing) = vec_size(code->chars); vec_append(code->chars, strlen(str)+1, str); util_htseth(code->string_cache, str, hash, CODE_HASH_ENTER(existing)); return CODE_HASH_LEAVE(existing); }
void transition_add_rule(Transition t, lmn_interned_str rule_name, LmnCost cost) { if (rule_name != ANONYMOUS || !vec_contains(&t->rule_names, rule_name)) { #ifdef PROFILE if (lmn_env.profile_level >= 3) { profile_remove_space(PROFILE_SPACE__TRANS_OBJECT, transition_space(t)); } #endif vec_push(&t->rule_names, rule_name); #ifdef KWBT_OPT if (((lmn_env.opt_mode == OPT_MINIMIZE) && t->cost > cost) || ((lmn_env.opt_mode == OPT_MAXIMIZE) && t->cost < cost)) { t->cost = cost; } #endif #ifdef PROFILE if (lmn_env.profile_level >= 3) { profile_add_space(PROFILE_SPACE__TRANS_OBJECT, transition_space(t)); } #endif } }
static int translate(const char *ifile, const char *ofile) { struct vector *v = vec_new(); vec_push(v, (char *)ifile); vec_push_safe(v, (char *)ofile); return runproc(program, v); }
static bool decode_visit_document(const bson_iter_t *iter, const char *key, const bson_t *v_document, void *data) { decode_state *ds = data; decode_state cs; ERL_NIF_TERM out; LOG("visit document: %s \r\n", key); if(ds->depth >= MAX_DEPTHS) { return true; } init_child_state(ds, &cs); cs.keys = true; cs.depth = ds->depth + 1; if(!iter_bson(v_document, &out, &cs)) { return true; } vec_push(ds->vec, out); return false; }
static bool decode_visit_regex(const bson_iter_t *iter, const char *key, const char *v_regex, const char *v_options, void *data) { decode_state *ds = data; ERL_NIF_TERM regex, options, out; if(!make_binary(ds->env, ®ex, v_regex, strlen(v_regex))) { return true; } if(!make_binary(ds->env, &options, v_options, strlen(v_options))) { return true; } out = enif_make_tuple4(ds->env, ds->st->atom_s_regex, regex, ds->st->atom_s_options, options); vec_push(ds->vec, out); return false; }
static bool decode_visit_codewscope(const bson_iter_t *iter, const char *key, size_t v_code_len, const char *v_code, const bson_t *v_scope, void *data) { decode_state *ds = data; decode_state cs; ERL_NIF_TERM code, scope, out; if(!make_binary(ds->env, &code, v_code, v_code_len)) { return true; } init_child_state(ds, &cs); cs.depth = ds->depth; cs.keys = true; if(!iter_bson(v_scope, &scope, &cs)) { return true; } out = enif_make_tuple4( ds->env, ds->st->atom_s_javascript, code, ds->st->atom_s_scope, scope); vec_push(ds->vec, out); return false; }
void dyad_write(dyad_Stream *stream, const void *data, int size) { const char *p = data; while (size--) { vec_push(&stream->writeBuffer, *p++); } stream->flags |= DYAD_FLAG_WRITTEN; }
int main(void) { int i, num_jobs; process_pool *pool; vec(job_t) cq; vec_init(&cq); num_jobs = 10000; for (i = 0; i < num_jobs; i++) { job_t jobs = {"Yhm, Forever!", i}; vec_push(&cq, jobs); } printf("Master[%d] Runnig\n", getpid()); process_pool_envinit(); pool = process_pool_new(3); process_pool_start(pool); for (i= 0; i < num_jobs; i++) { process_pool_dispatch(pool, &cq.data[i]); } wait(NULL); vec_deinit(&cq); return 0; }
static void fields(node_t * sym) { int follow[] = {INT, CONST, '}', IF, 0}; node_t *sty = SYM_TYPE(sym); if (!first_decl(token)) { error("expect type name or qualifiers"); return; } struct vector *v = vec_new(); do { node_t *basety = specifiers(NULL, NULL); for (;;) { node_t *field = new_field(); if (token->id == ':') { bitfield(field); FIELD_TYPE(field) = basety; } else { node_t *ty = NULL; struct token *id = NULL; declarator(&ty, &id, NULL); attach_type(&ty, basety); if (token->id == ':') bitfield(field); FIELD_TYPE(field) = ty; if (id) { for (int i = 0; i < vec_len(v); i++) { node_t *f = vec_at(v, i); if (FIELD_NAME(f) && !strcmp(FIELD_NAME(f), id->name)) { errorf(id->src, "redefinition of '%s'", id->name); break; } } FIELD_NAME(field) = id->name; AST_SRC(field) = id->src; } } vec_push(v, field); if (token->id != ',') break; expect(','); ensure_field(field, vec_len(v), false); } match(';', follow); ensure_field(vec_tail(v), vec_len(v), isstruct(sty) && !first_decl(token)); } while (first_decl(token)); TYPE_FIELDS(sty) = (node_t **) vtoa(v); set_typesize(sty); }
int vec_inserted_index(Vector *v, LmnWord w) { int i; for (i = 0; i < vec_num(v); i++){ if (vec_get(v, i) == w) return i; } vec_push(v, w); return vec_num(v) - 1; }
void dyad_addListener( dyad_Stream *stream, int event, dyad_Callback callback, void *udata ) { Listener listener; listener.event = event; listener.callback = callback; listener.udata = udata; vec_push(&stream->listeners, listener); }
static bool decode_visit_maxkey(const bson_iter_t *iter, const char *key, void *data) { decode_state *ds = data; vec_push(ds->vec, ds->st->atom_maxkey); return false; }
inline static struct buffer * newbuffer(struct editor *e) { struct buffer *b = alloc(sizeof *b); *b = buffer_new(e->nbufs++); vec_push(e->buffers, b); vec_push(e->pollfds, ((struct pollfd){ .fd = b->read_fd, .events = POLLIN }));
static bool decode_visit_bool(const bson_iter_t *iter, const char *key, bool v_bool, void *data) { decode_state *ds = data; vec_push(ds->vec, v_bool ? ds->st->atom_true : ds->st->atom_false); return false; }
struct atom *exprs_add(struct atom *atom, struct expr *arg) { if(atom->type != ATOM_TYPE_EXPRS) { LOG(LOG_ERROR, ("can't add expr to atom of type %d\n", atom->type)); exit(1); } vec_push(atom->u.exprs, &arg); return atom; }
static pak_file_t *pak_open_read(const char *file) { pak_file_t *pak; size_t itr; if (!(pak = (pak_file_t*)mem_a(sizeof(pak_file_t)))) return NULL; if (!(pak->handle = fs_file_open(file, "rb"))) { mem_d(pak); return NULL; } pak->directories = NULL; pak->insert = false; /* read doesn't allow insert */ memset (&pak->header, 0, sizeof(pak_header_t)); fs_file_read (&pak->header, sizeof(pak_header_t), 1, pak->handle); util_endianswap(&pak->header.magic, 1, sizeof(pak->header.magic)); util_endianswap(&pak->header.diroff, 1, sizeof(pak->header.diroff)); util_endianswap(&pak->header.dirlen, 1, sizeof(pak->header.dirlen)); /* * Every PAK file has "PACK" stored as FOURCC data in the * header. If this data cannot compare (as checked here), it's * probably not a PAK file. */ if (pak->header.magic != PAK_FOURCC) { fs_file_close(pak->handle); mem_d (pak); return NULL; } /* * Time to read in the directory handles and prepare the directories * vector. We're going to be reading some the file inwards soon. */ fs_file_seek(pak->handle, pak->header.diroff, FS_FILE_SEEK_SET); /* * Read in all directories from the PAK file. These are considered * to be the "file entries". */ for (itr = 0; itr < pak->header.dirlen / 64; itr++) { pak_directory_t dir; fs_file_read (&dir, sizeof(pak_directory_t), 1, pak->handle); /* Don't translate name - it's just an array of bytes. */ util_endianswap(&dir.pos, 1, sizeof(dir.pos)); util_endianswap(&dir.len, 1, sizeof(dir.len)); vec_push(pak->directories, dir); } return pak; }
static bool decode_visit_int64(const bson_iter_t *iter, const char *key, int64_t v_int64, void *data) { decode_state *ds = data; ERL_NIF_TERM out = enif_make_int64(ds->env, v_int64); vec_push(ds->vec, out); return false; }
// generate web from sharding redis void gen_web_sharded_redis(redisContext **c, web* w, size_t start) { assert(w != NULL); assert(c != NULL); unsigned int n_threads = g_n_sharded; // each sharded redis use one thread pthread_t* callThd = calloc(n_threads, sizeof(pthread_t)); size_t i; #ifdef USE_SPINLOCK g_spin_locks = calloc(n_threads, sizeof(pthread_spinlock_t)); for (i = 0; i < n_threads; ++i) pthread_spin_init(&g_spin_locks[i], 0); #else g_mutex_locks = calloc(n_threads, sizeof(pthread_mutex_t)); for (i = 0; i < n_threads; ++i) pthread_mutex_init(&g_mutex_locks[i], NULL); #endif int **out_link_size; out_link_size = calloc(n_threads, sizeof(int*)); for (i = 0; i < n_threads; ++i) out_link_size[i] = calloc(w->size, sizeof(int)); for (i = 0; i < n_threads; ++i) { parallel_i_info* info = malloc(sizeof(parallel_i_info)); info->n_threads = n_threads; info->size = w->size; info->w = w; info->id = i; info->c = c; info->start = start; info->out_link_size = out_link_size[i]; int ret = pthread_create(&callThd[i], NULL, parallel_init, (void*) info); assert(ret == 0); } int status = 0; for (i = 0; i < n_threads; ++i) { int ret = pthread_join(callThd[i], (void**) &status); assert(ret == 0); assert(status == 0); } // wait until all threads complete computing for (i = 0; i < n_threads; ++i) { for (size_t j = 0; j < w->size; ++j) { w->webnodes[j]->out_links_num += out_link_size[i][j]; if (i == (n_threads-1) && w->webnodes[j]->out_links_num == 0) vec_push(w->dangling_pages, j); } free(out_link_size[i]); } free(out_link_size); free(callThd); }
void lex_init(char *filename) { vec_push(buffers, make_vector()); if (!strcmp(filename, "-")) { push_stream(stdin, NULL); return; } FILE *fp = fopen(filename, "r"); if (!fp) error("Cannot open %s: %s", filename, strerror(errno)); push_stream(fp, filename); }
static bool decode_visit_double(const bson_iter_t *iter, const char *key, double v_double, void *data) { decode_state *ds = data; ERL_NIF_TERM out = enif_make_double(ds->env, v_double); vec_push(ds->vec, out); return false; }