ht_intset_t* ht_new() { ht_intset_t *set; int i; if ((set = (ht_intset_t*) ht_alloc(sizeof(ht_intset_t))) == NULL) { perror("malloc"); exit(1); } set->hash = maxhtlength - 1; set->buckets = ht_alloc((maxhtlength + 1) * sizeof(intset_t)); if (set->buckets == NULL) { perror("malloc buckets"); exit(1); } for (i=0; i < maxhtlength; i++) { bucket_set_init(&set->buckets[i]); } return set; }
/* * Expand an existing hash table. */ static void ht_expand(Atom a) { Ht ht, newh; int n; Atom p, *l; ht = a->ht; n = ht->htsz * 2; newh = a->ht = ht_alloc(n, ht->nbits); #if 0 printf("expanding from %d to %d, ent = %d, coll = %d, nbits = %d\n", ht->htsz, n, ht->ent, ht->coll, ht->nbits); #endif newh->ent = ht->ent; newh->nterm = ht->nterm; newh->term = ht->term; for (p = a->kids.lh_first; p; p = p->sibs.le_next) { l = &newh->ht[hash(newh, p->ir.u.eq.val)]; if((p->next = *l)) newh->coll++; *l = p; } free((void *)ht); }
TEST_F(TestAlloc, ResettingCustomAllocatorShouldNotFail) { // Arrange ht_allocator_set(test_realloc_function, nullptr); // Act ht_allocator_set(nullptr, nullptr); // Assert // Following instructions should not fail; void* ptr = ht_alloc(30); ht_free(ptr); }
TEST_F(TestAlloc, SettingCustomAllocatorShouldChangeAlloc) { // Arrange TestAllocInfo info; ht_allocator_set(test_realloc_function, &info); // Act ht_alloc(10); // Assert ASSERT_EQ(nullptr, info.ptr); ASSERT_EQ(10u, info.size); }
int main() { // allocate a hash table to translate executable -> full path specification hash h = ht_alloc(997); // get path: char *path = getenv("PATH"); /* getenv(3) */ char cmd[256], name[1024], basename[256]; char *dirname; DIR *dir; // split path members while ((dirname = strsep(&path,":"))) { /* strsep(3) */ // open directory file dir = opendir(dirname); /* opendir(3) */ if (dir) { struct dirent *de; /* dirent(5) */ while ((de = readdir(dir))) { /* readdir(3) */ int type = de->d_type; // check regular files.... if (type & DT_REG) { strcpy(name,dirname); /* strcpy(3) */ strcat(name,"/"); /* strcat(3) */ strcpy(basename,de->d_name); strcat(name,basename); // ...that are executable if (0 == access(name,X_OK)) { /* access(2) */ // add to database if they've not been encountered before if (!ht_get(h,basename)) { // enter into table, but: // make copies of key and value to void poisoning ht_put(h,strdup(basename),strdup(name)); /* strdup(3) */ } } } } } } printf("For each executable entered, its path will be printed.\n"); while (1 == scanf("%s",cmd)) { char *e = ht_get(h,cmd); if (e) { printf("%s\n",e); } else { printf("Not found as executable.\n");} } return 0; }
/* Upon the first calls to the functions: out, inp, and rdp We want to make sure the tuplespace (hashTable) has been initialized */ void init_ts(void) { pthread_rwlock_init(&lock, NULL); pthread_cond_init(&tuple_added, NULL); pthread_mutex_init(&signaling_mutex, NULL); tuplespace = ht_alloc(NUM_SHAPES); }