Debian Codegraph @

WWW/Library/Implementation/HTAtom.c

raw
1 /*
2 * $LynxId: HTAtom.c,v 1.22 2018/03/06 09:46:58 tom Exp $
3 *
4 * Atoms: Names to numbers HTAtom.c
5 * =======================
6 *
7 * Atoms are names which are given representative pointer values
8 * so that they can be stored more efficiently, and comparisons
9 * for equality done more efficiently.
10 *
11 * Atoms are kept in a hash table consisting of an array of linked lists.
12 *
13 * Authors:
14 * TBL Tim Berners-Lee, WorldWideWeb project, CERN
15 * (c) Copyright CERN 1991 - See Copyright.html
16 *
17 */
18
19 #include <HTUtils.h>
20 #include <HTAtom.h>
21 #include <HTList.h>
22 #include <LYexit.h>
23 #include <LYLeaks.h>
24
25 #define HASH_SIZE 101 /* Arbitrary prime. Memory/speed tradeoff */
26
27 static HTAtom *hash_table[HASH_SIZE];
28 static BOOL initialised = NO;
29
30 /*
31 * To free off all atoms.
32 */
33 #ifdef LY_FIND_LEAKS
34 static void free_atoms(void);
35 #endif
36
37 #define HASH_FUNCTION(cp_hash) ((strlen(cp_hash) * UCH(*cp_hash)) % HASH_SIZE)
38
39 HTAtom *HTAtom_for(const char *string)
40 {
41 size_t hash;
42 HTAtom *a;
43
44 if (!initialised) {
45 memset(hash_table, 0, sizeof(hash_table));
46 initialised = YES;
47 #ifdef LY_FIND_LEAKS
48 atexit(free_atoms);
49 #endif
50 }
51
52 hash = HASH_FUNCTION(string);
53
54 for (a = hash_table[hash]; a; a = a->next) {
55 if (0 == strcasecomp(a->name, string)) {
56 return a;
57 }
58 }
59
60 a = (HTAtom *) malloc(sizeof(*a));
61 if (a == NULL)
62 outofmem(__FILE__, "HTAtom_for");
63
64 a->name = (char *) malloc(strlen(string) + 1);
65 if (a->name == NULL)
66 outofmem(__FILE__, "HTAtom_for");
67
68 strcpy(a->name, string);
69 a->next = hash_table[hash];
70 hash_table[hash] = a;
71 return a;
72 }
73
74 #ifdef LY_FIND_LEAKS
75 /*
76 * Purpose: Free off all atoms.
77 * Arguments: void
78 * Return Value: void
79 * Remarks/Portability/Dependencies/Restrictions:
80 * To be used at program exit.
81 * Revision History:
82 * 05-29-94 created Lynx 2-3-1 Garrett Arch Blythe
83 */
84 static void free_atoms(void)
85 {
86 auto int i_counter;
87 HTAtom *HTAp_freeme;
88
89 /*
90 * Loop through all lists of atoms.
91 */
92 for (i_counter = 0; i_counter < HASH_SIZE; i_counter++) {
93 /*
94 * Loop through the list.
95 */
96 while (hash_table[i_counter] != NULL) {
97 /*
98 * Free off atoms and any members.
99 */
100 HTAp_freeme = hash_table[i_counter];
101 hash_table[i_counter] = HTAp_freeme->next;
102 FREE(HTAp_freeme->name);
103 FREE(HTAp_freeme);
104 }
105 }
106 }
107 #endif /* LY_FIND_LEAKS */