/* initialize pptp daemon */ int pptpd_init(pptpd *_this) { int i, m; struct sockaddr_in sin0; uint16_t call0, call[UINT16_MAX - 1]; memset(_this, 0, sizeof(pptpd)); _this->id = pptpd_seqno++; slist_init(&_this->listener); memset(&sin0, 0, sizeof(sin0)); sin0.sin_len = sizeof(sin0); sin0.sin_family = AF_INET; if (pptpd_add_listener(_this, 0, PPTPD_DEFAULT_LAYER2_LABEL, (struct sockaddr *)&sin0) != 0) { return 1; } _this->ip4_allow = NULL; slist_init(&_this->ctrl_list); slist_init(&_this->call_free_list); /* randomize call id */ for (i = 0; i < countof(call) ; i++) call[i] = i + 1; for (i = countof(call); i > 1; i--) { m = random() % i; call0 = call[m]; call[m] = call[i - 1]; call[i - 1] = call0; } for (i = 0; i < MIN(PPTP_MAX_CALL, countof(call)); i++) slist_add(&_this->call_free_list, (void *)(uintptr_t)call[i]); slist_add(&_this->call_free_list, (void *)PPTPD_SHUFFLE_MARK); if (_this->call_id_map == NULL) _this->call_id_map = hash_create(pptp_call_cmp, pptp_call_hash, 0); return 0; }
static result_t pickle__test2_read(void) { result_t err; hash_t *d; printf("test: create hash\n"); err = hash_create(NULL, 0, NULL, NULL, cheese_key_destroy, cheese_value_destroy, &d); if (err) goto Failure; printf("test: unpickle\n"); err = pickle_unpickle(FILENAME, d, &pickle_writer_hash, &unformat_cheese_methods, NULL); if (err) goto Failure; printf("test: iterate\n"); hash_walk(d, my_walk_fn, NULL); printf("test: destroy hash\n"); hash_destroy(d); return result_TEST_PASSED; Failure: printf("\n\n*** Error %x\n", err); return result_TEST_FAILED; }
void test_insert(int* errors) { char* location = "test_insert"; hash_table* ht = hash_create(hash_fn, hash_strcmp); char* k1 = "key1"; int64_t v1 = 1; confirm_insert(ht, k1, &v1, NULL, NULL, location, errors); char* k2 = "key2"; int64_t v2 = 2; confirm_insert(ht, k2, &v2, NULL, NULL, location, errors); int64_t v3 = 3; confirm_insert(ht, k1, &v3, k1, &v1, location, errors); hash_destroy(ht, false, false); ht = NULL; }
static inline int Command_parse(struct params *p, Command *cmd) { int next = 0; // TODO: refactor this, but for now command takes over the tokens until it's done memcpy(cmd->tokens, p->tokens, MAX_TOKENS); cmd->token_count = p->token_count; cmd->options = hash_create(HASHCOUNT_T_MAX, 0, 0); check_mem(cmd->options); hash_set_allocator(cmd->options, cmd_hnode_alloc, cmd_hnode_free, NULL); cmd->extra = list_create(LISTCOUNT_T_MAX); check_mem(cmd->extra); next = peek(p); if(next == TKIDENT || next == TKBLOB) { cmd->progname = match(p, next); check(cmd->progname, "No program name given in command."); } else { sentinel("Expected the name of the program you're running not: %s", bdata(match(p, next))); } cmd->name = match(p, TKIDENT); check(cmd->name, "No command name given. Use m2sh help to figure out what's available."); for(next = peek(p); next != -1 && !cmd->error; next = peek(p)) { if(next == TKOPTION) { Command_option(cmd, p); } else { Command_extra(cmd, next, p); } } check(p->curtk == p->token_count, "Didn't parse the whole command line, only: %d of %d", p->curtk, p->token_count); return 0; error: cmd->error = 1; return -1; }
int blockdev_register(struct inode *node, struct blockctl *ctl) { struct blockdev *bd = kmalloc(sizeof(struct blockdev)); mutex_create(&ctl->cachelock, 0); hash_create(&ctl->cache, 0, 0x4000); mpscq_create(&ctl->queue, 1000); bd->ctl = ctl; int num = atomic_fetch_add(&next_minor, 1); node->devdata = bd; node->phys_dev = GETDEV(block_major, num); node->kdev = dm_device_get(block_major); kthread_create(&ctl->elevator, "[kelevator]", 0, block_elevator_main, node); char name[64]; snprintf(name, 64, "/dev/bcache-%d", num); kerfs_register_parameter(name, ctl, 0, 0, kerfs_block_cache_report); return num; }
/* * ShmemInitHash -- Create and initialize, or attach to, a * shared memory hash table. * * We assume caller is doing some kind of synchronization * so that two processes don't try to create/initialize the same * table at once. (In practice, all creations are done in the postmaster * process; child processes should always be attaching to existing tables.) * * max_size is the estimated maximum number of hashtable entries. This is * not a hard limit, but the access efficiency will degrade if it is * exceeded substantially (since it's used to compute directory size and * the hash table buckets will get overfull). * * init_size is the number of hashtable entries to preallocate. For a table * whose maximum size is certain, this should be equal to max_size; that * ensures that no run-time out-of-shared-memory failures can occur. * * Note: before Postgres 9.0, this function returned NULL for some failure * cases. Now, it always throws error instead, so callers need not check * for NULL. */ HTAB * ShmemInitHash(const char *name, /* table string name for shmem index */ long init_size, /* initial table size */ long max_size, /* max size of the table */ HASHCTL *infoP, /* info about key and bucket size */ int hash_flags) /* info about infoP */ { bool found; void *location; /* * Hash tables allocated in shared memory have a fixed directory; it can't * grow or other backends wouldn't be able to find it. So, make sure we * make it big enough to start with. * * The shared memory allocator must be specified too. */ infoP->dsize = infoP->max_dsize = hash_select_dirsize(max_size); infoP->alloc = ShmemAlloc; hash_flags |= HASH_SHARED_MEM | HASH_ALLOC | HASH_DIRSIZE; /* look it up in the shmem index */ location = ShmemInitStruct(name, hash_get_shared_size(infoP, hash_flags), &found); /* * if it already exists, attach to it rather than allocate and initialize * new space */ if (found) hash_flags |= HASH_ATTACH; if(strcmp(name, "block lsn") == 0 || strcmp(name, "relation last block") == 0) { ereport(WARNING, (errmsg("%s: %c", name, found+'0'))); } /* Pass location of hashtable header to hash_create */ infoP->hctl = (HASHHDR *) location; return hash_create(name, init_size, infoP, hash_flags); }
/* * Construct an empty TupleHashTable * * numCols, keyColIdx: identify the tuple fields to use as lookup key * eqfunctions: equality comparison functions to use * hashfunctions: datatype-specific hashing functions to use * nbuckets: initial estimate of hashtable size * entrysize: size of each entry (at least sizeof(TupleHashEntryData)) * tablecxt: memory context in which to store table and table entries * tempcxt: short-lived context for evaluation hash and comparison functions * * The function arrays may be made with execTuplesHashPrepare(). Note they * are not cross-type functions, but expect to see the table datatype(s) * on both sides. * * Note that keyColIdx, eqfunctions, and hashfunctions must be allocated in * storage that will live as long as the hashtable does. */ TupleHashTable BuildTupleHashTable(int numCols, AttrNumber *keyColIdx, FmgrInfo *eqfunctions, FmgrInfo *hashfunctions, long nbuckets, Size entrysize, MemoryContext tablecxt, MemoryContext tempcxt) { TupleHashTable hashtable; HASHCTL hash_ctl; Assert(nbuckets > 0); Assert(entrysize >= sizeof(TupleHashEntryData)); /* Limit initial table size request to not more than work_mem */ nbuckets = Min(nbuckets, (long) ((work_mem * 1024L) / entrysize)); hashtable = (TupleHashTable) MemoryContextAlloc(tablecxt, sizeof(TupleHashTableData)); hashtable->numCols = numCols; hashtable->keyColIdx = keyColIdx; hashtable->tab_hash_funcs = hashfunctions; hashtable->tab_eq_funcs = eqfunctions; hashtable->tablecxt = tablecxt; hashtable->tempcxt = tempcxt; hashtable->entrysize = entrysize; hashtable->tableslot = NULL; /* will be made on first lookup */ hashtable->inputslot = NULL; hashtable->in_hash_funcs = NULL; hashtable->cur_eq_funcs = NULL; MemSet(&hash_ctl, 0, sizeof(hash_ctl)); hash_ctl.keysize = sizeof(TupleHashEntryData); hash_ctl.entrysize = entrysize; hash_ctl.hash = TupleHashTableHash; hash_ctl.match = TupleHashTableMatch; hash_ctl.hcxt = tablecxt; hashtable->hashtab = hash_create("TupleHashTable", nbuckets, &hash_ctl, HASH_ELEM | HASH_FUNCTION | HASH_COMPARE | HASH_CONTEXT); return hashtable; }
static void read_all_cnf P1H(void) { string *cnf_files; const_string cnf_path = kpse_init_format (kpse_cnf_format); cnf_hash = hash_create (CNF_HASH_SIZE); for (cnf_files = kpse_all_path_search (cnf_path, CNF_NAME); cnf_files && *cnf_files; cnf_files++) { string line; string cnf_filename = *cnf_files; FILE *cnf_file = xfopen (cnf_filename, FOPEN_R_MODE); while ((line = read_line (cnf_file)) != NULL) { unsigned len = strlen (line); /* Strip trailing spaces. */ while (len > 0 && ISSPACE(line[len-1])) { line[len - 1] = 0; --len; } /* Concatenate consecutive lines that end with \. */ while (len > 0 && line[len - 1] == '\\') { string next_line = read_line (cnf_file); line[len - 1] = 0; if (!next_line) { WARNING1 ("%s: Last line ends with \\", cnf_filename); } else { string new_line; new_line = concat (line, next_line); free (line); line = new_line; len = strlen (line); } } do_line (line); free (line); } xfclose (cnf_file, cnf_filename); } }
void bgp_sync_init (struct peer *peer) { afi_t afi; safi_t safi; struct bgp_synchronize *sync; for (afi = AFI_IP; afi < AFI_MAX; afi++) for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) { sync = XCALLOC (MTYPE_BGP_SYNCHRONISE, sizeof (struct bgp_synchronize)); FIFO_INIT (&sync->update); FIFO_INIT (&sync->withdraw); FIFO_INIT (&sync->withdraw_low); peer->sync[afi][safi] = sync; peer->hash[afi][safi] = hash_create (baa_hash_key, baa_hash_cmp); } }
/*************************************************************//** Creates a hash table with at least n array cells. The actual number of cells is chosen to be a prime number slightly bigger than n. @return own: created table */ UNIV_INTERN hash_table_t* ha_create_func( /*===========*/ ulint n, /*!< in: number of array cells */ #ifdef UNIV_SYNC_DEBUG ulint mutex_level, /*!< in: level of the mutexes in the latching order: this is used in the debug version */ #endif /* UNIV_SYNC_DEBUG */ ulint n_mutexes) /*!< in: number of mutexes to protect the hash table: must be a power of 2, or 0 */ { hash_table_t* table; ulint i; ut_ad(ut_is_2pow(n_mutexes)); table = hash_create(n); #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG table->adaptive = TRUE; #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */ /* Creating MEM_HEAP_BTR_SEARCH type heaps can potentially fail, but in practise it never should in this case, hence the asserts. */ if (n_mutexes == 0) { table->heap = mem_heap_create_in_btr_search( ut_min(4096, MEM_MAX_ALLOC_IN_BUF)); ut_a(table->heap); return(table); } hash_create_mutexes(table, n_mutexes, mutex_level); table->heaps = mem_alloc(n_mutexes * sizeof(void*)); for (i = 0; i < n_mutexes; i++) { table->heaps[i] = mem_heap_create_in_btr_search(4096); ut_a(table->heaps[i]); } return(table); }
bool init_timezone_hashtable(void) { HASHCTL hash_ctl; MemSet(&hash_ctl, 0, sizeof(hash_ctl)); hash_ctl.keysize = TZ_STRLEN_MAX + 1; hash_ctl.entrysize = sizeof(pg_tz_cache); timezone_cache = hash_create("Timezones", 4, &hash_ctl, HASH_ELEM); if (!timezone_cache) return false; return true; }
hashmap * _p11_conf_load_modules (int mode, const char *system_dir, const char *user_dir) { hashmap *configs; char *path; int error = 0; /* A hash table of name -> config */ configs = hash_create (hash_string_hash, hash_string_equal, free, (hash_destroy_func)hash_free); /* Load each user config first, if user config is allowed */ if (mode != CONF_USER_NONE) { path = expand_user_path (user_dir); if (!path) error = errno; else if (load_configs_from_directory (path, configs) < 0) error = errno; free (path); if (error != 0) { hash_free (configs); errno = error; return NULL; } } /* * Now unless user config is overriding, load system modules. * Basically if a value for the same config name is not already * loaded above (in the user configs) then they're loaded here. */ if (mode != CONF_USER_ONLY) { if (load_configs_from_directory (system_dir, configs) < 0) { error = errno; hash_free (configs); errno = error; return NULL; } } return configs; }
/* * init relid to in-memory table mapping */ void InitOidInMemHeapMapping(long initSize, MemoryContext memcxt, InMemMappingType mappingType) { HASHCTL info; Assert(mappingType < INMEM_MAPPINGS_SIZE); Assert(NULL == OidInMemMappings[mappingType]); info.hcxt = memcxt; info.hash = oid_hash; info.keysize = sizeof(Oid); info.entrysize = sizeof(struct OidInMemHeapMappingEntry); OidInMemMappings[mappingType] = hash_create(InMemMappingNames[mappingType], initSize, &info, HASH_CONTEXT | HASH_FUNCTION | HASH_ELEM); Assert(NULL != OidInMemMappings[mappingType]); InMemMappingCxt[mappingType] = memcxt; }
/* * Initialize config cache and prepare callbacks. This is split out of * lookup_ts_config_cache because we need to activate the callback before * caching TSCurrentConfigCache, too. */ static void init_ts_config_cache(void) { HASHCTL ctl; MemSet(&ctl, 0, sizeof(ctl)); ctl.keysize = sizeof(Oid); ctl.entrysize = sizeof(TSConfigCacheEntry); TSConfigCacheHash = hash_create("Tsearch configuration cache", 16, &ctl, HASH_ELEM | HASH_BLOBS); /* Flush cache on pg_ts_config and pg_ts_config_map changes */ CacheRegisterSyscacheCallback(TSCONFIGOID, InvalidateTSCacheCallBack, PointerGetDatum(TSConfigCacheHash)); CacheRegisterSyscacheCallback(TSCONFIGMAP, InvalidateTSCacheCallBack, PointerGetDatum(TSConfigCacheHash)); /* Also make sure CacheMemoryContext exists */ if (!CacheMemoryContext) CreateCacheMemoryContext(); }
int main(int argc, char **argv) { chdir(BBSHOME); hash_t stat; hash_create(&stat, 0, top_hash); load_stat(&stat, DAY); FILE *fp = fopen(BBSHOME"/.BOARDS", "r"); if (fp) { struct boardheader board; while (fread(&board, sizeof(board), 1, fp) == 1) { if (board.filename[0] != '\0') process(&stat, &board); } fclose(fp); } else { return EXIT_FAILURE; } top_t **tops = sort_stat(&stat); if (tops) { print_stat(&stat, tops, DAY); save_stat(&stat, tops, DAY); } time_t now = time(NULL); struct tm *t = localtime(&now); if (t->tm_hour == 3 && t->tm_min < 10) { merge_stat(&stat, WEEK); merge_stat(&stat, MONTH); merge_stat(&stat, YEAR); if (t->tm_wday == 0) remove_stat(WEEK); if (t->tm_mday == 1) remove_stat(MONTH); if (t->tm_yday == 0) remove_stat(YEAR); } return EXIT_SUCCESS; }
/* * Allocate a new Local MDVSN data structure in the Current Memory Context * */ static mdver_local_mdvsn * mdver_local_mdvsn_create_hashtable(const char *htable_name) { mdver_local_mdvsn *new_local_mdvsn = palloc0(sizeof(mdver_local_mdvsn)); HASHCTL ctl; ctl.keysize = sizeof(Oid); ctl.entrysize = sizeof(mdver_entry); ctl.hash = oid_hash; HTAB *htable = hash_create(htable_name, MDVER_LOCAL_MDVSN_NBUCKETS, &ctl, HASH_ELEM | HASH_FUNCTION); new_local_mdvsn->htable = htable; new_local_mdvsn->nuke_happened = false; return new_local_mdvsn; }
/* * EnablePortalManager * Enables the portal management module at backend startup. */ void EnablePortalManager(void) { HASHCTL ctl; Assert(PortalMemory == NULL); PortalMemory = AllocSetContextCreate(TopMemoryContext, "PortalMemory", ALLOCSET_DEFAULT_SIZES); ctl.keysize = MAX_PORTALNAME_LEN; ctl.entrysize = sizeof(PortalHashEnt); /* * use PORTALS_PER_USER as a guess of how many hash table entries to * create, initially */ PortalHashTable = hash_create("Portal hash", PORTALS_PER_USER, &ctl, HASH_ELEM); }
static void test_hash_delete() { struct _hash * h = hash_create( int_hash, int_cmp, sizeof(int), sizeof(int), 1); int key=0; int item; int item0=0; hash_add(h, &key, &item0); assert( hash_get(h, &key, &item) != NULL ); assert( item == 0 ); hash_delete(h, &key, &item0); assert( hash_get(h, &key, &item) == NULL ); assert ( h->n_items == 0 ); hash_remove(h); }
void bdd_output_candidate(bdd_node_t *n, list_t *vars) { hash_t *used; bdd_node_t *cur; listentry_t *ve; used = hash_create(VAR_HASH_SIZE); #ifdef VERBOSE printf("Tracing solution:\n"); #endif while ((cur = n->parent) != NULL) { #ifdef VERBOSE printf( "n: %p(\"%s\") -> (%p,%p); parent: %p(\"%s\") -> (%p,%p)\n", n, n->var, n->iszero, n->isone, cur, cur->var, cur->iszero, cur->isone ); #endif if (cur->iszero == n) hash_put(used, cur->var, no.var, 2); else if (cur->isone == n) hash_put(used, cur->var, yes.var, 2); else abort(); n = cur; } ve = vars->first; while (ve != NULL) { char *res; res = hash_get(used, (char *)ve->element); if (res == NULL) printf("*"); else printf("%c", res[0]); ve = ve->next; if (ve != NULL) printf(" "); else printf("\n"); } hash_destroy(used); }
void _PG_init() { HASHCTL ctl; MemoryContext oldctx = MemoryContextSwitchTo(CacheMemoryContext); Py_Initialize(); RegisterXactCallback(multicorn_xact_callback, NULL); #if PG_VERSION_NUM >= 90300 RegisterSubXactCallback(multicorn_subxact_callback, NULL); #endif /* Initialize the global oid -> python instances hash */ MemSet(&ctl, 0, sizeof(ctl)); ctl.keysize = sizeof(Oid); ctl.entrysize = sizeof(CacheEntry); ctl.hash = oid_hash; ctl.hcxt = CacheMemoryContext; InstancesHash = hash_create("multicorn instances", 32, &ctl, HASH_ELEM | HASH_FUNCTION); MemoryContextSwitchTo(oldctx); }
void AppendOnlyMirrorResyncEofs_HashTableInit(void) { HASHCTL info; int hash_flags; /* Set key and entry sizes. */ MemSet(&info, 0, sizeof(info)); info.keysize = sizeof(AppendOnlyMirrorResyncEofsKey); info.entrysize = sizeof(AppendOnlyMirrorResyncEofs); info.hash = tag_hash; info.hcxt = TopMemoryContext; hash_flags = (HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT); AppendOnlyMirrorResyncEofsTable = hash_create("AO Mirror Resync EOFs", 10, &info, hash_flags); if (Debug_persistent_print || Debug_persistent_appendonly_commit_count_print) elog(Persistent_DebugPrintLevel(), "Storage Manager: Append-Only mirror resync eofs hash-table created"); }
ekhtml_parser_t *ekhtml_parser_new(void *cbdata){ ekhtml_parser_t *res; res = malloc(sizeof(*res)); res->datacb = NULL; res->startendcb = hash_create(HASHCOUNT_T_MAX, ekhtml_string_comp, ekhtml_string_hash); res->cbdata = cbdata; res->startcb_unk = NULL; res->endcb_unk = NULL; res->commentcb = NULL; res->buf = NULL; res->nalloced = 0; res->nbuf = 0; res->freeattrs = NULL; res->state.state = EKHTML_STATE_NONE; res->state.state_data = NULL; /* Start out with a buffer of 1 block size */ ekhtml_buffer_grow(res); return res; }
int create_env_hash_table(char ** env, hash_table_t ** table_out) { hash_table_t *local_table = NULL; hash_key_t key; hash_value_t value; char * tmp; char **ui; int err_h; err_h = hash_create((unsigned long)INIT_ENV_TABLE_SIZE, &local_table, delete_callback, NULL); if (err_h != HASH_SUCCESS) { fprintf(stderr, "couldn't create hash table (%s)\n", hash_error_string(err_h)); return err_h; } for(ui = (char **) msg.user_env; *ui!=NULL; ui++) { tmp = strchr(*ui,'='); *tmp = '\0'; key.type = HASH_KEY_STRING; key.str = strdup(*ui); value.type = HASH_VALUE_PTR; value.ptr = tmp+1; if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "couldn't add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } *tmp = '=' ; } *table_out = local_table; return HASH_SUCCESS; }
void build_hash() { /* create a hash table */ hash_create(gc, &hash_string, &hash_string_cmp, &hash); /* push values into the hash */ for(int i=0; i <10; i++) { /* an ineffcient means of doing this */ gc_alloc(gc, 0, 40, (void **)&key1); gc_alloc(gc, 0, 40, (void **)&value); snprintf(key1, 40, "k%i", i); snprintf(value, 40, "v%i", i); hash_set(hash, (void*)key1, (void*)value); } }
/* * InitializeTableSpaceCache * Initialize the tablespace cache. */ static void InitializeTableSpaceCache(void) { HASHCTL ctl; /* Initialize the hash table. */ MemSet(&ctl, 0, sizeof(ctl)); ctl.keysize = sizeof(Oid); ctl.entrysize = sizeof(TableSpaceCacheEntry); TableSpaceCacheHash = hash_create("TableSpace cache", 16, &ctl, HASH_ELEM | HASH_BLOBS); /* Make sure we've initialized CacheMemoryContext. */ if (!CacheMemoryContext) CreateCacheMemoryContext(); /* Watch for invalidation events. */ CacheRegisterSyscacheCallback(TABLESPACEOID, InvalidateTableSpaceCacheCallback, (Datum) 0); }
// // create_heap // HEAP *create_heap( int max_size, // max size of heap; can grow if max_size < 0 int (*compare)(void *p1, void *p2), // function to compare two objects void *(*copy)(void *p), // function to copy an objects void (*destroy)(void *p), // function to destroy an object /* The function get_key must return an ascii character string. Note that this * module (heap.c) does not perform an memory managment on that string * pointer; at no point in this module is the memory associated with that * string freed. */ char *(*get_key)(void *p), // function to get an object string key void (*print)(FILE *f, void *p) // function to print nodes ) { int n; HEAP *heap = NULL; Resize(heap, 1, HEAP); heap->node_list = NULL; // a list containing nodes of the heap heap->max_size = max_size; n = (max_size < 0) ? HEAP_CHUNK : max_size + 1; // heap size is predefined or can grow Resize(heap->node_list, n, void *); heap->cur_size = n; heap->next_node = 1; // index of root node heap->compare = compare; // heap order function heap->copy = copy; // function to copy two ojects heap->destroy = destroy; // function to destroy ojects heap->get_key = get_key; heap->print = print; // function to print nodes // HASH_SIZE was 100 but I changed to 101 to make it prime // as prime numbers have better resistance to hash collisions #define HASH_SIZE 101 if (heap->get_key != NULL) { heap->ht = hash_create(HASH_SIZE); } else { heap->ht = NULL; } return (heap); } // create_heap
/* * Callback for loading a hash node. */ int pdb_load_hash_node_cb(FILE* fptr, struct pdb_node_t* pptr, char** tok_arr, int* line) { char b; int i; struct pdb_node_types_t* tiptr = pdb_get_type_info(HASH_NODE_TYPE); /* * Move to the next non-white character. */ while (!feof(fptr)) { FPEEK(&b, fptr); if (!is_whitespace(b)) break; if (b == '\n') *line++; fgetc(fptr); } /* * Skip over the opening token. */ for (i = 0; i < (signed)strlen(tiptr->open_token); ++i) fgetc(fptr); /* * Get the size of the hash table. */ if (!fscanf(fptr, "%i;", &i)) { char* e = va(NULL, "Unable to load hash from file -- no size (line %i).\n", *line); ERROR(e); free(e); return 0; } pptr->data = (void*)hash_create(i); return pdb_standard_load_node(fptr, pptr, tok_arr, line); }
/* * Construct an empty TupleHashTable * * numCols, keyColIdx: identify the tuple fields to use as lookup key * eqfunctions: equality comparison functions to use * hashfunctions: datatype-specific hashing functions to use * nbuckets: initial estimate of hashtable size * entrysize: size of each entry (at least sizeof(TupleHashEntryData)) * tablecxt: memory context in which to store table and table entries * tempcxt: short-lived context for evaluation hash and comparison functions * * The function arrays may be made with execTuplesHashPrepare(). * * Note that keyColIdx, eqfunctions, and hashfunctions must be allocated in * storage that will live as long as the hashtable does. */ TupleHashTable BuildTupleHashTable(int numCols, AttrNumber *keyColIdx, FmgrInfo *eqfunctions, FmgrInfo *hashfunctions, int nbuckets, Size entrysize, MemoryContext tablecxt, MemoryContext tempcxt) { TupleHashTable hashtable; HASHCTL hash_ctl; Assert(nbuckets > 0); Assert(entrysize >= sizeof(TupleHashEntryData)); hashtable = (TupleHashTable) MemoryContextAlloc(tablecxt, sizeof(TupleHashTableData)); hashtable->numCols = numCols; hashtable->keyColIdx = keyColIdx; hashtable->eqfunctions = eqfunctions; hashtable->hashfunctions = hashfunctions; hashtable->tablecxt = tablecxt; hashtable->tempcxt = tempcxt; hashtable->entrysize = entrysize; hashtable->tableslot = NULL; /* will be made on first lookup */ hashtable->inputslot = NULL; MemSet(&hash_ctl, 0, sizeof(hash_ctl)); hash_ctl.keysize = sizeof(TupleHashEntryData); hash_ctl.entrysize = entrysize; hash_ctl.hash = TupleHashTableHash; hash_ctl.match = TupleHashTableMatch; hash_ctl.hcxt = tablecxt; hashtable->hashtab = hash_create("TupleHashTable", (long) nbuckets, &hash_ctl, HASH_ELEM | HASH_FUNCTION | HASH_COMPARE | HASH_CONTEXT); return hashtable; }
static void init_lwlock_stats(void) { HASHCTL ctl; static MemoryContext lwlock_stats_cxt = NULL; static bool exit_registered = false; if (lwlock_stats_cxt != NULL) MemoryContextDelete(lwlock_stats_cxt); /* * The LWLock stats will be updated within a critical section, which * requires allocating new hash entries. Allocations within a critical * section are normally not allowed because running out of memory would * lead to a PANIC, but LWLOCK_STATS is debugging code that's not normally * turned on in production, so that's an acceptable risk. The hash entries * are small, so the risk of running out of memory is minimal in practice. */ lwlock_stats_cxt = AllocSetContextCreate(TopMemoryContext, "LWLock stats", ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE); MemoryContextAllowInCriticalSection(lwlock_stats_cxt, true); MemSet(&ctl, 0, sizeof(ctl)); ctl.keysize = sizeof(lwlock_stats_key); ctl.entrysize = sizeof(lwlock_stats); ctl.hash = tag_hash; ctl.hcxt = lwlock_stats_cxt; lwlock_stats_htab = hash_create("lwlock stats", 16384, &ctl, HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT); if (!exit_registered) { on_shmem_exit(print_lwlock_stats, 0); exit_registered = true; } }
/* * find_oper_cache_entry * * Look for a cache entry matching the given key. If found, return the * contained operator OID, else return InvalidOid. */ static Oid find_oper_cache_entry(OprCacheKey *key) { OprCacheEntry *oprentry; if (OprCacheHash == NULL) { /* First time through: initialize the hash table */ HASHCTL ctl; if (!CacheMemoryContext) CreateCacheMemoryContext(); MemSet(&ctl, 0, sizeof(ctl)); ctl.keysize = sizeof(OprCacheKey); ctl.entrysize = sizeof(OprCacheEntry); ctl.hash = tag_hash; OprCacheHash = hash_create("Operator lookup cache", 256, &ctl, HASH_ELEM | HASH_FUNCTION); /* Arrange to flush cache on pg_operator and pg_cast changes */ CacheRegisterSyscacheCallback(OPERNAMENSP, InvalidateOprCacheCallBack, (Datum) 0); CacheRegisterSyscacheCallback(CASTSOURCETARGET, InvalidateOprCacheCallBack, (Datum) 0); } /* Look for an existing entry */ oprentry = (OprCacheEntry *) hash_search(OprCacheHash, (void *) key, HASH_FIND, NULL); if (oprentry == NULL) return InvalidOid; return oprentry->opr_oid; }