hash.c
int
hash_put(hash_t *hash, const char *key, void *val, size_t valsize)
{
	hash_element_list_t *elem;
	size_t sum;
	int retval;

#ifdef DEBUG
	printf("Inserting element with key [%s].\n", key);
#endif
	/* make an element */
	elem = mem_malloc(sizeof(hash_element_list_t));
	if (elem == NULL) return -1;

	memset(elem, 0, sizeof(hash_element_list_t));

	/* make a copy of key */
	elem->key = mem_malloc(strlen(key)+1);
	if (elem->key == NULL) {
		mem_free(elem);
		return -1;
	}
	strcpy(elem->key, key);

	/* make a copy of value */
	elem->data = mem_malloc(valsize);
	if (elem->data == NULL) {
		mem_free(elem->key);
		mem_free(elem);
		return -1;
	}
	memcpy(elem->data, val, valsize);

	/* calculate hashsum */
	sum = hashsum(hash, key);
	assert(sum < hash->size);
	retval = hash_appendtolist(&hash->karray[sum], elem);
	if (retval == 0) hash->element_count++;

#ifdef DEBUG
	printf("%s element finished (%d).\n", retval == 0 ?
	    "Inserting" : "Replacing", retval);
#endif

	if (retval == 1) retval = 0; /* just replacing, but everything ok */
	return retval;
}
/* inserts a copy of val into the hash */
inserts a copy of val into the hash
inserts a copy of val into the hash
int
hash_put(hash_t *hash, const char *key, void *val, size_t valsize)
{
	hash_element_list_t *elem;
	size_t sum;
	int retval;
#ifdef DEBUG
	printf("Inserting element with key [%s].\n", key);
#endif
	elem = mem_malloc(sizeof(hash_element_list_t));
	if (elem == NULL) return -1;
	memset(elem, 0, sizeof(hash_element_list_t));
	elem->key = mem_malloc(strlen(key)+1);
	if (elem->key == NULL) {
		mem_free(elem);
		return -1;
	}
	strcpy(elem->key, key);
	elem->data = mem_malloc(valsize);
	if (elem->data == NULL) {
		mem_free(elem->key);
		mem_free(elem);
		return -1;
	}
	memcpy(elem->data, val, valsize);
	sum = hashsum(hash, key);
	assert(sum < hash->size);
	retval = hash_appendtolist(&hash->karray[sum], elem);
	if (retval == 0) hash->element_count++;
#ifdef DEBUG
	printf("%s element finished (%d).\n", retval == 0 ?
	    "Inserting" : "Replacing", retval);
#endif
	if (retval == 1) retval = 0; 
	return retval;
}
int hash_put ( hash_t * hash , const char * key , void * val , size_t valsize ) { hash_element_list_t * elem ; size_t sum ; int retval ; #ifdef DEBUG printf ( " \n " , key ) ; #endif elem = mem_malloc ( sizeof ( hash_element_list_t ) ) ; if ( elem == NULL ) return -1 ; memset ( elem , 0 , sizeof ( hash_element_list_t ) ) ; elem -> key = mem_malloc ( strlen ( key ) + 1 ) ; if ( elem -> key == NULL ) { mem_free ( elem ) ; return -1 ; } strcpy ( elem -> key , key ) ; elem -> data = mem_malloc ( valsize ) ; if ( elem -> data == NULL ) { mem_free ( elem -> key ) ; mem_free ( elem ) ; return -1 ; } memcpy ( elem -> data , val , valsize ) ; sum = hashsum ( hash , key ) ; assert ( sum < hash -> size ) ; retval = hash_appendtolist ( & hash -> karray [ sum ] , elem ) ; if ( retval == 0 ) hash -> element_count ++ ; #ifdef DEBUG printf ( " \n " , retval == 0 ? " " : " " , retval ) ; #endif if ( retval == 1 ) retval = 0 ; return retval ; }
inserts a copy of val into the hash
inserts a copy of val into the hash
/* make an element */ /* make a copy of key */ /* make a copy of value */ /* calculate hashsum */ /* just replacing, but everything ok */