symbols.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * Description (symbols.c)
  14. *
  15. * This module implements a symbol table for ACL-related structures.
  16. * The symbol table associates string names and types with pointers
  17. * to various kinds of structures.
  18. */
  19. /*
  20. #include <base/systems.h>
  21. */
  22. #include <plhash.h>
  23. #include <base/util.h>
  24. #include <netsite.h>
  25. #define __PRIVATE_SYMBOLS
  26. #include "libaccess/symbols.h"
  27. #include <ctype.h>
  28. static PLHashEntry * symAllocEntry(void * pool, const void *unused);
  29. static void * symAllocTable(void * pool, PRSize size);
  30. static int symCmpName(const void * name1, const void * name2);
  31. static int symCmpValue(const void * value1, const void * value2);
  32. static PLHashNumber symHash(const void * symkey);
  33. static void symFreeEntry(void * pool, PLHashEntry * he, PRUintn flag);
  34. static void symFreeTable(void * pool, void * item);
  35. /* Table of pointers to functions associated with the hash table */
  36. static PLHashAllocOps SymAllocOps = {
  37. symAllocTable, /* allocate the hash table */
  38. symFreeTable, /* free the hash table */
  39. symAllocEntry, /* allocate a table entry */
  40. symFreeEntry, /* free a table entry */
  41. };
  42. static void * symAllocTable(void * pool, PRSize size)
  43. {
  44. return (void *)PERM_MALLOC(size);
  45. }
  46. static void symFreeTable(void * pool, void * item)
  47. {
  48. PERM_FREE(item);
  49. }
  50. static PLHashEntry * symAllocEntry(void * pool, const void *ignored)
  51. {
  52. PLHashEntry * he;
  53. he = (PLHashEntry *) PERM_MALLOC(sizeof(PLHashEntry));
  54. return he;
  55. }
  56. static void symFreeEntry(void * pool, PLHashEntry * he, PRUintn flag)
  57. {
  58. if (flag == HT_FREE_ENTRY) {
  59. /* Just free the hash entry, not anything it references */
  60. PERM_FREE(he);
  61. }
  62. }
  63. static int symCmpName(const void * name1, const void * name2)
  64. {
  65. Symbol_t * sym1 = (Symbol_t *)name1;
  66. Symbol_t * sym2 = (Symbol_t *)name2;
  67. return ((sym1->sym_type == sym2->sym_type) &&
  68. !strcasecmp(sym1->sym_name, sym2->sym_name));
  69. }
  70. static int symCmpValue(const void * value1, const void * value2)
  71. {
  72. return (value1 == value2);
  73. }
  74. static PLHashNumber symHash(const void * symkey)
  75. {
  76. Symbol_t * sym = (Symbol_t *)symkey;
  77. const char * cp;
  78. PLHashNumber h;
  79. h = sym->sym_type;
  80. cp = sym->sym_name;
  81. if (cp) {
  82. while (*cp) {
  83. h = (h << 3) ^ tolower(*cp);
  84. ++cp;
  85. }
  86. }
  87. return h;
  88. }
  89. /* Helper function for symTableEnumerate() */
  90. typedef struct {
  91. int (*func)(Symbol_t * sym, void * parg);
  92. void * argp;
  93. } SymTableEnum_t;
  94. static int symTableEnumHelp(PLHashEntry * he, int n, void * step)
  95. {
  96. SymTableEnum_t * ste = (SymTableEnum_t *)step;
  97. int ret = 0;
  98. int rv;
  99. rv = (*ste->func)((Symbol_t *)(he->key), ste->argp);
  100. if (rv != 0) {
  101. if (rv & SYMENUMREMOVE) ret = HT_ENUMERATE_REMOVE;
  102. if (rv & SYMENUMSTOP) ret |= HT_ENUMERATE_STOP;
  103. }
  104. return ret;
  105. }
  106. NSPR_BEGIN_EXTERN_C
  107. /*
  108. * Description (symTableAddSym)
  109. *
  110. * This function adds a symbol definition to the symbol table.
  111. * The symbol definition includes a name string, a type, and a
  112. * reference to a structure.
  113. *
  114. * Arguments:
  115. *
  116. * table - handle for symbol table
  117. * newsym - pointer to new symbol name and type
  118. * symref - pointer to structure named by symbol
  119. *
  120. * Returns:
  121. *
  122. * If successful, the return code is zero. An error is indicated
  123. * by a negative return code (SYMERRxxxx - see symbols.h).
  124. */
  125. int symTableAddSym(void * table, Symbol_t * newsym, void * symref)
  126. {
  127. SymTable_t * st = (SymTable_t *)table;
  128. PLHashEntry **hep;
  129. PLHashNumber keyhash;
  130. int rv = 0;
  131. /* Compute the hash value for this symbol */
  132. keyhash = symHash((const void *)newsym);
  133. crit_enter(st->stb_crit);
  134. /* See if another symbol already has the same name and type */
  135. hep = PL_HashTableRawLookup(st->stb_ht, keyhash, (void *)newsym);
  136. if (*hep == 0) {
  137. /* Expand the hash table if necessary and allocate an entry */
  138. PL_HashTableRawAdd(st->stb_ht,
  139. hep, keyhash, (void *)newsym, symref);
  140. }
  141. else {
  142. /* The symbol is already there. It's an error */
  143. rv = SYMERRDUPSYM;
  144. }
  145. crit_exit(st->stb_crit);
  146. return rv;
  147. }
  148. /*
  149. * Description (symTableRemoveSym)
  150. *
  151. * This function removes an entry from a symbol table. It does
  152. * not free the entry itself, just the hash entry that references
  153. * it.
  154. *
  155. * Arguments:
  156. *
  157. * table - symbol table handle
  158. * sym - pointer to symbol structure
  159. */
  160. void symTableRemoveSym(void * table, Symbol_t * sym)
  161. {
  162. SymTable_t * st = (SymTable_t *)table;
  163. if (sym->sym_name != 0) {
  164. crit_enter(st->stb_crit);
  165. PL_HashTableRemove(st->stb_ht, (void *)sym);
  166. crit_exit(st->stb_crit);
  167. }
  168. }
  169. /*
  170. * Description (symTableEnumerate)
  171. *
  172. * This function enumerates all of the entries in a symbol table,
  173. * calling a specified function for each entry. The function
  174. * specified by the caller may return flags indicating actions
  175. * to be taken for each entry or whether to terminate the
  176. * enumeration. These flags are defined in symbols.h as
  177. * SYMENUMxxxx.
  178. *
  179. * Arguments:
  180. *
  181. * table - symbol table handle
  182. * argp - argument for caller function
  183. * func - function to be called for each entry
  184. */
  185. void symTableEnumerate(void * table, void * argp,
  186. int (*func)(Symbol_t * sym, void * parg))
  187. {
  188. SymTable_t * st = (SymTable_t *)table;
  189. SymTableEnum_t ste; /* enumeration arguments */
  190. ste.func = func;
  191. ste.argp = argp;
  192. crit_enter(st->stb_crit);
  193. (void)PL_HashTableEnumerateEntries(st->stb_ht,
  194. symTableEnumHelp, (void *)&ste);
  195. crit_exit(st->stb_crit);
  196. }
  197. /*
  198. * Description (symTableFindSym)
  199. *
  200. * This function locates a symbol with a specified name and type
  201. * in a given symbol table. It returns a pointer to the structure
  202. * named by the symbol.
  203. *
  204. * Arguments:
  205. *
  206. * table - symbol table handle
  207. * symname - symbol name string pointer
  208. * symtype - symbol type code
  209. * psymref - pointer to returned structure pointer
  210. *
  211. * Returns:
  212. *
  213. * If successful, the return code is zero and the structure pointer
  214. * associated with the symbol name and type is returned in the
  215. * location specified by 'psymref'. An error is indicated by a
  216. * negative return code (SYMERRxxxx - see symbols.h).
  217. */
  218. int symTableFindSym(void * table, const char * symname,
  219. int symtype, void **psymref)
  220. {
  221. SymTable_t * st = (SymTable_t *)table;
  222. Symbol_t sym;
  223. void * symref;
  224. /* Create temporary entry with fields needed by symHash() */
  225. sym.sym_name = symname;
  226. sym.sym_type = symtype;
  227. crit_enter(st->stb_crit);
  228. symref = PL_HashTableLookup(st->stb_ht, (void *)&sym);
  229. crit_exit(st->stb_crit);
  230. *psymref = symref;
  231. return (symref) ? 0 : SYMERRNOSYM;
  232. }
  233. /*
  234. * Description (symTableDestroy)
  235. *
  236. * This function destroys a symbol table created by symTableNew().
  237. *
  238. * Arguments:
  239. *
  240. * table - symbol table handle from symTableNew()
  241. * flags - bit flags (unused - must be zero)
  242. */
  243. void symTableDestroy(void * table, int flags)
  244. {
  245. SymTable_t * st = (SymTable_t *)table;
  246. if (st) {
  247. if (st->stb_crit) {
  248. crit_terminate(st->stb_crit);
  249. }
  250. if (st->stb_ht) {
  251. PL_HashTableDestroy(st->stb_ht);
  252. }
  253. PERM_FREE(st);
  254. }
  255. }
  256. /*
  257. * Description (symTableNew)
  258. *
  259. * This function creates a new symbol table, and returns a handle
  260. * for it.
  261. *
  262. * Arguments:
  263. *
  264. * ptable - pointer to returned symbol table handle
  265. *
  266. * Returns:
  267. *
  268. * If successful, the return code is zero and a handle for the new
  269. * symbol table is returned in the location specified by 'ptable'.
  270. * An error is indicated by a negative return code (SYMERRxxxx
  271. * - see symbols.h).
  272. */
  273. int symTableNew(void **ptable)
  274. {
  275. SymTable_t * st;
  276. /* Allocate the symbol table object */
  277. st = (SymTable_t *)PERM_MALLOC(sizeof(SymTable_t));
  278. if (st == 0) goto err_nomem;
  279. /* Get a monitor for it */
  280. st->stb_crit = crit_init();
  281. st->stb_ht = PL_NewHashTable(0, symHash, symCmpName, symCmpValue,
  282. &SymAllocOps, 0);
  283. if (st->stb_ht == 0) goto err_nomem;
  284. *ptable = st;
  285. return 0;
  286. err_nomem:
  287. if (st) {
  288. symTableDestroy(st, 0);
  289. }
  290. return SYMERRNOMEM;
  291. }
  292. NSPR_END_EXTERN_C