Annotation of libwww/Library/src/HTAtom.c, revision 2.4

1.1       timbl       1: /*                     Atoms: Names to numbers                 HTAtom.c
                      2: **                     =======================
                      3: **
                      4: **     Atoms are names which are given representative pointer values
                      5: **     so that they can be stored more efficiently, and comparisons
                      6: **     for equality done more efficiently.
                      7: **
                      8: **     Atoms are kept in a hash table consisting of an array of linked lists.
                      9: **
                     10: ** Authors:
                     11: **     TBL     Tim Berners-Lee, WorldWideWeb project, CERN
                     12: **     (c) Copyright CERN 1991 - See Copyright.html
                     13: **
                     14: */
                     15: #define HASH_SIZE      101             /* Tunable */
                     16: #include "HTAtom.h"
                     17: 
                     18: #include <stdio.h>                             /* joe@athena, TBL 921019 */
                     19: #include "HTUtils.h"
                     20: 
                     21: PRIVATE HTAtom * hash_table[HASH_SIZE];
                     22: PRIVATE BOOL initialised = NO;
                     23: 
                     24: #ifdef __STDC__
                     25: PUBLIC HTAtom * HTAtom_for(const char * string)
                     26: #else
                     27: PUBLIC HTAtom * HTAtom_for(string)
                     28:     char * string;
                     29: #endif
                     30: {
                     31:     int hash;
                     32:     CONST char * p;
                     33:     HTAtom * a;
                     34:     
                     35:     /*         First time around, clear hash table
                     36:     */
                     37:     if (!initialised) {
                     38:         int i;
                     39:        for (i=0; i<HASH_SIZE; i++)
                     40:            hash_table[i] = (HTAtom *) 0;
                     41:        initialised = YES;
                     42:     }
                     43:     
                     44:     /*         Generate hash function
                     45:     */
                     46:     for(p=string, hash=0; *p; p++) {
                     47:         hash = (hash * 3 + *p) % HASH_SIZE;
                     48:     }
                     49:     
                     50:     /*         Search for the string in the list
                     51:     */
                     52:     for (a=hash_table[hash]; a; a=a->next) {
                     53:        if (0==strcmp(a->name, string)) {
1.2       timbl      54:            /* if (TRACE) fprintf(stderr,
                     55:                "HTAtom: Old atom %p for `%s'\n", a, string); */
1.1       timbl      56:            return a;                           /* Found: return it */
                     57:        }
                     58:     }
                     59:     
                     60:     /*         Generate a new entry
                     61:     */
                     62:     a = (HTAtom *)malloc(sizeof(*a));
                     63:     if (a == NULL) outofmem(__FILE__, "HTAtom_for");
                     64:     a->name = (char *)malloc(strlen(string)+1);
                     65:     if (a->name == NULL) outofmem(__FILE__, "HTAtom_for");
                     66:     strcpy(a->name, string);
                     67:     a->next = hash_table[hash];                /* Put onto the head of list */
                     68:     hash_table[hash] = a;
1.2       timbl      69: /*    if (TRACE) fprintf(stderr, "HTAtom: New atom %p for `%s'\n", a, string); */
1.1       timbl      70:     return a;
                     71: }
                     72: 
                     73: 

Webmaster