2019-03-23 00:58:47 +01:00
|
|
|
// ************************************************************************
|
2020-05-23 23:13:14 +02:00
|
|
|
//
|
2019-03-23 00:58:47 +01:00
|
|
|
// Copyright (c) 1995-2002 Juniper Networks, Inc. All rights reserved.
|
2020-05-23 23:13:14 +02:00
|
|
|
//
|
2019-03-23 00:58:47 +01:00
|
|
|
// Permission is hereby granted, without written agreement and without
|
|
|
|
|
// license or royalty fees, to use, copy, modify, and distribute this
|
|
|
|
|
// software and its documentation for any purpose, provided that the
|
|
|
|
|
// above copyright notice and the following three paragraphs appear in
|
|
|
|
|
// all copies of this software.
|
2020-05-23 23:13:14 +02:00
|
|
|
//
|
2019-03-23 00:58:47 +01:00
|
|
|
// IN NO EVENT SHALL JUNIPER NETWORKS, INC. BE LIABLE TO ANY PARTY FOR
|
|
|
|
|
// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
// ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
|
|
|
|
|
// JUNIPER NETWORKS, INC. HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
|
// DAMAGE.
|
2020-05-23 23:13:14 +02:00
|
|
|
//
|
2019-03-23 00:58:47 +01:00
|
|
|
// JUNIPER NETWORKS, INC. SPECIFICALLY DISCLAIMS ANY WARRANTIES,
|
|
|
|
|
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
|
|
|
|
|
// NON-INFRINGEMENT.
|
2020-05-23 23:13:14 +02:00
|
|
|
//
|
2019-03-23 00:58:47 +01:00
|
|
|
// THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND JUNIPER
|
|
|
|
|
// NETWORKS, INC. HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
|
|
|
|
|
// UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
2020-05-23 23:13:14 +02:00
|
|
|
//
|
2019-03-23 00:58:47 +01:00
|
|
|
// ************************************************************************
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ihash.h --
|
|
|
|
|
*
|
2020-05-23 23:13:14 +02:00
|
|
|
* "Internal" hash routines.
|
2019-03-23 00:58:47 +01:00
|
|
|
* Allows hashing of existing structs without creating parallel structs
|
2020-05-23 23:13:14 +02:00
|
|
|
* to hold keys etc.
|
2019-03-23 00:58:47 +01:00
|
|
|
*
|
|
|
|
|
* The structs to be hashed must have "key" and "next" fields. The offsets
|
|
|
|
|
* of these fields are passed to HashInit.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* rcsid "$Header$" */
|
|
|
|
|
|
2024-10-16 08:38:41 +02:00
|
|
|
#ifndef _MAGIC__UTILS__IHASH_H
|
|
|
|
|
#define _MAGIC__UTILS__IHASH_H
|
2019-03-23 00:58:47 +01:00
|
|
|
|
2020-04-08 18:28:26 +02:00
|
|
|
/* returns total memory required for malloc of given size, for */
|
|
|
|
|
/* routine IHashStat2() only. */
|
|
|
|
|
|
|
|
|
|
static __inline__ int IHashAlignedSize(int size)
|
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
/* Expand size to be double-word (64 bit) aligned */
|
|
|
|
|
result = ((size + 7) / 8) * 8;
|
2021-06-13 02:29:09 +02:00
|
|
|
return result;
|
2020-04-08 18:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-23 00:58:47 +01:00
|
|
|
/* The IHashTable struct should not be manipulated directly by clients */
|
|
|
|
|
|
|
|
|
|
typedef struct ihashtable
|
|
|
|
|
{
|
|
|
|
|
void **iht_table; /* Pointer to array of pointers. */
|
|
|
|
|
int iht_nBucketsInit; /* Initial size of array. */
|
|
|
|
|
int iht_nBuckets; /* Size of array. */
|
|
|
|
|
int iht_nEntries; /* Number of hashed items */
|
|
|
|
|
int iht_keyOffset; /* offset of keys in client strucs */
|
|
|
|
|
int iht_nextOffset; /* offset of next fields in client strucs */
|
|
|
|
|
int (*iht_hashFn)(void *key); /* Hash function */
|
|
|
|
|
int (*iht_sameKeyFn)(void *key1, void *key2); /* returns 1 if keys match */
|
|
|
|
|
|
|
|
|
|
} IHashTable;
|
2020-05-23 23:13:14 +02:00
|
|
|
|
2019-03-23 00:58:47 +01:00
|
|
|
/* create a new hash table */
|
|
|
|
|
extern IHashTable *IHashInit(
|
2020-05-23 23:13:14 +02:00
|
|
|
int nBuckets,
|
2019-03-23 00:58:47 +01:00
|
|
|
int keyOffset,
|
|
|
|
|
int nextOffset,
|
|
|
|
|
int (*hashFn)(void *key),
|
|
|
|
|
int (*sameKeyFn)(void *key1, void *key2)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/* lookup an entry in table (returns first match) */
|
|
|
|
|
extern void *IHashLookUp(IHashTable *table, void *key);
|
|
|
|
|
|
|
|
|
|
/* lookup NEXT matching entry in table */
|
|
|
|
|
extern void *IHashLookUpNext(IHashTable *table, void *prevEntry);
|
|
|
|
|
|
|
|
|
|
/* add an entry to the table */
|
2020-05-23 23:13:14 +02:00
|
|
|
extern void IHashAdd(IHashTable *table, void *entry);
|
2019-03-23 00:58:47 +01:00
|
|
|
|
|
|
|
|
/* delete an entry from the table */
|
2020-05-23 23:13:14 +02:00
|
|
|
extern void IHashDelete(IHashTable *table, void *entry);
|
2019-03-23 00:58:47 +01:00
|
|
|
|
|
|
|
|
/* delete all entrys (and restore initial hash table size) */
|
|
|
|
|
extern void IHashClear(IHashTable *table);
|
|
|
|
|
|
|
|
|
|
/* callback supplied func for each entry in table */
|
|
|
|
|
extern void IHashEnum(IHashTable *table, void (*clientFunc)(void *entry));
|
|
|
|
|
|
|
|
|
|
/* return number of entries in table */
|
|
|
|
|
extern int IHashEntries(IHashTable *table);
|
|
|
|
|
|
|
|
|
|
/* print hash table statistics */
|
|
|
|
|
extern void IHashStats(IHashTable *table);
|
|
|
|
|
|
|
|
|
|
/* return hashtable memory usage and stats */
|
|
|
|
|
extern int IHashStats2(IHashTable *table, int *nBuckets, int *nEntries);
|
2020-05-23 23:13:14 +02:00
|
|
|
|
2019-03-23 00:58:47 +01:00
|
|
|
/* free hash table (does not free client strucs!) */
|
|
|
|
|
extern void IHashFree(IHashTable *table);
|
|
|
|
|
|
|
|
|
|
/* A hash function suitable for hash key fields that are pointers to strings */
|
|
|
|
|
extern int IHashStringPKeyHash(void *key);
|
|
|
|
|
|
|
|
|
|
/* key comparison function for key fields that are pointers to strings */
|
|
|
|
|
extern int IHashStringPKeyEq(void *key1, void *key2);
|
|
|
|
|
|
|
|
|
|
/* A hash function suitable for hash key fields that are strings */
|
|
|
|
|
extern int IHashStringKeyHash(void *key);
|
|
|
|
|
|
|
|
|
|
/* key comparison function for key fields that are strings */
|
|
|
|
|
extern int IHashStringKeyEq(void *key1, void *key2);
|
|
|
|
|
|
|
|
|
|
/* A hash function suitable for keys that are pointers */
|
|
|
|
|
extern int IHashWordKeyHash(void *key);
|
|
|
|
|
|
|
|
|
|
/* key comparison function for key fields that are pointers */
|
|
|
|
|
extern int IHashWordKeyEq(void *key1, void *key2);
|
|
|
|
|
|
|
|
|
|
/* A hash function suitable for 4 word keys */
|
|
|
|
|
extern int IHash4WordKeyHash(void *key);
|
|
|
|
|
|
|
|
|
|
/* key comparison function for four word keys */
|
|
|
|
|
extern int IHash4WordKeyEq(void *key1, void *key2);
|
2024-10-16 08:38:41 +02:00
|
|
|
#endif /* _MAGIC__UTILS__IHASH_H */
|