Removed warnings, changes for compiling with Visual C++ and xlc
[dyninst.git] / common / src / stringPool.C
1 /*
2  * Copyright (c) 1996 Barton P. Miller
3  * 
4  * We provide the Paradyn Parallel Performance Tools (below
5  * described as Paradyn") on an AS IS basis, and do not warrant its
6  * validity or performance.  We reserve the right to update, modify,
7  * or discontinue this software at any time.  We shall have no
8  * obligation to supply such updates or modifications or any other
9  * form of support to you.
10  * 
11  * This license is for research uses.  For such uses, there is no
12  * charge. We define "research use" to mean you may freely use it
13  * inside your organization for whatever purposes you see fit. But you
14  * may not re-distribute Paradyn or parts of Paradyn, in any form
15  * source or binary (including derivatives), electronic or otherwise,
16  * to any other organization or entity without our permission.
17  * 
18  * (for other uses, please contact us at paradyn@cs.wisc.edu)
19  * 
20  * All warranties, including without limitation, any warranty of
21  * merchantability or fitness for a particular purpose, are hereby
22  * excluded.
23  * 
24  * By your use of Paradyn, you understand and agree that we (or any
25  * other person or entity with proprietary rights in Paradyn) are
26  * under no obligation to provide either maintenance services,
27  * update services, notices of latent defects, or correction of
28  * defects for Paradyn.
29  * 
30  * Even if advised of the possibility of such damages, under no
31  * circumstances shall we (or any other person or entity with
32  * proprietary rights in the software licensed hereunder) be liable
33  * to you or any third party for direct, indirect, or consequential
34  * damages of any character regardless of type of action, including,
35  * without limitation, loss of profits, loss of use, loss of good
36  * will, or computer failure or malfunction.  You agree to indemnify
37  * us (and any other person or entity with proprietary rights in the
38  * software licensed hereunder) for any and all liability it may
39  * incur to third parties resulting from your use of Paradyn.
40  */
41
42 /*
43  * 
44  * $Log: stringPool.C,v $
45  * Revision 1.10  1996/11/12 17:50:17  mjrg
46  * Removed warnings, changes for compiling with Visual C++ and xlc
47  *
48  * Revision 1.9  1996/08/16 21:32:05  tamches
49  * updated copyright for release 1.1
50  *
51  * Revision 1.8  1995/11/28 15:58:35  naim
52  * Minor fix. Chaning constant 4090 to SP_PAGE_SIZE - naim
53  *
54  * Revision 1.7  1995/02/16  09:28:13  markc
55  * Removed compiler warnings.
56  * Changed Boolean to bool
57  *
58  * Revision 1.6  1994/09/22  03:19:13  markc
59  * Changed private pointers to char*
60  *
61  * Revision 1.5  1994/08/05  16:02:05  hollings
62  * More consistant use of stringHandle vs. char *.
63  *
64  * Revision 1.4  1994/07/28  22:22:06  krisna
65  * changed definitions of ReadFunc and WriteFunc to conform to prototypes
66  *
67  * Revision 1.3  1994/07/14  23:43:14  hollings
68  * added abort for malloc failure.
69  *
70  * Revision 1.2  1994/01/26  04:53:43  hollings
71  * Change to using <module>/h/{*.h}
72  *
73  */
74
75 #include <stdlib.h>
76 #include <string.h>
77
78 #include "util/h/list.h"
79 #include "util/h/stringPool.h"
80
81 #define SP_PAGE_SIZE 4090
82
83 /*
84  * Hash Function from Aho, Sethi, Ulman _Compilers_ (Second Edition)
85  *   page 436.
86  *
87  */
88 static int hash(const char *ch, int size)
89 {
90     register unsigned int h = 0, g;
91
92     for (; *ch != '\0'; ch++) {
93         h = (h << 4) + (*ch);
94         if ((g = (h & 0xf0000000))) {
95             h = h ^ (g >> 24);
96             h = h ^ g;
97         }
98     }
99     return(h % size);
100 }
101
102 stringPool::stringPool()
103 {
104     memset(table, '\0', sizeof(table));
105     currPage = new char[SP_PAGE_SIZE];
106     currPos = currPage;
107 }
108
109 stringHandle stringPool::find(const char *data)
110 {
111     int hid;
112     stringEntry *curr;
113
114     hid = hash(data, TAB_SIZE);
115     for (curr=table[hid]; curr; curr=curr->next) {
116         if (!strcmp(data, curr->data)) {
117             return(curr->data);
118         }
119     }
120     return(NULL);
121 }
122
123 char *stringPool::getSpace(int size)
124 {
125     char *ret;
126
127     if ((int)(currPos - currPage) + size > SP_PAGE_SIZE) {
128       // create a new page.
129       currPage = new char[SP_PAGE_SIZE];
130       if (!currPage) abort();
131       currPos = currPage;
132     }
133     ret = currPos;
134     currPos += size;
135     return(ret);
136 }
137
138 stringHandle stringPool::findAndAdd(const char *data)
139 {
140     int hid;
141     stringHandle val;
142     stringEntry *temp;
143
144     val = find(data);
145     if (!val) {
146         // add it.
147         hid = hash(data, TAB_SIZE);
148         temp = new stringEntry;
149         temp->data = getSpace(strlen(data)+1);
150         strcpy(temp->data, data);
151         temp->next = table[hid];
152         table[hid] = temp;
153         val = (stringHandle) temp->data;
154     }
155     return(val);
156 }