Exemple #1
0
static void check_uint32_wrapper(void)
{
	int ret;
	hi_handle_t *hi_handle;
	uint32_t key = 23;
	int data = 666, *val;
	void *data_ptr;

	fputs(" o check uint32_t wrapper functions tests ...", stdout);

	ret = hi_init_uint32_t(&hi_handle, 23);
	assert(ret == 0);

	for (key = 0; key < 23; key++)
	{
		ret = hi_insert_uint32_t(hi_handle, key, &data);
		assert(ret == 0);

		ret = hi_get_uint32_t(hi_handle, key, &data_ptr);
		assert(ret == 0);
		val = data_ptr;
		assert(*val == data);
	}

	ret = hi_fini(hi_handle);
	assert(ret == 0);

	puts(" passed");
}
Exemple #2
0
static void check_str_wrapper(void)
{
	int ret;
	hi_handle_t *hi_handle;
	char key[32];
	uint16_t i;
	const char *data = "data element";
	void *data_ptr;

	fputs(" o check string wrapper functions tests ...", stdout);

	ret = hi_init_str(&hi_handle, 23);
	assert(ret == 0);

	for (i = 0; i < 23; i++)
	{
		sprintf(key, "%u", i);

		ret = hi_insert_str(hi_handle, key, data);
		assert(ret == 0);

		ret = hi_get_str(hi_handle, key, &data_ptr);
		assert(ret == 0);
	}

	ret = hi_fini(hi_handle);
	assert(ret == 0);

	puts(" passed");
}
Exemple #3
0
void ethernet_dissector_destroy(void)
{
	oui_hash_destroy();
	ether_types_hash_destroy();
	udp_ports_hash_destroy();
	tcp_ports_hash_destroy();
	hi_fini(ethernet_dissector_hash);
}
static int test_hashtable(enum coll_eng collision_engine)
{
    int ret = 0, i;
    pthread_t thread_id[MAXTHREAD];
    struct hi_init_set hi_set;

    fputs("# concurrent test\n", stderr);

    /* create one hash */
    hi_set_zero(&hi_set);
    hi_set_bucket_size(&hi_set, 100);
    hi_set_hash_alg(&hi_set, HI_HASH_ELF);
    hi_set_coll_eng(&hi_set, collision_engine);
    hi_set_key_cmp_func(&hi_set, hi_cmp_str);

    ret = hi_create(&hi_hndl, &hi_set);
    if (ret != 0) {
        fprintf(stderr, "Error %s\n", ret == HI_ERR_SYSTEM ?
                strerror(errno) : hi_strerror(ret));
        return ret;
    }

    for (i = 0; i < MAXTHREAD; i++) {
        int *num = malloc(sizeof(int *));

        if (!num) {
            perror("malloc");
            exit(1);
        }
        *num = i;
        ret = pthread_create(&thread_id[i], NULL, thread_main, num);
        if (ret) {
            fprintf(stderr, "pthread_create failed: %s\n", strerror(ret));
            free(num);
            return ret;
        }
    }

    fputs("# + -> thread startup; - -> thread shutdown\n", stderr);
    for(i = 0; i < MAXTHREAD; i++) {
        pthread_join(thread_id[i], NULL);
    }

    hi_fini(hi_hndl);

    fprintf(stderr, " passed\n");
    return ret;
}
Exemple #5
0
static void check_preambel_test(void)
{
  int ret;
  hi_handle_t *hi_handle;
  const char *key = "23";
  const char *data = "data element";
  void *data_ptr;

  /* initialize hashish handle */
  hi_init_str(&hi_handle, 23);

  /* insert an key/data pair */
  ret = hi_insert_str(hi_handle, key, data);

  /* search for a pair with a string key and store result */
  hi_get_str(hi_handle, key, &data_ptr);

  /* free the hashish handle */
  hi_fini(hi_handle);

}
Exemple #6
0
static void check_hi_load_factor(void)
{
	int ret;
	hi_handle_t *hi_handle;
	double load_factor;

	fputs(" o check_hi_load_factor test ...", stdout);

	/* initialize hashish handle */
	hi_init_str(&hi_handle, 3);

	ret = hi_insert_str(hi_handle, "1", NULL);
	ret = hi_insert_str(hi_handle, "2", NULL);

	load_factor = hi_table_load_factor(hi_handle);

	assert(load_factor == ((double)3)/2);

	/* free the hashish handle */
	hi_fini(hi_handle);

	fputs("passed\n", stdout);
}
Exemple #7
0
int hi_rehash(hi_handle_t *hi_hndl, uint32_t new_table_size)
{
	int ret, auto_rehash;
	void *key, *data;
	uint32_t keylen;
	hi_handle_t *hi_handle;
	hi_iterator_t *iterator;

	/* create a clean handle for the new structure */
	ret = lhi_create_vanilla_hdnl(&hi_handle);
	if (ret != SUCCESS)
		return ret;

	/* we take over the original settings done
	 * by the user taken at hi_create() time */
	lhi_transform_hndl_2_hndl(hi_hndl, hi_handle);

	hi_handle->table_size = new_table_size;

	/* Allocate memory fot accounting the number of
	 * elements within every bucket in the table.  */
	ret = XMALLOC((void **) &hi_handle->bucket_size,
			hi_handle->table_size * sizeof(*hi_handle->bucket_size));
	if (ret != 0) {
		return HI_ERR_SYSTEM;
	}

	/* 0 objects in the list at start-up */
	hi_handle->no_objects = 0;

	/* Initiate mutex lock if build with thread
	 * support. */
	hi_handle->mutex_lock = NULL;
	ret = lhi_pthread_mutex_init(&hi_handle->mutex_lock, NULL);
	if (ret != 0) {
		return HI_ERR_SYSTEM;
	}

	/* Create internal data structure for
	 * list, array or rbtree */
	switch (hi_handle->coll_eng) {

		case COLL_ENG_LIST:
		case COLL_ENG_LIST_HASH:
		case COLL_ENG_LIST_MTF:
		case COLL_ENG_LIST_MTF_HASH:
			ret = lhi_create_eng_list(hi_handle);
			if (ret != SUCCESS)
				return ret;


			break;
		case COLL_ENG_ARRAY:
		case COLL_ENG_ARRAY_HASH:
		case COLL_ENG_ARRAY_DYN:
		case COLL_ENG_ARRAY_DYN_HASH:
			ret = lhi_create_eng_array(hi_handle);
			if (ret != SUCCESS)
				return ret;


			break;
		case COLL_ENG_RBTREE:
			ret = lhi_create_eng_rbtree(hi_handle);
			if (ret != SUCCESS)
				return ret;


			break;
		default:
			return HI_ERR_INTERNAL;
			break;
	}

	ret = hi_iterator_create(hi_hndl, &iterator);
	if (ret != SUCCESS)
		return ret;

	auto_rehash = hi_handle->rehash_auto;
	hi_handle->rehash_auto = 0;
	while ((ret = hi_iterator_getnext(iterator, &data, &key, &keylen)) ==
			SUCCESS) {
		ret = hi_insert(hi_handle, key, keylen, data);
		if (ret != SUCCESS) {
			hi_iterator_fini(iterator);
			hi_fini(hi_handle);

			hi_handle->rehash_auto = auto_rehash;
			return ret;
		}
	}
	hi_handle->rehash_auto = auto_rehash;
	/* verify that no error occured during iterator run */
	if (ret != HI_ERR_NODATA) {
		hi_iterator_fini(iterator);
		return ret;
	}

	hi_iterator_fini(iterator);

	/* free old hashish handle */
	lhi_fini_internal(hi_hndl);

	memcpy(hi_hndl, hi_handle, sizeof(*hi_hndl));

	return SUCCESS;
}
int main(int argc, char **argv)
{

	int ret, verbose = 0; unsigned int i;
	char *affix = NULL;
	char *prefix = NULL;
	int opt = 0;
	int len = 0;
	int fd1, fd2;
	char *png_filename = NULL;
	unsigned int entries = DEFAULT_ENTRIES;
	unsigned int table_size = DEFAULT_HASHTABLE_SIZE;
	struct drand48_data r_d;
	hi_handle_t *hi_handle;
	uint32_t (*hashf)(const uint8_t *, uint32_t);
	const char *hashfname;

	hashf = NULL;

	srand48_r((long int)time(NULL), &r_d);

	while ((opt = getopt(argc, argv, "hqn:l:p:t:a:g:f:v")) != -1) {
		switch (opt) {
		case 'q':
			{
			int max_fd, std_fd, j;
			if ((max_fd = (int) sysconf(_SC_OPEN_MAX)) < 0) {
				max_fd = 256;
			}
			for (j = max_fd - 1; j >= 0; --j) {
				close(j);
			}
			std_fd = open("/dev/null", O_RDWR);
			fd1 = dup(std_fd);
			fd2 = dup(std_fd);
			}
			break;
		case 'f':
			if (!(strcasecmp(optarg, "elf"))) {
				hashf = lhi_hash_elf;
				hashfname = "lhi_hash_elf";
			} else if (!(strcasecmp(optarg, "torek"))) {
				hashf = lhi_hash_torek;
				hashfname = "lhi_hash_torek";
			} else if (!(strcasecmp(optarg, "dumb1"))) {
				hashf = lhi_hash_dumb1;
				hashfname = "lhi_hash_dumb1";
			} else if (!(strcasecmp(optarg, "phong"))) {
				hashf = lhi_hash_phong;
				hashfname = "lhi_hash_phong";
			} else {
				fprintf(stderr, "Hashing function not supported: %s\n",
						optarg);
				usage(EXIT_FAILURE, argv[0]);
			}
			break;
		case 'h':
			usage(EXIT_SUCCESS, argv[0]);
			break;
		case 'n':
			entries = atoi(optarg);
			break;
		case 'l':
			len = atoi(optarg);
			break;
		case 'p':
			prefix = strdup(optarg);
			break;
		case 't':
			table_size = atoi(optarg);
			break;
		case 'v':
			verbose++;
			break;
		case 'a':
			affix = strdup(optarg);
			break;
		case 'g':
#ifdef HAVE_LIBGD
			png_filename = strdup(optarg);
# else
			fprintf(stderr, "sorry - you build without gd library support\n");
			usage(EXIT_FAILURE, argv[0]);
#endif
			break;
		case '?':
			fprintf(stderr, "No such option: `%c'\n\n", optopt);
			usage(EXIT_FAILURE, argv[0]);
			break;
		}
	}

	fputs("# String Distribution Hash Test\n", stderr);

	/* initialize secure[tm] rand() seed */
	//init_seed();

	/* initialize hash table */
	ret = hi_init_str(&hi_handle, table_size);

	if (hashf != NULL) {
		lhi_sethashfunc(hi_handle, hashf);
		fprintf(stderr, "# take %s as hash function\n", hashfname);
	}

	/* fill hash table */
	for(i = 0; i < entries; i++) {

		char *key = NULL, *tmp_key;
		size_t key_len = 0;

		/* compound key */
		if (len == 0) {
			len = (rand() % (MAX_STRING_LEN - MIN_STRING_LEN + 1)) + MIN_STRING_LEN;
		}
		if (random_string(len, &tmp_key, &r_d) < 0)
			exit(EXIT_FAILURE);

		if (prefix)
			key_len += strlen(prefix);
		key_len += strlen(tmp_key);
		if (affix)
			key_len += strlen(affix);


		key = malloc(key_len + 1);
		if (key == NULL) {
			fprintf(stderr, "malloc %s\n", strerror(errno));
			exit(1);
		}

		if (prefix != NULL) {
			sprintf(&key[0], "%s%s", prefix, tmp_key);
		} else {
			sprintf(key, "%s", tmp_key);
		}

		free(tmp_key);
		tmp_key = NULL;

		if (affix)
			strcat(key, affix);

		if (verbose >= 1)
			fprintf(stdout, "key: %s\n", key);

		ret = hi_insert_str(hi_handle, (void *) key, NULL);
		if (ret < 0)
			fprintf(stderr, "# WARNING: Can't insert key (maybe a duplicated key: %s)!\n", key);
	}

	/* print statistic */
	ret = hi_size(hi_handle);
	fprintf(stderr, "# hash table entries: %d\n", ret);

	if (png_filename) { /* grapical output */

#ifdef HAVE_LIBGD
# define RECT_SIZE 5
# define RECT_BODER_SIZE 1

		int j;
		uint32_t x, y;
		gdImagePtr im;
		FILE *pngout;
		int black, white, red, max_list_len = 0;


		/* calculate maximum listsize */
		for(i = 0; i < table_size; i++) {
			int tmp_bucket_size = hi_bucket_size(hi_handle, i);
			max_list_len = max(max_list_len, tmp_bucket_size);
		}


		/* create a image with max_list_len X table_size */
		im = gdImageCreate((max_list_len * (RECT_SIZE + RECT_BODER_SIZE * 2)) + 2,
				(table_size * ((RECT_SIZE + RECT_BODER_SIZE * 2)) + 2));

		black = gdImageColorAllocate(im, 255, 231, 186);
		white = gdImageColorAllocate(im, 255, 165, 79);
		red   = gdImageColorAllocate(im, 205, 102, 29);

		x = 1;
		y = 1;

		for (i = 0; i < table_size; i++) {
			int bucket_size =  hi_bucket_size(hi_handle, i);
			for (j = 0; j < bucket_size; j++) {
				gdImageFilledRectangle(im, x + 1, y + 1, x + RECT_SIZE, y + RECT_SIZE, white);
				gdImageRectangle(im, x, y, x + RECT_SIZE + (RECT_BODER_SIZE << 1), y + RECT_SIZE + (RECT_BODER_SIZE << 1), red);
				x += RECT_SIZE + (RECT_BODER_SIZE << 1);
			}
			x = 1;
			y += RECT_SIZE + (RECT_BODER_SIZE << 1);
		}

		pngout = fopen(png_filename, "wb");
		gdImagePng(im, pngout);
		fclose(pngout);
		gdImageDestroy(im);

# undef RECT_SIZE
# undef RECT_BODER_SIZE
#endif /* HAVE_LIBGD */

	}

	if (verbose >= 1) {  /* terminal output */
		for(i = 0; i < table_size; i++) {
			fprintf(stderr, "bucket no: %d bucket size: %d\n",
					i, hi_bucket_size(hi_handle, i));
		}

	}

	/* delete table */
	hi_fini(hi_handle);

	return 0;
}
Exemple #9
0
static void check_iterator(enum coll_eng engine, enum hash_alg hash_alg)
{
	int ret, i;
	hi_handle_t *hi_hndl;
	struct hi_init_set hi_set;
	hi_iterator_t *iterator;
	void *data_ptr = (void *) 0xdeadbeef;
	void *key;
	uint32_t keylen;

	hi_set_zero(&hi_set);
	ret = hi_set_bucket_size(&hi_set, 100);
	assert(ret == 0);
	ret = hi_set_hash_alg(&hi_set, hash_alg);
	assert(ret == 0);
	ret = hi_set_coll_eng(&hi_set, engine);
	assert(ret == 0);
	ret = hi_set_key_cmp_func(&hi_set, hi_cmp_str);
	assert(ret == 0);

	/* we need aditional arguments for ARRAY based engines */
	switch (engine) {
		case COLL_ENG_ARRAY:
		case COLL_ENG_ARRAY_HASH:
		case COLL_ENG_ARRAY_DYN:
		case COLL_ENG_ARRAY_DYN_HASH:
			ret = hi_set_coll_eng_array_size(&hi_set, 20);
			assert(ret == 0);
			break;
		default:
			break;
	};

	ret = hi_create(&hi_hndl, &hi_set);
	if (ret != 0)
		print_error(ret);
	assert(ret == 0);

	ret = hi_insert(hi_hndl, (void *) "key", sizeof("key"), "data");
	assert(ret == 0);

	ret = hi_insert(hi_hndl, (void *) "key1", sizeof("key1"), "data1");
	assert(ret == 0);

	ret = hi_insert(hi_hndl, (void *) "key2", sizeof("key2"), "data2");
	assert(ret == 0);

	ret = hi_iterator_create(hi_hndl, &iterator);
	if (ret != 0)
		return;

	for (i = 0; i < 2; i++) {
		bool got_key[] = { 0, 0, 0 };
		unsigned int j;

		for (j = 0 ; j < 3 ; j++) {
			data_ptr = NULL;
			ret = hi_iterator_getnext(iterator, &data_ptr, &key, &keylen);
			assert(ret == 0);
			assert(data_ptr);
			if (strcmp(data_ptr, "data") == 0) {
				assert(!got_key[0]);
				got_key[0] = true;
				continue;
			}
			if (strcmp(data_ptr, "data1") == 0) {
				assert(!got_key[1]);
				got_key[1] = true;
				continue;
			}
			assert (strcmp(data_ptr, "data2") == 0);
			assert(!got_key[2]);
			got_key[2] = true;
		}
		ret = hi_iterator_getnext(iterator, &data_ptr, &key, &keylen);
		assert (ret == HI_ERR_NODATA);
		ret = hi_iterator_reset(iterator);
		assert(ret == 0);
	}
	hi_iterator_fini(iterator);

	ret = hi_remove(hi_hndl, (void *) "key", sizeof("key"), &data_ptr);
	assert(ret == 0);
	ret = hi_remove(hi_hndl, (void *) "key1", sizeof("key1"), &data_ptr);
	assert(ret == 0);
	ret = hi_remove(hi_hndl, (void *) "key2", sizeof("key2"), &data_ptr);
	assert(ret == 0);

	ret = hi_fini(hi_hndl);
	assert(ret == 0);

	fputs("passed\n", stdout);
}
Exemple #10
0
static void check_insert(enum coll_eng engine, enum hash_alg hash_alg)
{
	int ret;
	hi_handle_t *hi_hndl;
	struct hi_init_set hi_set;
	void *data_ptr = (void *) 0xdeadbeef;

	hi_set_zero(&hi_set);
	ret = hi_set_bucket_size(&hi_set, 100);
	assert(ret == 0);
	ret = hi_set_hash_alg(&hi_set, hash_alg);
	assert(ret == 0);
	ret = hi_set_coll_eng(&hi_set, engine);
	assert(ret == 0);
	ret = hi_set_key_cmp_func(&hi_set, hi_cmp_str);
	assert(ret == 0);

	/* we need aditional arguments for ARRAY based engines */
	switch (engine) {
		case COLL_ENG_ARRAY:
		case COLL_ENG_ARRAY_HASH:
		case COLL_ENG_ARRAY_DYN:
		case COLL_ENG_ARRAY_DYN_HASH:
			ret = hi_set_coll_eng_array_size(&hi_set, 20);
			assert(ret == 0);
			break;
		default:
			break;
	};


	ret = hi_create(&hi_hndl, &hi_set);
	if (ret != 0)
		print_error(ret);
	assert(ret == 0);


	ret = hi_insert(hi_hndl, (void *) "key", sizeof("key"), "XX");
	assert(ret == 0);

	/* same key -> must fail */
	ret = hi_insert(hi_hndl, (void *) "key", sizeof("key"), "XX");
	assert(ret == HI_ERR_DUPKEY);


	/* key already in data structure -> must return 0 (SUCCESS) */
	ret = hi_get(hi_hndl, (void *) "key", sizeof("key"), &data_ptr);
	assert(ret == 0);
	//assert(data_ptr == NULL);

	ret = hi_remove(hi_hndl, (void *) "key", sizeof("key"), &data_ptr);
	assert(ret == 0);

	ret = hi_get(hi_hndl, (void *) "key", sizeof("key"), &data_ptr);
	assert(ret == HI_ERR_NOKEY);

	ret = hi_fini(hi_hndl);
	assert(ret == 0);

	fputs("passed\n", stdout);
}
Exemple #11
0
static void check_get_remove(enum coll_eng engine, enum hash_alg hash_alg)
{
	int ret;
	hi_handle_t *hi_hndl;
	struct hi_init_set hi_set;
	void *data_ret;

	hi_set_zero(&hi_set);
	ret = hi_set_bucket_size(&hi_set, 100);
	assert(ret == 0);
	ret = hi_set_hash_alg(&hi_set, hash_alg);
	assert(ret == 0);
	ret = hi_set_coll_eng(&hi_set, engine);
	assert(ret == 0);
	ret = hi_set_key_cmp_func(&hi_set, hi_cmp_str);
	assert(ret == 0);

	/* we need aditional arguments for ARRAY based engines */
	switch (engine) {
		case COLL_ENG_ARRAY:
		case COLL_ENG_ARRAY_HASH:
		case COLL_ENG_ARRAY_DYN:
		case COLL_ENG_ARRAY_DYN_HASH:
			ret = hi_set_coll_eng_array_size(&hi_set, 20);
			assert(ret == 0);
			break;
		default:
			break;
	};

	ret = hi_create(&hi_hndl, &hi_set);
	if (ret != 0)
		print_error(ret);
	assert(ret == 0);

	assert(hi_hndl->no_objects == 0);
	ret = hi_insert(hi_hndl, (void *) "key", sizeof("key"), "DATA");
	assert(ret == 0);
	assert(hi_hndl->no_objects == 1);
	ret = hi_insert(hi_hndl, (void *) "key2", sizeof("key2"), "DATAX");
	assert(ret == 0);
	assert(hi_hndl->no_objects == 2);
	ret = hi_insert(hi_hndl, (void *) "key3", sizeof("key3"), "DATAX");
	assert(ret == 0);
	assert(hi_hndl->no_objects == 3);

	/* key already in data structure -> must return 0 (SUCCESS) */
	ret = hi_get(hi_hndl, (void *) "key", sizeof("key"), &data_ret);
	if (ret != 0)
		print_error(ret);
	check_data(data_ret, "DATA");
	assert(hi_hndl->no_objects == 3);

	ret = hi_get(hi_hndl, (void *) "key3", sizeof("key3"), &data_ret);
	if (ret != 0)
		print_error(ret);
	check_data(data_ret, "DATAX");
	assert(hi_hndl->no_objects == 3);
	data_ret = NULL;

	ret = hi_remove(hi_hndl, (void *) "key", sizeof("key"), &data_ret);
	assert(ret == 0);
	assert(hi_hndl->no_objects == 2);
	check_data(data_ret, "DATA");
	data_ret = NULL;
	ret = hi_remove(hi_hndl, (void *) "key2", sizeof("key2"), &data_ret);
	assert(ret == 0);
	assert(hi_hndl->no_objects == 1);
	ret = hi_remove(hi_hndl, (void *) "key3", sizeof("key3"), &data_ret);
	assert(ret == 0);
	assert(hi_hndl->no_objects == 0);
	check_data(data_ret, "DATAX");

	ret = hi_fini(hi_hndl);
	assert(ret == 0);

	fputs("passed\n", stdout);
}
// destructor
PbniHashStr::~PbniHashStr()
{
	hi_fini(m_hi_handle);
}
Exemple #13
0
void oui_hash_destroy(void)
{
	hi_fini(oui_hash_handle);
}
void ether_types_hash_destroy(void)
{
	hi_fini(ether_types_hash_handle);
}