Exemple #1
0
/* initialize a table. */
int ht_init(hashtbl_t * tbl,
            size_t sz,
            unsigned int (*hash) (unsigned char *),
            void (*memfree) (void *)) {

  /* some things are required */
  if (tbl == NULL || sz == 0)
    return 1;

  sz = ht_next_prime(sz);

  tbl->arr = xmalloc(sizeof(bstree_t *) * sz);
  memset(tbl->arr, 0, sizeof(bstree_t *) * sz);

  tbl->ht_elem_pool = mempool_create(sizeof(ht_elem_t) * 128);
  if (tbl->ht_elem_pool == NULL)
    return -1;

  /* since keys are free-form text, the pool size is arbitrary. */
  tbl->key_pool = mempool_create(4096);
  if (tbl->key_pool == NULL)
    return -1;

  tbl->nelems = 0;
  tbl->arrsz = sz;
  tbl->free = memfree;  /* NULL ok here */
  if (hash)             /* set a default hash function if none specified */
    tbl->hash = hash;
  else
    tbl->hash = BKDRHash;

  return 0;
}
Exemple #2
0
/* create a new HT */
static struct ht *
ht_new(long sz) {
	struct ht *ht = calloc(sizeof(struct ht), 1);

	sz = ht_next_prime(sz);

	ht->sz = sz;

	ht->slots = calloc(sizeof(struct bucket), sz);
	if(ht->slots == NULL) {
		fprintf(stderr, "failed to allocate %ld bytes.\n",
				sz * (long)sizeof(struct bucket));
		abort();
	}

	return ht;
}