Bank* bank_create() { Bank *bank = (Bank*) malloc(sizeof(Bank)); if(bank == NULL) { perror("Could not allocate Bank"); exit(1); } // Set up the network state bank->sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&bank->rtr_addr,sizeof(bank->rtr_addr)); bank->rtr_addr.sin_family = AF_INET; bank->rtr_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); bank->rtr_addr.sin_port=htons(ROUTER_PORT); bzero(&bank->bank_addr, sizeof(bank->bank_addr)); bank->bank_addr.sin_family = AF_INET; bank->bank_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); bank->bank_addr.sin_port = htons(BANK_PORT); bind(bank->sockfd,(struct sockaddr *)&bank->bank_addr,sizeof(bank->bank_addr)); // Set up the protocol state // TODO set up more, as needed bank->logged_user_ht = hash_table_create(MAX_USER_NUM); bank->user_balance_ht = hash_table_create(MAX_USER_NUM); return bank; }
struct dag_node *dag_node_create(struct dag *d, int linenum) { struct dag_node *n; n = malloc(sizeof(struct dag_node)); memset(n, 0, sizeof(struct dag_node)); n->d = d; n->linenum = linenum; n->state = DAG_NODE_STATE_WAITING; n->nodeid = d->nodeid_counter++; n->variables = hash_table_create(0, 0); n->source_files = list_create(0); n->target_files = list_create(0); n->remote_names = itable_create(0); n->remote_names_inv = hash_table_create(0, 0); n->descendants = set_create(0); n->ancestors = set_create(0); n->ancestor_depth = -1; n->resources = make_rmsummary(-1); if(verbose_parsing && d->nodeid_counter % PARSING_RULE_MOD_COUNTER == 0) { fprintf(stdout, "\rRules parsed: %d", d->nodeid_counter + 1); fflush(stdout); } return n; }
struct dag_node *dag_node_create(struct dag *d, int linenum) { struct dag_node *n; n = malloc(sizeof(struct dag_node)); memset(n, 0, sizeof(struct dag_node)); n->d = d; n->linenum = linenum; n->state = DAG_NODE_STATE_WAITING; n->nodeid = d->nodeid_counter++; n->variables = hash_table_create(0, 0); n->source_files = list_create(0); n->target_files = list_create(0); n->remote_names = itable_create(0); n->remote_names_inv = hash_table_create(0, 0); n->descendants = set_create(0); n->ancestors = set_create(0); n->ancestor_depth = -1; n->resources_requested = rmsummary_create(-1); n->resources_measured = NULL; n->resource_request = CATEGORY_ALLOCATION_FIRST; return n; }
Bank* bank_create() { Bank *bank = (Bank*) malloc(sizeof(Bank)); if(bank == NULL) { perror("Could not allocate Bank"); exit(1); } // Set up the network state bank->sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&bank->rtr_addr,sizeof(bank->rtr_addr)); bank->rtr_addr.sin_family = AF_INET; bank->rtr_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); bank->rtr_addr.sin_port=htons(ROUTER_PORT); bzero(&bank->bank_addr, sizeof(bank->bank_addr)); bank->bank_addr.sin_family = AF_INET; bank->bank_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); bank->bank_addr.sin_port = htons(BANK_PORT); bind(bank->sockfd,(struct sockaddr *)&bank->bank_addr,sizeof(bank->bank_addr)); // Set up the protocol state // TODO set up more, as needed bank->user_pin = hash_table_create(100); bank->user_bal = hash_table_create(100); bank->key = (unsigned char *)calloc(BLOCK_SIZE+1, sizeof(unsigned char)); bank->iv = (unsigned char *)calloc(BLOCK_SIZE+1, sizeof(unsigned char)); return bank; }
void chirp_alloc_flush() { char *path, *root; struct alloc_state *s; if(!alloc_enabled) return; debug(D_ALLOC, "flushing allocation states..."); if(!alloc_table) alloc_table = hash_table_create(0, 0); hash_table_firstkey(alloc_table); while(hash_table_nextkey(alloc_table, &path, (void **) &s)) { alloc_state_save(path, s); hash_table_remove(alloc_table, path); } if(!root_table) root_table = hash_table_create(0, 0); hash_table_firstkey(root_table); while(hash_table_nextkey(root_table, &path, (void **) &root)) { free(root); hash_table_remove(root_table, path); } last_flush_time = time(0); }
struct dag_node *dag_node_create(struct dag *d, int linenum) { struct dag_node *n; n = malloc(sizeof(struct dag_node)); memset(n, 0, sizeof(struct dag_node)); n->d = d; n->linenum = linenum; n->state = DAG_NODE_STATE_WAITING; n->nodeid = d->nodeid_counter++; n->variables = hash_table_create(0, 0); n->source_files = list_create(0); n->target_files = list_create(0); n->remote_names = itable_create(0); n->remote_names_inv = hash_table_create(0, 0); n->descendants = set_create(0); n->ancestors = set_create(0); n->ancestor_depth = -1; return n; }
static struct bin_config_elem_t *bin_config_add_dup_data(struct bin_config_t *bin_config, struct bin_config_elem_t *parent_elem, char *var, void *data, int size, int dup_data) { struct bin_config_elem_t *elem; struct hash_table_t *child_elem_list; /* Check parent element */ if (parent_elem && parent_elem->bin_config != bin_config) { bin_config->error_code = BIN_CONFIG_ERR_PARENT; return NULL; } /* Check valid data pointer */ if (size && !data) { bin_config->error_code = BIN_CONFIG_ERR_DATA; return NULL; } /* Get list of child elements. * Lazy creation of child element lists happens here. */ if (parent_elem) { if (!parent_elem->child_elem_list) parent_elem->child_elem_list = hash_table_create(1, 0); child_elem_list = parent_elem->child_elem_list; } else { if (!bin_config->elem_list) bin_config->elem_list = hash_table_create(1, 0); child_elem_list = bin_config->elem_list; } /* Create element */ elem = bin_config_elem_create(bin_config, data, size, dup_data); elem->parent_elem = parent_elem; /* Add to list of child elements */ if (!hash_table_insert(child_elem_list, var, elem)) { bin_config_elem_free(elem); bin_config->error_code = BIN_CONFIG_ERR_DUPLICATE; return NULL; } /* Return created element */ bin_config->error_code = BIN_CONFIG_ERR_OK; return elem; }
struct vi_x86_core_t *vi_x86_core_create(char *name) { struct vi_x86_core_t *core; /* Initialize */ core = xcalloc(1, sizeof(struct vi_x86_core_t)); core->name = str_set(NULL, name); core->context_table = hash_table_create(0, FALSE); core->inst_table = hash_table_create(0, FALSE); /* Return */ return core; }
struct dag *dag_create() { struct dag *d = malloc(sizeof(*d)); memset(d, 0, sizeof(*d)); d->nodes = 0; d->filename = NULL; d->node_table = itable_create(0); d->local_job_table = itable_create(0); d->remote_job_table = itable_create(0); d->files = hash_table_create(0, 0); d->inputs = set_create(0); d->outputs = set_create(0); d->nodeid_counter = 0; d->export_vars = set_create(0); d->special_vars = set_create(0); d->completed_files = 0; d->deleted_files = 0; d->categories = hash_table_create(0, 0); d->default_category = makeflow_category_lookup_or_create(d, "default"); d->allocation_mode = CATEGORY_ALLOCATION_MODE_FIXED; /* Add GC_*_LIST to variables table to ensure it is in * global DAG scope. / hash_table_insert(d->variables,"GC_PRESERVE_LIST" , dag_variable_create(NULL, "")); hash_table_insert(d->variables,"GC_COLLECT_LIST" , dag_variable_create(NULL, "")); hash_table_insert(d->variables,"MAKEFLOW_INPUTS" , dag_variable_create(NULL, "")); hash_table_insert(d->variables,"MAKEFLOW_OUTPUTS" , dag_variable_create(NULL, "")); */ /* Declare special variables */ set_insert(d->special_vars, "CATEGORY"); set_insert(d->special_vars, "SYMBOL"); /* Deprecated alias for CATEGORY */ set_insert(d->special_vars, RESOURCES_CORES); set_insert(d->special_vars, RESOURCES_MEMORY); set_insert(d->special_vars, RESOURCES_DISK); set_insert(d->special_vars, RESOURCES_GPUS); /* export all variables related to resources */ set_insert(d->export_vars, "CATEGORY"); set_insert(d->export_vars, RESOURCES_CORES); set_insert(d->export_vars, RESOURCES_MEMORY); set_insert(d->export_vars, RESOURCES_DISK); set_insert(d->export_vars, RESOURCES_GPUS); memset(d->node_states, 0, sizeof(int) * DAG_NODE_STATE_MAX); return d; }
int task_init(void) { if (!hash_table_create(&task_hash_table, 0, 0, &task_hash_table_ops)) { printf(NAME ": No memory available for tasks\n"); return ENOMEM; } if (!hash_table_create(&phone_to_id, 0, 0, &p2i_ops)) { printf(NAME ": No memory available for tasks\n"); return ENOMEM; } list_initialize(&pending_wait); return EOK; }
static int cmd_deduplicate_box(struct doveadm_mail_cmd_context *_ctx, const struct mailbox_info *info, struct mail_search_args *search_args) { struct deduplicate_cmd_context *ctx = (struct deduplicate_cmd_context *)_ctx; struct doveadm_mail_iter *iter; struct mailbox *box; struct mail *mail; enum mail_error error; pool_t pool; HASH_TABLE(const char *, struct uidlist *) hash; const char *key, *errstr; struct uidlist *value; int ret = 0; if (doveadm_mail_iter_init(_ctx, info, search_args, 0, NULL, &iter) < 0) return -1; pool = pool_alloconly_create("deduplicate", 10240); hash_table_create(&hash, pool, 0, str_hash, strcmp); while (doveadm_mail_iter_next(iter, &mail)) { if (ctx->by_msgid) { if (mail_get_first_header(mail, "Message-ID", &key) < 0) { errstr = mailbox_get_last_error(mail->box, &error); if (error == MAIL_ERROR_NOTFOUND) continue; i_error("Couldn't lookup Message-ID: for UID=%u: %s", mail->uid, errstr); doveadm_mail_failed_error(_ctx, error); ret = -1; break; } } else { if (mail_get_special(mail, MAIL_FETCH_GUID, &key) < 0) { errstr = mailbox_get_last_error(mail->box, &error); if (error == MAIL_ERROR_NOTFOUND) continue; i_error("Couldn't lookup GUID: for UID=%u: %s", mail->uid, errstr); doveadm_mail_failed_error(_ctx, error); ret = -1; break; } } if (key != NULL && *key != '\0') { value = p_new(pool, struct uidlist, 1); value->uid = mail->uid; value->next = hash_table_lookup(hash, key); if (value->next == NULL) { key = p_strdup(pool, key); hash_table_insert(hash, key, value); } else { hash_table_update(hash, key, value); } } }
static struct alloc_state *alloc_state_cache_exact(const char *path) { struct alloc_state *a; char *d; char dirname[CHIRP_PATH_MAX]; char statename[CHIRP_PATH_MAX]; d = alloc_state_root_cached(path); if(!d) return 0; /* Save a copy of dirname, because the following alloc_table_load_cached may result in a flush of the alloc table root. */ strcpy(dirname, d); sprintf(statename, "%s/.__alloc", dirname); if(!alloc_table) alloc_table = hash_table_create(0, 0); a = hash_table_lookup(alloc_table, dirname); if(a) return a; a = alloc_state_load(dirname); if(!a) return a; hash_table_insert(alloc_table, dirname, a); return a; }
/* FUNC DEFS */ void miniroute_initialize() { route_cache = miniroute_cache_create(); dcb_table = hash_table_create(); curr_discovery_pkt_id = 1; network_get_my_address(my_addr); //init my_addr return; }
static struct chirp_client * connect_to_host( const char *host, time_t stoptime ) { struct chirp_client *c; if(!table) { table = hash_table_create(0,0); if(!table) return 0; } c = hash_table_lookup(table,host); if(c) return c; if(!strncmp(host,"CONDOR",6)) { c = chirp_client_connect_condor(stoptime); } else { c = chirp_client_connect(host,1,stoptime); } if(c) { /* If a default replication factor was set earlier, then it must be re-applied when re-connecting after a failure. */ if(chirp_reli_default_nreps>0) { chirp_client_setrep(c,"@@@",chirp_reli_default_nreps,stoptime); } hash_table_insert(table,host,c); return c; } else { return 0; } }
int main(int argc, char *argv[]) { hash_table_t *H; char word[WORDLEN+1]; char *new; int htab_size=HTAB_DEFAULT; int i, dwords=0, nwords=0, max, maxloc, min, mincount; int found; srand(86421357); if (argc>1) { htab_size = atoi(argv[1]); } /* create the empty hash table */ H = hash_table_create(htab_size); while (getword(word, WORDLEN) != EOF) { nwords += 1; /* search the hash table */ found = (hash_table_search(H, word, strcmp) != NULL); if (!found) { /* duplicate the string and insert it */ new = malloc(strlen(word)+1); assert(new); strcpy(new, word); H = hash_table_insert(H, new); dwords += 1; } }
struct hash_table *chirp_audit(const char *path) { struct hash_table *table; time_t stop, start; int result; /* An audit can be time consuming and resource intensive, so run the audit at a low priority so as not to hurt anyone else. */ nice(10); table = hash_table_create(0, 0); if(!table) return 0; audit_count = 0; start = time(0); debug(D_LOCAL, "audit: starting"); result = chirp_audit_recursive(path, table); stop = time(0); if(stop == start) stop++; debug(D_LOCAL, "audit: completed %d items in %d seconds (%d items/sec)", audit_count, (int) (stop - start), audit_count / (stop - start)); if(result < 0) { chirp_audit_delete(table); return 0; } else { return table; } }
bool tmpfs_init(void) { if (!hash_table_create(&nodes, NODES_BUCKETS, 2, &nodes_ops)) return false; return true; }
struct maildir_keywords * maildir_keywords_init_readonly(struct mailbox *box) { struct maildir_keywords *mk; const char *dir; if (mailbox_get_path_to(box, MAILBOX_LIST_PATH_TYPE_CONTROL, &dir) <= 0) i_unreached(); mk = i_new(struct maildir_keywords, 1); mk->storage = box->storage; mk->path = i_strconcat(dir, "/" MAILDIR_KEYWORDS_NAME, NULL); mk->pool = pool_alloconly_create("maildir keywords", 512); i_array_init(&mk->list, MAILDIR_MAX_KEYWORDS); hash_table_create(&mk->hash, mk->pool, 0, strcase_hash, strcasecmp); mk->dotlock_settings.use_excl_lock = box->storage->set->dotlock_use_excl; mk->dotlock_settings.nfs_flush = box->storage->set->mail_nfs_storage; mk->dotlock_settings.timeout = mail_storage_get_lock_timeout(box->storage, KEYWORDS_LOCK_STALE_TIMEOUT + 2); mk->dotlock_settings.stale_timeout = KEYWORDS_LOCK_STALE_TIMEOUT; mk->dotlock_settings.temp_prefix = mailbox_list_get_temp_prefix(box->list); return mk; }
int ast_program_execute( struct ast_group *program, time_t stoptime ) { struct ast_group *g; struct ast_function *f, *old; /* First, fill up the function table with all of the functions in this entire syntax tree. */ ftable = hash_table_create(127,hash_string); if(!ftable) ftsh_fatal(0,"out of memory"); for( g=program; g; g=g->next ) { if(g->command->type==AST_COMMAND_FUNCTION) { f = g->command->u.function; old = hash_table_remove(ftable,f->name->text); if(old) { ftsh_error(FTSH_ERROR_SYNTAX,f->function_line,"function %s is defined twice (first at line %d)",f->name->text,old->function_line); return 0; } if(!hash_table_insert(ftable,f->name->text,f)) { ftsh_fatal(f->function_line,"out of memory"); } } } return ast_group_execute(program,stoptime); }
struct deltadb * deltadb_create() { struct deltadb *db = malloc(sizeof(*db)); db->table = hash_table_create(0,0); db->ndeltadb_reductions = 0; return db; }
static void stats_top(const char *path, const char *sort_type) { struct top_context ctx; memset(&ctx, 0, sizeof(ctx)); ctx.path = path; ctx.fd = doveadm_connect(path); ctx.prev_pool = pool_alloconly_create("stats top", 1024*16); ctx.cur_pool = pool_alloconly_create("stats top", 1024*16); i_array_init(&ctx.lines, 128); hash_table_create(&ctx.sessions, default_pool, 0, str_hash, strcmp); net_set_nonblock(ctx.fd, FALSE); ctx.input = i_stream_create_fd(ctx.fd, (size_t)-1, TRUE); if (strstr(sort_type, "cpu") != NULL) ctx.lines_sort = sort_cpu; else ctx.lines_sort = sort_num; ctx.sort_type = sort_type; stats_top_start(&ctx); i_stream_destroy(&ctx.input); hash_table_destroy(&ctx.sessions); array_free(&ctx.lines); pool_unref(&ctx.prev_pool); pool_unref(&ctx.cur_pool); i_close_fd(&ctx.fd); }
void chirp_stats_summary(char *buf, int length) { int chunk; char *addr; struct chirp_stats *s; if(!stats_table) stats_table = hash_table_create(0, 0); chunk = snprintf(buf, length, "bytes_written %" PRIu64 "\nbytes_read %" PRIu64 "\ntotal_ops %" PRIu64 "\n", total_bytes_written, total_bytes_read, total_ops); length -= chunk; buf += chunk; chunk = snprintf(buf, length, "clients "); length -= chunk; buf += chunk; hash_table_firstkey(stats_table); while(hash_table_nextkey(stats_table, &addr, (void **) &s)) { chunk = snprintf(buf, length, "%s,1,1,%" PRIu64 ",%" PRIu64 ",%" PRIu64 "; ", s->addr, s->ops, s->bytes_read, s->bytes_written); buf += chunk; length -= chunk; } snprintf(buf, length, "\n"); }
int grow_hash(struct hash_table *hash_tbl) { int i, hash_size, index; struct hashlist_element *element, *oldnext; struct hash_table *new_table; void *key; hash_size = 2*hash_tbl->hash_size; new_table = hash_table_create(hash_size, hash_tbl->hash_function, hash_tbl->find_hashkey, hash_tbl->compare_hashkey); if (!new_table) { dprintf(LOG_ERR, "couldn't grow hash table"); return (-1); } for (i = 0; i < hash_tbl->hash_size; i++) { element = hash_tbl->hash_list[i]; while (element) { key = hash_tbl->find_hashkey(element->data); index = new_table->hash_function(key) % hash_size; oldnext = element->next; element->next = new_table->hash_list[index]; new_table->hash_list[index] = element; new_table->hash_count++; element = oldnext; } } free(hash_tbl->hash_list); hash_tbl->hash_count = new_table->hash_count; hash_tbl->hash_size = hash_size; hash_tbl->hash_list = new_table->hash_list; free(new_table); return 0; }
int main(int argc, char* argv[]) { int i = 0; int n = 10000; int ret_data = 0; HashTable* hash_table = hash_table_create(NULL, NULL, hash_int, 31); for(i = 0; i < n; i++) { assert(hash_table_length(hash_table) == i); assert(hash_table_insert(hash_table, (void*)i) == RET_OK); assert(hash_table_length(hash_table) == (i + 1)); assert(hash_table_find(hash_table, cmp_int, (void*)i, (void**)&ret_data) == RET_OK); assert(ret_data == i); } for(i = 0; i < n; i++) { assert(hash_table_delete(hash_table, cmp_int, (void*)i) == RET_OK); assert(hash_table_length(hash_table) == (n - i -1)); assert(hash_table_find(hash_table, cmp_int, (void*)i, (void**)&ret_data) != RET_OK); } hash_table_destroy(hash_table); return 0; }
void mail_sessions_init(void) { session_guid_warn_hide_until = ioloop_time + SESSION_GUID_WARN_HIDE_SECS; hash_table_create(&mail_sessions_hash, default_pool, 0, guid_128_hash, guid_128_cmp); }
struct nvpair *nvpair_create() { struct nvpair *n; n = xxmalloc(sizeof(*n)); n->table = hash_table_create(7, hash_string); return n; }
static int server_table_load(time_t stoptime) { struct catalog_query *q; struct jx *j; char *key; void *item; if((last_update + update_interval) > time(0)) { return 1; } if(!server_table) { server_table = hash_table_create(0, 0); if(!server_table) return 0; } if(inhibit_catalog_queries) { debug(D_CHIRP, "catalog queries disabled\n"); return 1; } hash_table_firstkey(server_table); while(hash_table_nextkey(server_table, &key, &item)) { hash_table_remove(server_table, key); jx_delete(item); } debug(D_CHIRP, "querying catalog at %s:%d", CATALOG_HOST, CATALOG_PORT); q = catalog_query_create(CATALOG_HOST, CATALOG_PORT, stoptime); if(!q) return 0; while((j = catalog_query_read(q, stoptime))) { char name[CHIRP_PATH_MAX]; const char *type, *hname; int port; type = jx_lookup_string(j, "type"); if(type && !strcmp(type, "chirp")) { hname = jx_lookup_string(j, "name"); if(hname) { port = jx_lookup_integer(j, "port"); if(!port) port = CHIRP_PORT; sprintf(name, "%s:%d", hname, port); hash_table_insert(server_table, name, j); } else { jx_delete(j); } } else { jx_delete(j); } } catalog_query_delete(q); last_update = time(0); return 1; }
int main(int argc, char *argv[]) { struct timeval start_time, stop_time; size_t i; int test_size = atoi(argv[1]); int hash_table_size = atoi(argv[2]); void** tmp = malloc(test_size * sizeof(void*)); assert(tmp != NULL); HashTable* hash_table = hash_table_create(hash_table_size); printf("%d put operations .........................: ", test_size); fflush(stdout); START_TIMER(start_time); for (i = 0; i < test_size; ++i) { tmp[i] = malloc(sizeof(void)); assert(tmp[i] != NULL); hash_table_insert(hash_table, (void*) tmp[i]); } STOP_TIMER(stop_time); printf("%.3f milisecs\n", TIME_DIFF(start_time, stop_time) / 1000); printf("Iterating .........................: "); fflush(stdout); START_TIMER(start_time); HashTableIterator* it = hash_table_iterator_create(hash_table); void* e = hash_table_iterator_next(it); while (e) { e = hash_table_iterator_next(it); } STOP_TIMER(stop_time); printf("done in %.3f milisecs\n", TIME_DIFF(start_time, stop_time) / 1000); return 1; }
struct mail_index *mail_index_alloc(const char *dir, const char *prefix) { struct mail_index *index; index = i_new(struct mail_index, 1); index->dir = i_strdup(dir); index->prefix = i_strdup(prefix); index->fd = -1; index->extension_pool = pool_alloconly_create(MEMPOOL_GROWING"index extension", 1024); p_array_init(&index->extensions, index->extension_pool, 5); i_array_init(&index->sync_lost_handlers, 4); i_array_init(&index->module_contexts, I_MIN(5, mail_index_module_register.id)); index->mode = 0600; index->gid = (gid_t)-1; index->lock_method = FILE_LOCK_METHOD_FCNTL; index->max_lock_timeout_secs = UINT_MAX; index->keywords_ext_id = mail_index_ext_register(index, MAIL_INDEX_EXT_KEYWORDS, 128, 2, 1); index->keywords_pool = pool_alloconly_create("keywords", 512); i_array_init(&index->keywords, 16); hash_table_create(&index->keywords_hash, index->keywords_pool, 0, strcase_hash, strcasecmp); index->log = mail_transaction_log_alloc(index); mail_index_modseq_init(index); return index; }
/** Basic initialization of the driver. * * This is only needed to create the hash table * for storing open nodes. * * @return Error code * */ int ext4fs_global_init(void) { if (!hash_table_create(&open_nodes, 0, 0, &open_nodes_ops)) return ENOMEM; return EOK; }