Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Implementation of the symbol table type.
0004  *
0005  * Author : Stephen Smalley, <sds@tycho.nsa.gov>
0006  */
0007 #include <linux/kernel.h>
0008 #include <linux/string.h>
0009 #include <linux/errno.h>
0010 #include "symtab.h"
0011 
0012 static unsigned int symhash(const void *key)
0013 {
0014     const char *p, *keyp;
0015     unsigned int size;
0016     unsigned int val;
0017 
0018     val = 0;
0019     keyp = key;
0020     size = strlen(keyp);
0021     for (p = keyp; (p - keyp) < size; p++)
0022         val = (val << 4 | (val >> (8*sizeof(unsigned int)-4))) ^ (*p);
0023     return val;
0024 }
0025 
0026 static int symcmp(const void *key1, const void *key2)
0027 {
0028     const char *keyp1, *keyp2;
0029 
0030     keyp1 = key1;
0031     keyp2 = key2;
0032     return strcmp(keyp1, keyp2);
0033 }
0034 
0035 static const struct hashtab_key_params symtab_key_params = {
0036     .hash = symhash,
0037     .cmp = symcmp,
0038 };
0039 
0040 int symtab_init(struct symtab *s, unsigned int size)
0041 {
0042     s->nprim = 0;
0043     return hashtab_init(&s->table, size);
0044 }
0045 
0046 int symtab_insert(struct symtab *s, char *name, void *datum)
0047 {
0048     return hashtab_insert(&s->table, name, datum, symtab_key_params);
0049 }
0050 
0051 void *symtab_search(struct symtab *s, const char *name)
0052 {
0053     return hashtab_search(&s->table, name, symtab_key_params);
0054 }