symbols.cpp 8.2 KB

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