ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/shstr.C
(Generate patch)

Comparing deliantra/server/common/shstr.C (file contents):
Revision 1.6 by root, Sun Sep 3 11:37:25 2006 UTC vs.
Revision 1.13 by root, Sun Sep 10 16:00:23 2006 UTC

1
1/* 2/*
2 * shstr.C 3 * shstr.C
3 */ 4 */
4 5
5#include <cstring> 6#include <cstring>
6#include <cstdlib> 7#include <cstdlib>
7 8
9#include <glib.h>
10
8#include <tr1/unordered_set> 11#include <tr1/unordered_set>
9 12
10#include "shstr.h" 13#include "shstr.h"
14#include "util.h"
11 15
12// NOTE: even with lots of stuff loaded, we do not usually have >>20000 strings. 16typedef
13// maybe refcounting is just overhead? 17std::tr1::unordered_set < const char *,
18 str_hash,
19 str_equal >
20 HT;
14 21
15struct hash 22static HT
23 ht;
24
25static const char *
26makevec (const char *s)
16{ 27{
17 std::size_t operator ()(const char *s) const 28 int
18 { 29 len = strlen (s);
19 unsigned long hash = 0;
20 unsigned int i = 0;
21 30
22 /* use the one-at-a-time hash function, which supposedly is 31 const char *
23 * better than the djb2-like one used by perl5.005, but 32 v = (const char *) (2 + (int *) g_slice_alloc (sizeof (int) * 2 + len + 1));
24 * certainly is better then the bug used here before.
25 * see http://burtleburtle.net/bob/hash/doobs.html
26 */
27 while (*s)
28 {
29 hash += *s++;
30 hash += hash << 10;
31 hash ^= hash >> 6;
32 }
33 33
34 hash += hash << 3; 34 shstr::length (v) = len;
35 hash ^= hash >> 11; 35 shstr::refcnt (v) = 1;
36 hash += hash << 15;
37 36
38 return hash; 37 memcpy ((char *) v, s, len + 1);
39 }
40};
41 38
42struct equal 39 return v;
43{ 40}
44 bool operator ()(const char *a, const char *b) const
45 {
46 return !strcmp (a, b);
47 }
48};
49 41
50typedef std::tr1::unordered_set<const char *, hash, equal> HT; 42const char *
43 shstr::null = makevec ("<nil>");
51 44
52static HT ht; 45// what weird misoptimisation is this again?
46const shstr undead_name ("undead");
53 47
54const char * 48const char *
55shstr::find (const char *s) 49shstr::find (const char *s)
56{ 50{
57 if (!s) 51 if (!s)
58 return s; 52 return s;
59 53
60 HT::iterator i = ht.find (s); 54 HT::iterator i = ht.find (s);
61 55
62 return i != ht.end () 56 return i != ht.end ()? *i : 0;
63 ? *i
64 : 0;
65} 57}
66 58
67const char * 59const char *
68shstr::intern (const char *s) 60shstr::intern (const char *s)
69{ 61{
70 if (!s) 62 if (!s)
71 return s; 63 return null;
72 64
73 if (const char *found = find (s)) 65 if (const char *found = find (s))
74 { 66 {
75 ++refcnt (found); 67 ++refcnt (found);
76 return found; 68 return found;
77 } 69 }
78 70
79 int len = strlen (s); 71 s = makevec (s);
80
81 const char *v = (const char *)(2 + (int *)malloc (sizeof (int) * 2 + len + 1));
82
83 length (v) = len;
84 refcnt (v) = 1;
85
86 memcpy ((char *)v, s, len + 1);
87
88 ht.insert (v); 72 ht.insert (s);
89
90 return v; 73 return s;
91} 74}
92 75
93// periodically test refcounts == 0 for a few strings 76// periodically test refcounts == 0 for a few strings
94// this is the ONLY thing that erases stuff from ht. keep it that way. 77// this is the ONLY thing that erases stuff from ht. keep it that way.
95void 78void
116 break; 99 break;
117 else if (!refcnt (*i)) 100 else if (!refcnt (*i))
118 { 101 {
119 HT::iterator o = i++; 102 HT::iterator o = i++;
120 const char *s = *o; 103 const char *s = *o;
104
121 ht.erase (o); 105 ht.erase (o);
122 106
123 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); 107 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s));
124 free (-2 + (int *)s); 108 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s);
125 } 109 }
126 else 110 else
127 ++i; 111 ++i;
128 } 112 }
129 113
130 curpos = *i; 114 curpos = *i;
131} 115}
132 116
133//TODO: this should of course not be here 117//TODO: this should of course not be here
118
134/* buf_overflow() - we don't want to exceed the buffer size of 119/* buf_overflow() - we don't want to exceed the buffer size of
135 * buf1 by adding on buf2! Returns true if overflow will occur. 120 * buf1 by adding on buf2! Returns true if overflow will occur.
136 */ 121 */
137 122
138int 123int
139buf_overflow (const char *buf1, const char *buf2, int bufsize) 124buf_overflow (const char *buf1, const char *buf2, int bufsize)
140{ 125{
141 int len1 = 0, len2 = 0; 126 int len1 = 0, len2 = 0;
142 127
143 if (buf1) 128 if (buf1)
144 len1 = strlen (buf1); 129 len1 = strlen (buf1);
145 if (buf2) 130 if (buf2)
146 len2 = strlen (buf2); 131 len2 = strlen (buf2);
147 if ((len1 + len2) >= bufsize) 132 if ((len1 + len2) >= bufsize)
148 return 1;
149 return 0; 133 return 1;
134 return 0;
150} 135}
151

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines