xref: /illumos-gate/usr/src/cmd/sgs/crle/common/hash.c (revision 8285fe28)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (c) 2000 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include	<stdio.h>
287c478bd9Sstevel@tonic-gate #include	<stdlib.h>
297c478bd9Sstevel@tonic-gate #include	<string.h>
307c478bd9Sstevel@tonic-gate #include	<libelf.h>
317c478bd9Sstevel@tonic-gate #include	"_crle.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate Hash_tbl *
make_hash(int size,Hash_type type,ulong_t ident)347c478bd9Sstevel@tonic-gate make_hash(int size, Hash_type type, ulong_t ident)
357c478bd9Sstevel@tonic-gate {
367c478bd9Sstevel@tonic-gate 	Hash_tbl *	tbl;
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 	if ((tbl = malloc(sizeof (Hash_tbl))) == 0)
397c478bd9Sstevel@tonic-gate 		return (0);
407c478bd9Sstevel@tonic-gate 
41*8285fe28SToomas Soome 	tbl->t_entry = calloc(size, (unsigned)(sizeof (Hash_ent *)));
42*8285fe28SToomas Soome 	if (tbl->t_entry == NULL) {
43*8285fe28SToomas Soome 		free(tbl);
447c478bd9Sstevel@tonic-gate 		return (0);
45*8285fe28SToomas Soome 	}
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	tbl->t_ident = ident;
487c478bd9Sstevel@tonic-gate 	tbl->t_type = type;
497c478bd9Sstevel@tonic-gate 	tbl->t_size = size;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	return (tbl);
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate Hash_ent *
get_hash(Hash_tbl * tbl,Addr key,Half id,int mode)567c478bd9Sstevel@tonic-gate get_hash(Hash_tbl * tbl, Addr key, Half id, int mode)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	int		bucket;
597c478bd9Sstevel@tonic-gate 	Hash_ent *	ent;
607c478bd9Sstevel@tonic-gate 	Word		hashval;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	if (tbl->t_type == HASH_STR)
637c478bd9Sstevel@tonic-gate 		hashval = elf_hash((const char *)key);
647c478bd9Sstevel@tonic-gate 	else
657c478bd9Sstevel@tonic-gate 		hashval = key;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	bucket = hashval % tbl->t_size;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if (mode & HASH_FND_ENT) {
707c478bd9Sstevel@tonic-gate 		for (ent = tbl->t_entry[bucket]; ent != NULL;
717c478bd9Sstevel@tonic-gate 		    ent = ent->e_next) {
727c478bd9Sstevel@tonic-gate 			if (tbl->t_type == HASH_STR) {
737c478bd9Sstevel@tonic-gate 				if ((strcmp((const char *)ent->e_key,
747c478bd9Sstevel@tonic-gate 				    (const char *)key) == 0) && ((id == 0) ||
757c478bd9Sstevel@tonic-gate 				    (id == ent->e_id)))
767c478bd9Sstevel@tonic-gate 					return (ent);
777c478bd9Sstevel@tonic-gate 			} else {
787c478bd9Sstevel@tonic-gate 				if (ent->e_key == key)
797c478bd9Sstevel@tonic-gate 					return (ent);
807c478bd9Sstevel@tonic-gate 			}
817c478bd9Sstevel@tonic-gate 		}
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 	if (!(mode & HASH_ADD_ENT))
847c478bd9Sstevel@tonic-gate 		return (0);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/*
877c478bd9Sstevel@tonic-gate 	 * Key not found in this hash table ... insert new entry into bucket.
887c478bd9Sstevel@tonic-gate 	 */
89*8285fe28SToomas Soome 	if ((ent = calloc(1, sizeof (Hash_ent))) == NULL)
907c478bd9Sstevel@tonic-gate 		return (0);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	ent->e_key = key;
937c478bd9Sstevel@tonic-gate 	ent->e_hash = hashval;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	/*
967c478bd9Sstevel@tonic-gate 	 * Hook into bucket chain
977c478bd9Sstevel@tonic-gate 	 */
987c478bd9Sstevel@tonic-gate 	ent->e_next = tbl->t_entry[bucket];
997c478bd9Sstevel@tonic-gate 	tbl->t_entry[bucket] = ent;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	return (ent);
1027c478bd9Sstevel@tonic-gate }
103