void test_ht_free(void **state) { Hashtable *ht; int i; ht = ht_create(0, uint16_hash, uint16_compare, uint16_copy, uint16_free, uint16_compare, uint16_copy, uint16_free); for (i=0; i<LIMIT; i++) { uint16_t *data; uint16_t *key; data = malloc(sizeof(uint16_t)); *data = i; key = malloc(sizeof(uint16_t)); *key = i; ht_insert(ht, key, data); } ht_free(ht); ht = ht_create(0, uint16_hash, uint16_compare, uint16_copy, uint16_free, uint16_compare, uint16_copy, uint16_free); ht_free(ht); }
/* delete a dictionary */ void dict_free(struct dict *d) { /* free both hash tables */ ht_free(d->ht, d->key_free); if(d->ht_old) ht_free(d->ht_old, d->key_free); free(d); }
void test_ht_search(void **state) { Hashtable *ht; int i; ht = ht_create(0, uint16_hash, uint16_compare, uint16_copy, uint16_free, uint16_compare, uint16_copy, uint16_free); for (i=0; i<LIMIT; i++) { uint16_t *key; uint16_t *data; data = malloc(sizeof(uint16_t)); *data = i; key = malloc(sizeof(uint16_t)); *key = i; ht_insert(ht, key, data); } for (i=0; i<LIMIT; i++) { uint16_t *data; data = ht_search(ht, &i); assert_true(data != NULL); assert_int_equal(*data, i); } i = LIMIT + 1; assert_true(ht_search(ht, &i) == NULL); ht_free(ht); }
rt_public char *partial_retrieve(EIF_INTEGER f_desc, long position, long nb_obj) /* Return `nb_obj' retrieved in file `file_ptr' read at `position'. */ { RT_GET_CONTEXT EIF_GET_CONTEXT char *result; file_descriptor = (int) f_desc; if (lseek (f_desc, position, SEEK_SET) == -1) esys (); current_position = 0; end_of_buffer = 0; rt_kind = BASIC_STORE; rt_kind_version = BASIC_STORE_6_6; rt_kind_properties = 0; result = rt_nmake(nb_obj); /* Retrieve `nb_obj' objects */ ht_free(rt_table); /* Free hash table descriptor */ #ifdef ISE_GC if (nb_recorded) { eif_ostack_npop(&hec_stack, nb_recorded); /* Pop hector records */ nb_recorded = 0; } #endif return result; }
int main(void) { unsigned long al1; unsigned long al2; test_assert(ht_init(&ht)); add(&ht, "AAAAAAAAAAAA", "AAAAAAAAAAAA", 1); add(&ht, "BBBBBBBBBBBB", "BBBBBBBBBBBB", 1); add(&ht, "CCCCCCCCCCCC", "CCCCCCCCCCCC", 1); add(&ht, "DDDDDDDDDDDD", "DDDDDDDDDDDD", 1); add(&ht, "EEEEEEEEEEEE", "EEEEEEEEEEEE", 1); add(&ht, "FFFFFFFFFFFF", "FFFFFFFFFFFF", 1); add(&ht, "HHHHHHHHHHHH", "HHHHHHHHHHHH", 1); add(&ht, "IIIIIIIIIIII", "IIIIIIIIIIII", 1); add(&ht, "JJJJJJJJJJJJ", "JJJJJJJJJJJJ", 1); add(&ht, "KKKKKKKKKKKK", "KKKKKKKKKKKK", 1); add(&ht, "LLLLLLLLLLLL", "LLLLLLLLLLLL", 1); add(&ht, "MMMMMMMMMMMM", "MMMMMMMMMMMM", 1); al1 = count(); test_assert(al1 == 12); ht_clear_ext(&ht, X, 0); al2 = count(); test_assert(al1 == al2); ht_free(&ht); return 0; }
rt_public char *retrieve_all(EIF_INTEGER f_desc, long position) { RT_GET_CONTEXT EIF_GET_CONTEXT /* Return object graph retrieved in file `file_ptr' read at * position. */ char *result; file_descriptor = (int)f_desc; if (lseek(file_descriptor, position, SEEK_SET) == -1) esys(); /* bail out */ current_position = 0; end_of_buffer = 0; rt_kind = BASIC_STORE; rt_kind_version = BASIC_STORE_6_6; rt_kind_properties = 0; result = rt_make(); ht_free(rt_table); /* Free hash table descriptor */ #ifdef ISE_GC if (nb_recorded) { eif_ostack_npop(&hec_stack, nb_recorded); /* Pop hector records */ nb_recorded = 0; } #endif return result; }
int main(int argc, char *argv[]) { if (argc != 3) { printf("Incorrect number of arguments. Run again\n"); return EXIT_FAILURE; } else { if(strcmp(argv[2], ".") == 0 || strcmp(argv[2], "..") == 0){ printf("Error: Cannot utilize parent directory or current working directory as input.\n"); return EXIT_FAILURE; } if(strcmp(argv[2],"") == 0){ printf("Error: Starting file or directory is NULL\n"); return EXIT_FAILURE; } ht = ht_create(); out_path = argv[1]; if(get_Files(argv[2]) == -1) { printf("File read fails. Invalid start input\n"); return EXIT_FAILURE; } } Record** rec_array; rec_array = hash_pull(ht); qsort(rec_array, ht->size, sizeof(Record*), rec_compare); print_json(rec_array); ht_free(ht); free(rec_array); return 0; }
void sm_private_free(sm_private_t my) { if (my) { free(my->all_fds); free(my->server_fds); free(my->send_fds); free(my->recv_fds); free(my->tmp_send_fds); free(my->tmp_recv_fds); free(my->tmp_fail_fds); ht_free(my->fd_to_value); ht_free(my->fd_to_sendq); free(my->tmp_buf); memset(my, 0, sizeof(struct sm_private)); free(my); } }
/** * Frees a compiler object and all associated memory. * compiler: an instance of Compiler. * TODO: a lot of allocations don't have frees yet. These will be added in when * the program structure becomes final. */ void compiler_free(Compiler * compiler) { assert(compiler != NULL); if(compiler->compiledScripts != NULL) { set_free(compiler->compiledScripts); } if(compiler->symTableStk != NULL) { stk_free(compiler->symTableStk); } if(compiler->functionHT != NULL) { HTIter htIterator; ht_iter_get(compiler->functionHT, &htIterator); while(ht_iter_has_next(&htIterator)) { DSValue value; ht_iter_next(&htIterator, NULL, 0, &value, NULL, true); compilerfunc_free(value.pointerVal); } ht_free(compiler->functionHT); } if(compiler->outBuffer != NULL) { buffer_free(compiler->outBuffer); } free(compiler); }
/* * ht_free() */ static void ht_free (HT_T *ht) { if (ht != NULL) { ht_free(ht->next); free(ht); } }
/* search and remove an element */ int ht_rm(struct hashtable *t, void *key) { int ret; unsigned int index; if ((ret = ht_search(t, key, &index)) != HT_FOUND) return ret; return ht_free(t, index); }
/* * ht_destory() */ void ht_cleanup (void) { int i; for (i = 0; i <_bucket_num; i++) { ht_free(_ht[i]); } free(_ht); }
int main(void) { struct hashtable ht; alloc_set_alloc(count_malloc); test_assert(ht_init(&ht)); replace1(&ht); ht_free(&ht); return 0; }
rt_public void run_idr_destroy (void) { RT_GET_CONTEXT idrf_destroy(&idrf); #ifdef EIF_64_BITS if (idr_ref_table) { ht_free (idr_ref_table); } idr_ref_table = NULL; idr_ref_table_counter = 0; #endif }
/* * Add text to text box */ static void l_add_text(struct tbox *tb, const char *str) { char *line, *line_end, *str_cpy; str_cpy = line_end = ht_strdup(str); for (line = str_cpy; line_end != NULL; line = line_end + 1) { line_end = strchr(line, '\n'); if (line_end) *line_end = 0; tbox_line_add(tb, line); } ht_free(str_cpy); }
void test_ht_create(void **state) { Hashtable *ht; expect_assert_failure(ht_create(-1, uint16_hash, uint16_compare, uint16_copy, uint16_free, uint16_compare, uint16_copy, uint16_free)); ht = ht_create(0, uint16_hash, uint16_compare, uint16_copy, uint16_free, uint16_compare, uint16_copy, uint16_free); ht_free(ht); }
SDB_VISIBLE void sdb_free (Sdb* s) { if (!s) return; cdb_free (&s->db); if (s->lock) sdb_unlock (sdb_lockfile (s->dir)); ls_free (s->ns); ht_free (s->ht); if (s->fd != -1) close (s->fd); free (s->ndump); free (s->dir); free (s); }
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); }
void dl_free(dl_t self) { if (self) { dl_private_t my = self->private_state; if (my) { cb_free(my->in); ht_free(my->device_num_to_device_id); memset(my, 0, sizeof(struct dl_private)); free(my); } memset(self, 0, sizeof(struct dl_struct)); free(self); } }
TEST_F(TestAlloc, SettingCustomAllocatorShouldChangeFree) { // Arrange TestAllocInfo info; ht_allocator_set(test_realloc_function, &info); // Act ht_free(&info); // Assert ASSERT_EQ(&info, info.ptr); ASSERT_EQ(0u, info.size); }
int main(void) { printf("*****testing hash table*****\n\n"); htbl* t = ht_new(&good_hash, 10); FILE* cnets2015 = fopen("cnets2015", "r"); while (!feof(cnets2015)) { char* s = alloc_str(256); fgets(s, 256, cnets2015); char* l = trim_newline(s); free(s); if (strlen(l) > 0) { ht_ins(l, t); } free(l); } fclose(cnets2015); ht_show(t); printf("The hash table has %u buckets with %u entries (load factor %lg).\n\n", t->n_buckets, ht_n_entries(t), ht_load_factor(t)); printf("The bucket with the most items in it contains %u items.\n\n", ht_max_bucket(t)); printf("ht_member:\n"); printf("membership of cnet \"aardvark\" : %i\n", ht_member("aardvark", t)); printf("membership of cnet \"borja\" : %i\n", ht_member("borja", t)); printf("\n"); // resize printf("***now resizing***\n"); ht_resize(&t); printf("***resizing complete***\n\n"); // do everything again ht_show(t); printf("The hash table has %u buckets with %u entries (load factor %lg).\n\n", t->n_buckets, ht_n_entries(t), ht_load_factor(t)); printf("The bucket with the most items in it contains %u items.\n\n", ht_max_bucket(t)); printf("ht_member:\n"); printf("membership of cnet \"aardvark\" : %i\n", ht_member("aardvark", t)); printf("membership of cnet \"borja\" : %i\n", ht_member("borja", t)); printf("\n"); ht_free(t); return 0; }
//~~~~~~~~~~~~~~~~~MAIN~~~~~~~~~~~~~~~~~~~~~~~ int main(int argc, char* argv[]){ int debug = 0, flag = 1, arg, numRead = 0; char command, *stream; Line line = line_init( stdin); gen_parse_args( argc, argv, &debug); Htable htable = ht_init( ht_mod_hash); while(flag){ arg = -1; fprintf( stdout, "\ncommand: "); line_read_line( line); stream = get_line( line); sscanf( stream, " %c%n", &command, &numRead); switch( command){ case 'q': flag = 0; break; case 'i': sscanf( stream+numRead, "%d", &arg); if( arg < 0) fprintf( stderr, "MUST BE >= 0\n"); else ht_add( htable, arg, arg, debug); break; case 'd': sscanf( stream+numRead, "%d", &arg); if( arg < 0) fprintf( stderr, "MUST BE >= 0\n"); else{ int pos = ht_mod_hash( htable, arg); node_delete( &((htable->table)[pos]), arg, arg, debug); } break; case 'c': sscanf( stream+numRead, "%d", &arg); if( arg < 0) fprintf( stderr, "MUST BE >= 0\n"); else{ if( ht_exists( htable, arg, debug) ) fprintf( stdout, "The value, %d, exists!\n", arg); else fprintf( stdout, "The value, %d, does NOT exist\n", arg); } break; case 'e': ht_erase( htable); break; case 'r': sscanf( stream+numRead, "%d", &arg); if( arg >= 1) ht_resize( htable, arg, debug); else fprintf( stderr, "Error, size must be >= 1\n"); break; case 'l': ht_list( htable); break; case '\n': break; default: fprintf( stdout, "Sorry, Invalid command\n"); break; } free(stream); } line_free( line); ht_free( htable); return 0; }
/* Add an element, discarding the old if the key already exists */ int ht_replace(struct hashtable *t, void *key, void *data) { int ret; unsigned int index; /* Try to add the element */ ret = ht_add(t, key, data); if (ret == HT_OK || ret != HT_BUSY) return ret; /* It already exists, get the index */ ret = ht_search(t, key, &index); assert(ret == HT_FOUND); /* Remove the old */ ret = ht_free(t, index); assert(ret == HT_OK); /* And add the new */ return ht_add(t, key, data); }
int main(void) { HashTable *ht; ht = ht_create(3); if (ht == NULL) { return 1; } ht_put(ht, "key 1", "val 1"); ht_put(ht, "key 2", "val 2"); ht_put(ht, "key 3", "val 3"); ht_put(ht, "key 4", "val 4"); ht_put(ht, "key 5", "val 5"); ht_put(ht, "key 4", "NEW VAL 4"); print_hashtable(ht); print_str(ht_get(ht, "key 4")); print_str("\n"); ht_free(ht); return 0; }
void json_free(JSON *v) { switch(v->type) { case j_string: case j_number: free(v->value); break; case j_object: { Hash_Tbl *h = v->value; ht_free(h, json_free_member); } break; case j_array: { while(v->value) { JSON *t = v->value; v->value = t->next; json_free(t); } } default: break; } free(v); }
/* transfer K items from the old hash table to the new one. */ static void dict_rehash(struct dict *d) { long k = DICT_REHASH_BATCH_SIZE; struct bucket *b, *next; if(d->ht_old == NULL) { return; } /* transfer old elements to the new HT. */ for(b = d->ht_old->first; b && k--;) { struct bucket *b_new; unsigned long h = d->key_hash(b->k, b->sz); if((b_new = ht_insert(d->ht, h, b->k, b->sz, b->v))) { /* new used slot, add to list. */ ht_record_used_bucket(d->ht, b_new); } next = b->next; /* re-attach b's neighbours together and free b. */ bucket_free(b); b = next; } if((d->ht_old->first = b)) { return; } ht_free(d->ht_old, d->key_free); d->ht_old = NULL; }
int main (int argc, char const *argv[]) { /* allocate hash table of ints to store counts */ /* i've implemented a *closed* hash table with quadratic probing */ /* so i tried to pick some large prime value as an upper limit. */ table *wt = ht_init_int(max_words, max_buffer, ht_hash, ht_quad_probe); vect_wc *wv = vect_init_wc(0); char buf[max_buffer]; /* buffer to hold each word as we discover it */ char c; /* char we are currently examining in the text */ int count; /* count of the current word in the text */ int i = 0; /* offset to the current position in the buffer */ /* read a character from stdin */ while((c = getchar()) != EOF) { /* look for valid characters. */ /* special characters - and ' are only allowed mid-word */ if (isalnum(c) || i > 0 && (c == '\'' || c == '-')) { /* append c and null byte to the buffer */ buf[i++] = tolower(c); buf[i] = '\0'; /* break on buffer overflow */ if (i >= max_buffer-1) break; /* next character */ continue; } /* found a delimiting character */ /* skip if the buffer is empty */ if (i == 0) continue; /* remove any trailing special characters */ while (!isalnum(buf[i]) && i > 0) buf[i--] = '\0'; /* find out if we've already stored this word */ if (ht_has_key(wt, buf)) /* retrieve the count if so */ count = ht_get_int(wt, buf); else /* otherwise default to 0 */ count = 0; /* add or update the word, incrementing count */ ht_add_int(wt, buf, count+1); /* clear the buffer for the next word */ buf[0] = '\0'; i = 0; } /* convert hash table into vector */ for (int i=0; i < wt->size; i++) { struct wc_tuple wc; strcpy(wc.word, wt->keys + wt->key_len * i); wc.count = *((unsigned int *) wt->values + i); if (wc.word[0]) vect_push_wc(wv, wc); } /* sort the vector */ qsort(wv->data, wv->size, wv->data_size, cmp_wc); /* output sorted vector */ for (int i=0; i < wv->size; i++) printf("%s: %d\n", wv->data[i].word, wv->data[i].count); /* not necessary, but momma always told me to free what i malloc */ ht_free(wt); vect_free(wv); return 0; }
void shiki_free(struct shiki *lru) { ht_free(lru->ht); free(lru); }
void sdb_ht_free(SdbHash *ht) { ht_free (ht); }
int main(int argc, char **argv) { int i; hash_table_t table[] = { /* some tables to operate on */ HASH_TABLE_INIT(0, check_func, check_comp, 0, TABLE0), HASH_TABLE_INIT(0, check_func, check_comp, 0, TABLE1), { DEADINT, DEADINT, DEADINT, DEADINT, DEADINT, DEADINT, DEADPTR, (hash_func_t)DEADPTR, (hash_comp_t)DEADPTR, (hash_resize_t)DEADPTR, DEADPTR } /* table[2] */ }; hash_entry_t entry[] = { /* some entries to operate on */ HASH_ENTRY_INIT(OBJECT0), HASH_ENTRY_INIT(OBJECT1), HASH_ENTRY_INIT(OBJECT2), HASH_ENTRY_INIT(OBJECT3), HASH_ENTRY_INIT(OBJECT4), }; db_key_t key[] = { /* some keys... */ DB_KEY_INIT("obj0", 0), DB_KEY_INIT("obj1", 1), DB_KEY_INIT("obj2", 2), DB_KEY_INIT("obj3", 3), DB_KEY_INIT("obj4", 4), }; /* initialize the tables with a size */ if (ht_init(&table[0], 0, check_func, check_comp, 0, TABLE0, 7) || ht_init(&table[1], 0, check_func, check_comp, 0, TABLE1, 7) || !table[0].ht_table || !table[1].ht_table) return -1; /* failed to initialize test */ /* Add some entries to various hash tables */ for (i = 0; i < 5; i++) if (ht_add(&table[0], &entry[i], &key[i])) return -1; /* failed to initialize test */ /* Check handling of bad arguments */ check_result(ht_free(0), DB_ERR_BADARGS, "ht_free_noargs", "ht_free() with no valid arguments", 0); check_result(ht_free(&table[2]), DB_ERR_BADARGS, "ht_free_badtable", "ht_free() with bad table", 0); /* Freeze the table temporarily */ ht_flags(&table[1]) |= HASH_FLAG_FREEZE; /* check if frozen tables are excluded */ check_result(ht_free(&table[1]), DB_ERR_FROZEN, "ht_free_frozen", "ht_free() on frozen table", 1); /* Unfreeze the table */ ht_flags(&table[1]) &= ~HASH_FLAG_FREEZE; /* Check if non-empty tables are excluded */ check_result(ht_free(&table[0]), DB_ERR_NOTEMPTY, "ht_free_nonempty", "ht_free() on non-empty table", 0); /* OK, now try to free the table */ check_result(ht_free(&table[1]), 0, "ht_free_t0", "ht_free() on empty table", 1); /* Verify that table pointer is now 0 */ if (table[1].ht_table != 0) printf("FAIL/ht_free_t0_table:Table not cleared properly\n"); else printf("PASS/ht_free_t0_table:Table properly cleared\n"); return 0; }