1 //-< SYMTAB.CPP >----------------------------------------------------*--------*
2 // GigaBASE                  Version 1.0         (c) 1999  GARRET    *     ?  *
3 // (Post Relational Database Management System)                      *   /\|  *
4 //                                                                   *  /  \  *
5 //                          Created:     20-Nov-98    K.A. Knizhnik  * / [] \ *
6 //                          Last update: 20-Nov-98    K.A. Knizhnik  * GARRET *
7 //-------------------------------------------------------------------*--------*
8 // Symbol table implementation
9 //-------------------------------------------------------------------*--------*
10 
11 #define INSIDE_GIGABASE
12 
13 #include "stdtp.h"
14 #include "sync.h"
15 #include "symtab.h"
16 
17 BEGIN_GIGABASE_NAMESPACE
18 
19 const size_t hashTableSize = 1009;
20 dbSymbolTable::HashTableItem* dbSymbolTable::hashTable[hashTableSize];
21 
22 dbSymbolTable dbSymbolTable::instance;
23 
~dbSymbolTable()24 dbSymbolTable::~dbSymbolTable() {
25     cleanup();
26 }
27 
28 
cleanup()29 void dbSymbolTable::cleanup() {
30     for (int i = hashTableSize; --i >= 0;) {
31         HashTableItem *ip, *next;
32         for (ip = hashTable[i]; ip != NULL; ip = next) {
33             next = ip->next;
34             delete ip;
35         }
36         hashTable[i] = NULL;
37     }
38 }
39 
add(char_t * & str,int tag,bool allocate)40 int dbSymbolTable::add(char_t* &str, int tag, bool allocate) {
41     static dbMutex mutex;
42     dbCriticalSection cs(mutex);
43     unsigned hash = 0;
44     char_t* p = str;
45     while (*p != 0) {
46         hash = hash*31 + *p++;
47     }
48     int index = hash % hashTableSize;
49     HashTableItem *ip;
50     for (ip = hashTable[index]; ip != NULL; ip = ip->next) {
51         if (ip->hash == hash && STRCMP(ip->str, str) == 0) {
52             str = ip->str;
53             if (tag > ip->tag) {
54                 ip->tag = tag;
55             }
56             return ip->tag;
57         }
58     }
59     ip = new HashTableItem;
60     ip->allocated = false;
61     if (allocate) {
62         char_t* dupstr = new char_t[STRLEN(str) + 1];
63         STRCPY(dupstr, str);
64         ip->allocated = true;
65         str = dupstr;
66     }
67     ip->str = str;
68     ip->hash = hash;
69     ip->tag = tag;
70     ip->next = hashTable[index];
71     hashTable[index] = ip;
72     return tag;
73 }
74 
75 
76 END_GIGABASE_NAMESPACE
77 
78